This is an archive of the discontinued LLVM Phabricator instance.

[PR36880] Avoid -Wunused-lambda-capture false positive for explicit this capture used in an unresolved member in a generic lambda
Needs ReviewPublic

Authored by arphaman on Mar 23 2018, 1:26 PM.

Details

Summary

Clang emits an incorrect -Wunused-lambda-capture for 'this' capture in a generic lambda in a template where 'this' is used implicitly in an unresolved member expression. This patch ensures that the use of 'this' is checked as a potential lambda capture use when the lambda is instantiated.

rdar://38803903

Diff Detail

Repository
rC Clang

Event Timeline

arphaman created this revision.Mar 23 2018, 1:26 PM
efriedma added inline comments.
lib/Sema/TreeTransform.h
11269

This doesn't make sense; in general, you can't tell whether an UnresolvedMemberExpr requires a "this" capture.

template<typename T> class C : T {
static int f(int);
int f(double);
public:
int g() {
  return []{ return f(T::x); }();
}
};

struct A { static int x; };
void x(C<A> c) { c.g(); }
arphaman added inline comments.Mar 23 2018, 4:07 PM
lib/Sema/TreeTransform.h
11269

Good point, we still emit a false positive warning for

template <class T> struct DummyTemplate {
  template <class T2> void methodTemplate(const T2 &) {}
  static void methodTemplate(float *) { }

  void ToTemplate(const int &param) {
    [this](const auto &p) { methodTemplate(p); }(param); // incorrect warning!
  }
};

void main() {
  int i = 0;
  DummyTemplate<bool> dt;
  dt.ToTemplate(i);
}

I will work on a better fix.