https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122642
Bug ID: 122642
Summary: Incorrect -Wunused-but-set-variable for static
variables captured in lambda
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: lucas.chollet at free dot fr
Target Milestone: ---
In the following code, GCC reports false positives of the
"unused-but-set-variable" warning.
void f1() {
static int b = 0;
[](auto c) { return c <= b; };
}
void g(auto&& d){
static int context = 3;
d(context);
}
void f2() {
static int b = 0;
g([](auto c) { return c <= b; });
}
<source>:2:14: error: variable 'b' set but not used
[-Werror=unused-but-set-variable=]
2 | static int b = 0;
| ^
<source>: In function 'void f2()':
<source>:12:14: error: variable 'b' set but not used
[-Werror=unused-but-set-variable=]
12 | static int b = 0;
| ^
f1 is the minimal reproducer, but I also included f2 which is closer to what
you might find in real codebases.
This seems to affect every version since at least GCC 10.1.
https://godbolt.org/z/ve1nvfYEe