]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: avoid false positive for useless casts and generic [PR125261]
authorMartin Uecker <uecker@tugraz.at>
Tue, 12 May 2026 05:11:38 +0000 (07:11 +0200)
committerMartin Uecker <uecker@gcc.gnu.org>
Wed, 13 May 2026 04:57:26 +0000 (06:57 +0200)
To reduce the number of false positives, we guard -Wuseless-cast by
c_inhibit_evaluation_warnings and also increment it for a generic
association if we have seen a prior match for a (non-default)
association.  This covers the common case where the default association
comes last.  If there is another association selected after we have
seen a default, we still have false positives.

PR c/125261

gcc/c/ChangeLog:
* c-parser.cc (c_parser_generic_selection): Modify logic for
c_inhibit_evaluation_warnings.
* c-typeck.cc (build_c_cast): Use c_inhibit_evaluation_warnings.

gcc/testsuite/ChangeLog:
* gcc.dg/pr125261.c: New test.

gcc/c/c-parser.cc
gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/pr125261.c [new file with mode: 0644]

index 9ddb449d71069627d366fbb43d3b5f5c669ddd9c..daf57061ee9a421b7e30ff789e007dec321fa113 100644 (file)
@@ -11469,14 +11469,14 @@ c_parser_generic_selection (c_parser *parser)
       bool match = assoc.type == NULL_TREE
                   || comptypes (assoc.type, selector_type);
 
-      if (!match)
+      if (!match || matched_assoc.type != NULL_TREE)
        c_inhibit_evaluation_warnings++;
       in_generic++;
 
       assoc.expression = c_parser_expr_no_commas (parser, NULL);
 
-      if (!match)
-         c_inhibit_evaluation_warnings--;
+      if (!match || matched_assoc.type != NULL_TREE)
+       c_inhibit_evaluation_warnings--;
       in_generic--;
       if (!match)
        pop_maybe_used (!flag_isoc23);
index 6195d1795432947ac6ceb7f0765cb8d40ebf9cf5..a03ec920e19a325893667b402896414d433fb23c 100644 (file)
@@ -7417,7 +7417,7 @@ build_c_cast (location_t loc, tree type, tree expr)
          && pedwarn (loc, OPT_Wpedantic,
                      "ISO C forbids casting nonscalar to the same type"))
              ;
-      else if (warn_useless_cast)
+      else if (warn_useless_cast && c_inhibit_evaluation_warnings == 0)
        warning_at (loc, OPT_Wuseless_cast,
                    "useless cast to type %qT", type);
 
diff --git a/gcc/testsuite/gcc.dg/pr125261.c b/gcc/testsuite/gcc.dg/pr125261.c
new file mode 100644 (file)
index 0000000..1bac78a
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-Wuseless-cast" } */
+
+
+static int i = _Generic(0., int: (int)0, default: 0);
+static int j = _Generic(0, int: 0, default: (int)0, float: 0);
+static int k = _Generic(0, default: 0, int: (int)0);   /* { dg-warning "useless" } */
+
+/* If the active assocation cames later than the default, we do
+   not know that it is unused.  */
+// static int l = _Generic(0, default: (int)0, int: 0);
+