Bug 105571 - Spurious "set but not used" on static constexpr local, used in lambda
Summary: Spurious "set but not used" on static constexpr local, used in lambda
Status: RESOLVED DUPLICATE of bug 88804
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2022-05-11 19:06 UTC by Chris Uzdavinis
Modified: 2022-09-23 16:25 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Uzdavinis 2022-05-11 19:06:34 UTC
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.
Comment 1 Johel Ernesto Guerrero Peña 2022-05-26 18:38:16 UTC
*** Bug 105743 has been marked as a duplicate of this bug. ***
Comment 2 Johel Ernesto Guerrero Peña 2022-05-26 18:39:07 UTC
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.
Comment 3 Andrew Pinski 2022-09-23 16:25:54 UTC
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 ***