Bug 96400 - False positive on Wunused-but-set-variable for static constexpr var used in lambda
Summary: False positive on Wunused-but-set-variable for static constexpr var used in l...
Status: RESOLVED DUPLICATE of bug 88804
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 105743 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-07-31 09:56 UTC by Björn Fahller
Modified: 2022-09-23 16:27 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Björn Fahller 2020-07-31 09:56:21 UTC
Similar to Bug 96311, but not quite the same.

This code is flags "is_zero" as being unused but set:

template <typename Pred, typename Val>
bool test(Pred p, Val v)
{
    return p(v);
}

bool func(int* p)
{
    static constexpr auto is_zero = [](auto v) { return v == 0;};
    return test([](auto v){return is_zero(*v);}, p);
}

Output:
<source>: In function 'bool func(int*)':

<source>:9:27: warning: variable 'is_zero' set but not used [-Wunused-but-set-variable]

    9 |     static constexpr auto is_zero = [](auto v) { return v == 0;};

      |                           ^~~~~~~


"is_zero" can correctly be used in the lambda without being captured, since it's static. The warning only appears when using the extra indirection "test()". Calling the anonymous lambda directly, without the indirection "test()", does not display the warning.

Possibly "constexpr" is a red herring above.

Godbolt link:
https://godbolt.org/z/x4zb3s
Comment 1 Andrew Pinski 2021-07-27 04:49:11 UTC
Here is the most reduced C++20 testcase which produce the warning:
void test(auto){}
void func()
{
    static auto is_zero = [](){ };
    test([](auto v){return is_zero(*v);});
}

Here is a C++17 testcase:
template<typename t> void test(t){}

void func()
{
    static auto is_zero = [](){ };
    test([](auto v){return is_zero(*v);});
}
Comment 2 Andrew Pinski 2022-09-23 16:16:15 UTC
*** Bug 105743 has been marked as a duplicate of this bug. ***
Comment 3 Andrew Pinski 2022-09-23 16:27:40 UTC
Changing "[](auto v)" to "[](int *v)" removes the warning. So this is a dup of bug 88804.

*** This bug has been marked as a duplicate of bug 88804 ***