Skip to content

-Wunused-lambda-capture: false positive on if constexpr #44743

@gribozavr

Description

@gribozavr
Bugzilla Link 45398
Version trunk
OS All
CC @CaseyCarter,@dwblaikie,@egorpugin,@zygoloid

Extended Description

-Wunused-lambda-capture produces a false positive on lambdas that use captured variables within inactive branches of if constexpr.

$ ./bin/clang++ --version
clang version 11.0.0 (git@github.com:llvm/llvm-project.git cdce2fe)
...
$ cat /tmp/unused-lambda-capture-false-positive.cc
template
struct A {};
struct B {
template <typename T, bool flag>
void Top(T t, int n) {
auto lambda = [t, n, this](auto arg) {
if constexpr (flag) {
UndefinedFunction(t, n);
} else {
TopImpl<A>();
}
};
}
template
void TopImpl() {}
};
void Test(B* b) {
b->Top<int, false>(0, 0);
}
$ ./bin/clang++ -c -std=gnu++17 -Wunused-lambda-capture /tmp/unused-lambda-capture-false-positive.cc
/tmp/unused-lambda-capture-false-positive.cc:6:20: warning: lambda capture 't' is not used [-Wunused-lambda-capture]
auto lambda = [t, n, this](auto arg) {
^
/tmp/unused-lambda-capture-false-positive.cc:18:6: note: in instantiation of function template specialization 'B::Top<int, false>' requested here
b->Top<int, false>(0, 0);
^
/tmp/unused-lambda-capture-false-positive.cc:6:23: warning: lambda capture 'n' is not used [-Wunused-lambda-capture]
auto lambda = [t, n, this](auto arg) {
^
/tmp/unused-lambda-capture-false-positive.cc:6:26: warning: lambda capture 'this' is not used [-Wunused-lambda-capture]
auto lambda = [t, n, this](auto arg) {
^
3 warnings generated.

ANALYSIS

The false positive on this might be a dup of #36228 , however false positives on t and n seem diffirent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++17clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerduplicateResolved as duplicatefalse-positiveWarning fires when it should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions