git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 08/10] compat/regex: explicitly mark intentional use of the comma operator

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 25, 2025, 23:32 UTC
Message-ID
<dc626f36df34df4897289e508dbf608512a93870.1742945534.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1889.v2.git.1742945534.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

The comma operator is a somewhat obscure C feature that is often used by mistake and can even cause unintentional code flow. That is why the `-Wcomma` option of clang was introduced: To identify unintentional uses of the comma operator.

In the `compat/regex/` code, the comma operator is used twice, once to avoid surrounding two conditional statements with curly brackets, the other one to increment two counters simultaneously in a `do ... while` condition.

The first one is replaced with a proper conditional block, surrounded by curly brackets.

The second one would be harder to replace because the loop contains two `continue`s. Therefore, the second one is marked as intentional by casting the value-to-discard to `void`.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/regex/regex_internal.c | 7 ++++---
 compat/regex/regexec.c        | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/compat/regex/regex_internal.c b/compat/regex/regex_internal.c
index ec5cc5d2dd1..7672583bf7e 100644
--- a/compat/regex/regex_internal.c
+++ b/compat/regex/regex_internal.c
@@ -1231,9 +1231,10 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
   for (sbase = dest->nelem + 2 * src->nelem,
        is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
     {
-      if (dest->elems[id] == src->elems[is])
-	is--, id--;
-      else if (dest->elems[id] < src->elems[is])
+      if (dest->elems[id] == src->elems[is]) {
+	is--;
+	id--;
+      } else if (dest->elems[id] < src->elems[is])
 	dest->elems[--sbase] = src->elems[is--];
       else /* if (dest->elems[id] > src->elems[is]) */
 	--id;
diff --git a/compat/regex/regexec.c b/compat/regex/regexec.c
index 2eeec82f407..c08f1bbe1f5 100644
--- a/compat/regex/regexec.c
+++ b/compat/regex/regexec.c
@@ -2210,7 +2210,7 @@ sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
 	  /* mctx->bkref_ents may have changed, reload the pointer.  */
 	  entry = mctx->bkref_ents + enabled_idx;
 	}
-      while (enabled_idx++, entry++->more);
+      while ((void)enabled_idx++, entry++->more);
     }
   err = REG_NOERROR;
  free_return:
-- 
gitgitgadget
Previous: Junio C HamanoNext: Taylor Blau
Message 35 of 64 in “Avoid the comma operator”
  1. 0/2 Avoid the comma operatorJohannes Schindelin via GitGitGadget, Mar 25, 2025
  2. 1/2 remote-curl: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  3. Phillip WoodMar 25, 2025
  4. Jeff KingMar 25, 2025
  5. 2/2 rebase: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  6. Phillip WoodMar 25, 2025
  7. Philip OakleyMar 25, 2025
  8. Johannes SchindelinMar 25, 2025
  9. Taylor BlauMar 26, 2025
  10. Patrick SteinhardtMar 25, 2025
  11. Johannes SchindelinMar 25, 2025
  12. Taylor BlauMar 26, 2025
  13. Karthik NayakMar 25, 2025
  14. Junio C HamanoMar 25, 2025
  15. 00/10 Avoid the comma operatorJohannes Schindelin via GitGitGadget, Mar 25, 2025
  16. 01/10 remote-curl: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  17. 02/10 rebase: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  18. 03/10 kwset: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  19. 04/10 clar: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  20. Patrick SteinhardtMar 26, 2025
  21. Johannes SchindelinMar 26, 2025
  22. 05/10 xdiff: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 25, 2025
  23. 06/10 diff-delta: explicitly mark intentional use of the comma operatorJohannes Schindelin via GitGitGadget, Mar 25, 2025
  24. Patrick SteinhardtMar 26, 2025
  25. Johannes SchindelinMar 26, 2025
  26. Phillip WoodMar 26, 2025
  27. Taylor BlauMar 26, 2025
  28. Chris TorekMar 27, 2025
  29. 07/10 wildmatch: explicitly mark intentional use of the comma operatorJohannes Schindelin via GitGitGadget, Mar 25, 2025
  30. Patrick SteinhardtMar 26, 2025
  31. Johannes SchindelinMar 26, 2025
  32. Junio C HamanoMar 26, 2025
  33. Phillip WoodMar 26, 2025
  34. Junio C HamanoMar 26, 2025
  35. 08/10 compat/regex: explicitly mark intentional use of the comma operatorJohannes Schindelin via GitGitGadget, Mar 25, 2025
  36. Taylor BlauMar 26, 2025
  37. Johannes SchindelinMar 27, 2025
  38. Taylor BlauMar 27, 2025
  39. 09/10 clang: warn when the comma operator is usedJohannes Schindelin via GitGitGadget, Mar 25, 2025
  40. Patrick SteinhardtMar 26, 2025
  41. Johannes SchindelinMar 26, 2025
  42. Patrick SteinhardtMar 26, 2025
  43. Johannes SchindelinMar 27, 2025
  44. 10/10 detect-compiler: detect clang even if it found CUDAJohannes Schindelin via GitGitGadget, Mar 25, 2025
  45. Jeff KingMar 26, 2025
  46. Eric SunshineMar 26, 2025
  47. Jeff KingMar 27, 2025
  48. Eric SunshineMar 27, 2025
  49. Jeff KingMar 27, 2025
  50. Patrick SteinhardtMar 26, 2025
  51. Jeff KingMar 26, 2025
  52. 00/10 Avoid the comma operatorJohannes Schindelin via GitGitGadget, Mar 27, 2025
  53. 02/10 rebase: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 27, 2025
  54. 01/10 remote-curl: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 27, 2025
  55. 03/10 kwset: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 27, 2025
  56. 04/10 clar: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 27, 2025
  57. 05/10 xdiff: avoid using the comma operator unnecessarilyJohannes Schindelin via GitGitGadget, Mar 27, 2025
  58. 06/10 diff-delta: avoid using the comma operatorJohannes Schindelin via GitGitGadget, Mar 27, 2025
  59. 07/10 wildmatch: avoid using of the comma operatorJohannes Schindelin via GitGitGadget, Mar 27, 2025
  60. 08/10 compat/regex: explicitly mark intentional use of the comma operatorJohannes Schindelin via GitGitGadget, Mar 27, 2025
  61. 09/10 clang: warn when the comma operator is usedJohannes Schindelin via GitGitGadget, Mar 27, 2025
  62. 10/10 detect-compiler: detect clang even if it found CUDAJohannes Schindelin via GitGitGadget, Mar 27, 2025
  63. Phillip WoodMar 27, 2025
  64. Junio C HamanoMar 29, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.