]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix false positive with -Wunused [PR114450]
authorLucas Chollet <lucas.chollet@free.fr>
Mon, 26 Jan 2026 10:16:53 +0000 (18:16 +0800)
committerJason Merrill <jason@redhat.com>
Wed, 28 Jan 2026 02:31:00 +0000 (10:31 +0800)
The patch fixes a bug in the detection of the usage of static variables
inside generic lambdas.  The comment in finish_id_expression_1 already
mentions this case, but the code didn't actually handle it.

PR c++/114450

gcc/cp/ChangeLog:

* lambda.cc (generic_lambda_fn_p): Handle null argument.
* semantics.cc (finish_id_expression_1): Check for generic lambda.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wunused-var-42.C: New test.

Co-authored-by: Jason Merrill <jason@redhat.com>
gcc/cp/lambda.cc
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/warn/Wunused-var-42.C [new file with mode: 0644]

index 402d5f7110a05d17ffd598cfe41fafbd6b71b0b8..e1ff304ffe892b64ae17ccc572b006c2542a4196 100644 (file)
@@ -1153,7 +1153,7 @@ prepare_op_call (tree fn, int nargs)
 bool
 generic_lambda_fn_p (tree callop)
 {
-  return (LAMBDA_FUNCTION_P (callop)
+  return (callop && LAMBDA_FUNCTION_P (callop)
          && DECL_TEMPLATE_INFO (callop)
          && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
 }
index 0a15b32a16d2d3ae4d41b8ef55005d954cf1f3bb..35bc48e49dc0240907651bc8a6dbf784aa94b2a6 100644 (file)
@@ -4814,7 +4814,9 @@ finish_id_expression_1 (tree id_expression,
       /* A use in unevaluated operand might not be instantiated appropriately
         if tsubst_copy builds a dummy parm, or if we never instantiate a
         generic lambda, so mark it now.  */
-      if (processing_template_decl && cp_unevaluated_operand)
+      if (processing_template_decl
+         && (cp_unevaluated_operand
+             || generic_lambda_fn_p (current_function_decl)))
        mark_type_use (decl);
 
       /* Disallow uses of local variables from containing functions, except
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-42.C b/gcc/testsuite/g++.dg/warn/Wunused-var-42.C
new file mode 100644 (file)
index 0000000..35c438b
--- /dev/null
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wunused" }
+
+template <typename F>
+void f (F &&d)
+{
+  static unsigned context;
+  d(context);
+}
+
+void g ()
+{
+  static int b;
+  f([](auto c) { return c <= b; });
+}
+
+void h ()
+{
+  static int b = 0; // { dg-warning "unused variable" }
+  f([](auto c) { return c <= 0; });
+}
+
+void i ()
+{
+  static int b = 0;
+  [](auto c) { return c <= b; };
+}
+
+void j ()
+{
+  static int b = 0; // { dg-warning "unused variable" }
+  [](auto c) { return c <= 0; };
+}