With -Wall, in every c++ language level on virtually every version of g++ as far back as 4.9, g++ warns ("variable 'THIS_IS_USED' set but not used") on the following complete code: template <typename IterT, typename FuncT> void for_each(IterT b, IterT e, FuncT f) { while (b != e) { f(*b++); } } volatile int sink; void xxx(int x) { sink = x; } void foo() { static constexpr auto THIS_IS_USED = 1; int arr[10]{1,2,3,4,5,6,7,8,9,10}; for_each(arr, arr+10, [](auto v) { xxx(THIS_IS_USED + v); }); } <source>: In function 'void foo()': <source>:15:27: warning: variable 'THIS_IS_USED' set but not used [-Wunused-but-set-variable] 15 | static constexpr auto THIS_IS_USED = 1; | ^~~~~~~~~~~~ Compiler returned: 0 https://godbolt.org/z/b1s5Yqb3r This appears to be related to the "auto" parameter in the lambda. Changing it to 'int' makes the problem go away.
*** Bug 105743 has been marked as a duplicate of this bug. ***
Simplified reproducer from Bug 105743: See https://godbolt.org/z/xq16xac15. ```C++ void f(auto x) { x(0); } void g() { static constexpr auto h = [](...) { }; f([](auto x) { h(x); }); } ``` ``` <source>: In function 'void g()': <source>:3:25: warning: variable 'h' set but not used [-Wunused-but-set-variable] 3 | static constexpr auto h = [](...) { }; | ^ ``` Arguably, `h(x)` could perform ADL on `x`. But perhaps it'd be better to stay silent, like the other compilers.
Dup of bug 88804. The problem is with templated lambdas (auto param is really a templated lambda). *** This bug has been marked as a duplicate of bug 88804 ***