[gcc r16-7094] c++: Fix false positive with -Wunused [PR114450]
Jason Merrill
jason@gcc.gnu.org
Wed Jan 28 02:33:10 GMT 2026
https://gcc.gnu.org/g:04ec3d4712519e32f85c034ff4237fcd5f9fbe39
commit r16-7094-g04ec3d4712519e32f85c034ff4237fcd5f9fbe39
Author: Lucas Chollet <lucas.chollet@free.fr>
Date: Mon Jan 26 18:16:53 2026 +0800
c++: Fix false positive with -Wunused [PR114450]
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>
Diff:
---
gcc/cp/lambda.cc | 2 +-
gcc/cp/semantics.cc | 4 +++-
gcc/testsuite/g++.dg/warn/Wunused-var-42.C | 33 ++++++++++++++++++++++++++++++
3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc
index 402d5f7110a0..e1ff304ffe89 100644
--- a/gcc/cp/lambda.cc
+++ b/gcc/cp/lambda.cc
@@ -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)));
}
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 0a15b32a16d2..35bc48e49dc0 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -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
index 000000000000..35c438bbcc77
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-var-42.C
@@ -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; };
+}
More information about the Gcc-cvs
mailing list