* [PATCH] mingw: avoid the comma operator
@ 2025-11-17 20:46 Johannes Schindelin via GitGitGadget
2025-11-17 22:16 ` Junio C Hamano
0 siblings, 1 reply; 3+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2025-11-17 20:46 UTC (permalink / raw)
To: git; +Cc: Johannes Schindelin, Johannes Schindelin
From: Johannes Schindelin <johannes.schindelin@gmx.de>
The pattern `return errno = ..., -1;` is observed several times in
`compat/mingw.c`. It has served us well over the years, but now clang
starts complaining:
compat/mingw.c:723:24: error: possible misuse of comma operator here [-Werror,-Wcomma]
723 | return errno = ENOSYS, -1;
| ^
See for example this failing workflow run:
https://github.com/git-for-windows/git-sdk-arm64/actions/runs/15457893907/job/43513458823#step:8:201
Let's appease clang (and also reduce the use of the no longer common
comma operator).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
mingw: avoid the comma operator
I wonder how many more times I will deal with the comma operator...
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2007%2Fdscho%2Fmingw-avoid-the-comma-operator-5660--v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2007/dscho/mingw-avoid-the-comma-operator-5660--v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2007
compat/mingw.c | 48 ++++++++++++++++++++++++++++--------------------
1 file changed, 28 insertions(+), 20 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 736a07a028..90ba5cea9d 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -491,8 +491,10 @@ static int mingw_open_append(wchar_t const *wfilename, int oflags, ...)
DWORD create = (oflags & O_CREAT) ? OPEN_ALWAYS : OPEN_EXISTING;
/* only these flags are supported */
- if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND))
- return errno = ENOSYS, -1;
+ if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND)) {
+ errno = ENOSYS;
+ return -1;
+ }
/*
* FILE_SHARE_WRITE is required to permit child processes
@@ -2450,12 +2452,14 @@ static int start_timer_thread(void)
timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
if (timer_event) {
timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
- if (!timer_thread )
- return errno = ENOMEM,
- error("cannot start timer thread");
- } else
- return errno = ENOMEM,
- error("cannot allocate resources for timer");
+ if (!timer_thread ) {
+ errno = ENOMEM;
+ return error("cannot start timer thread");
+ }
+ } else {
+ errno = ENOMEM;
+ return error("cannot allocate resources for timer");
+ }
return 0;
}
@@ -2488,13 +2492,15 @@ int setitimer(int type UNUSED, struct itimerval *in, struct itimerval *out)
static const struct timeval zero;
static int atexit_done;
- if (out)
- return errno = EINVAL,
- error("setitimer param 3 != NULL not implemented");
+ if (out) {
+ errno = EINVAL;
+ return error("setitimer param 3 != NULL not implemented");
+ }
if (!is_timeval_eq(&in->it_interval, &zero) &&
- !is_timeval_eq(&in->it_interval, &in->it_value))
- return errno = EINVAL,
- error("setitimer: it_interval must be zero or eq it_value");
+ !is_timeval_eq(&in->it_interval, &in->it_value)) {
+ errno = EINVAL;
+ return error("setitimer: it_interval must be zero or eq it_value");
+ }
if (timer_thread)
stop_timer_thread();
@@ -2516,12 +2522,14 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out)
{
if (sig == SIGCHLD)
return -1;
- else if (sig != SIGALRM)
- return errno = EINVAL,
- error("sigaction only implemented for SIGALRM");
- if (out)
- return errno = EINVAL,
- error("sigaction: param 3 != NULL not implemented");
+ else if (sig != SIGALRM) {
+ errno = EINVAL;
+ return error("sigaction only implemented for SIGALRM");
+ }
+ if (out) {
+ errno = EINVAL;
+ return error("sigaction: param 3 != NULL not implemented");
+ }
timer_fn = in->sa_handler;
return 0;
base-commit: 9a2fb147f2c61d0cab52c883e7e26f5b7948e3ed
--
gitgitgadget
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] mingw: avoid the comma operator
2025-11-17 20:46 [PATCH] mingw: avoid the comma operator Johannes Schindelin via GitGitGadget
@ 2025-11-17 22:16 ` Junio C Hamano
2025-11-18 9:49 ` Jeff King
0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2025-11-17 22:16 UTC (permalink / raw)
To: Johannes Schindelin via GitGitGadget; +Cc: git, Johannes Schindelin
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
writes:
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> The pattern `return errno = ..., -1;` is observed several times in
> `compat/mingw.c`. It has served us well over the years, but now clang
> starts complaining:
>
> compat/mingw.c:723:24: error: possible misuse of comma operator here [-Werror,-Wcomma]
> 723 | return errno = ENOSYS, -1;
> | ^
>
> See for example this failing workflow run:
> https://github.com/git-for-windows/git-sdk-arm64/actions/runs/15457893907/job/43513458823#step:8:201
>
> Let's appease clang (and also reduce the use of the no longer common
> comma operator).
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
> mingw: avoid the comma operator
>
> I wonder how many more times I will deal with the comma operator...
;-)
> /* only these flags are supported */
> - if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND))
> - return errno = ENOSYS, -1;
> + if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND)) {
> + errno = ENOSYS;
> + return -1;
> + }
Good riddance. It indeed is somewhat hard to read, especially
because it may not be apparent to readers how "A = B, C" binds
(answer: B gets assigned to A and then the whole thing yields C).
I wonder if
return (errno = ENOSYS), -1;
is accepted by the compiler, but in these error handling we do not
have to be cute, and updated code that is both simple and stupid
reads very well.
Will queue. Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mingw: avoid the comma operator
2025-11-17 22:16 ` Junio C Hamano
@ 2025-11-18 9:49 ` Jeff King
0 siblings, 0 replies; 3+ messages in thread
From: Jeff King @ 2025-11-18 9:49 UTC (permalink / raw)
To: Junio C Hamano
Cc: Johannes Schindelin via GitGitGadget, git, Johannes Schindelin
On Mon, Nov 17, 2025 at 02:16:34PM -0800, Junio C Hamano wrote:
> > /* only these flags are supported */
> > - if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND))
> > - return errno = ENOSYS, -1;
> > + if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND)) {
> > + errno = ENOSYS;
> > + return -1;
> > + }
>
> Good riddance. It indeed is somewhat hard to read, especially
> because it may not be apparent to readers how "A = B, C" binds
> (answer: B gets assigned to A and then the whole thing yields C).
>
> I wonder if
>
> return (errno = ENOSYS), -1;
>
> is accepted by the compiler, but in these error handling we do not
> have to be cute, and updated code that is both simple and stupid
> reads very well.
Agreed that we are best avoiding comma operators when we can. There is
one spot where we use it, though, and I haven't figured out a good way
around it: in the error() macro wrapper.
I guess it does not cause the same compiler complaints because there is
no assignment in it. So if nobody is complaining, we can just avert our
eyes when looking at the macro. :)
-Peff
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-18 9:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-17 20:46 [PATCH] mingw: avoid the comma operator Johannes Schindelin via GitGitGadget
2025-11-17 22:16 ` Junio C Hamano
2025-11-18 9:49 ` Jeff King
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).