This is an archive of the discontinued LLVM Phabricator instance.

Fix spurious Wunused-lambda-capture warning
ClosedPublic

Authored by kongyi on May 24 2017, 5:14 PM.

Details

Summary

Clang emits unused-lambda-capture warning for captures in generic lambdas even though they are actually used.

Fixes PR31815.

Diff Detail

Repository
rL LLVM

Event Timeline

kongyi created this revision.May 24 2017, 5:14 PM

Thanks for looking at this for me.

lib/Sema/SemaLambda.cpp
1524–1526 ↗(On Diff #100187)

Should generic lambdas and lambdas in a dependent context be treated the same?

Is a lambda inside a generic lambda in a dependent context?

kongyi added inline comments.May 26 2017, 4:45 PM
lib/Sema/SemaLambda.cpp
1524–1526 ↗(On Diff #100187)

Generic lambdas are essentially templated lambdas. For diagnosing unused captures, they can be treated as the same. However we are not generating diagnoses within dependent contexts right now (L1524), so this is not really related to this fix.

ahatanak added inline comments.
lib/Sema/SemaLambda.cpp
1524–1526 ↗(On Diff #100187)

It looks like clang stops warning about the unnecessary capture in the following code when this patch is applied:

void foo2(int);

void foo1() {
  const int c = 10;
  auto lambda1 = [c](auto a) { // clang used to issue warning "lambda capture 'c' is not required to be captured for this use"
    foo2(c);
  };
  lambda1(100);
}

Is it possible to suppress the warning if From is an init capture instead?

lib/Sema/SemaLambda.cpp
1525–1526 ↗(On Diff #100187)

This would be better using a ternary operator.
Add a comment too.

kongyi updated this revision to Diff 102099.Jun 9 2017, 4:49 PM
kongyi marked an inline comment as done.Jun 9 2017, 5:24 PM

I don't understand why init captures have this problem.
The variable is defined within the context of the lambda, but it's not within the context of the templated method.
Are you working around a bug somewhere else?

For normal captures, variables are safe to eliminate if they are non-ODR used or totally unused. However for init captures, non-ODR usage still depends on the capture; they are only safe to eliminate if totally unused.

This revision is now accepted and ready to land.Jun 13 2017, 3:55 AM
This revision was automatically updated to reflect the committed changes.