diff --git a/build/clang-plugin/clang-plugin.cpp b/build/clang-plugin/clang-plugin.cpp index 43e8b8def4c..9f987d77d10 100644 --- a/build/clang-plugin/clang-plugin.cpp +++ b/build/clang-plugin/clang-plugin.cpp @@ -704,14 +704,8 @@ DiagnosticsMatcher::DiagnosticsMatcher() ).bind("node"), &noAddRefReleaseOnReturnChecker); - // Match declrefs with type "pointer to object of ref-counted type" inside a - // lambda, where the declaration they reference is not inside the lambda. - // This excludes arguments and local variables, leaving only captured - // variables. astMatcher.addMatcher(lambdaExpr( - hasDescendant(declRefExpr(hasType(pointerType(pointee(isRefCounted()))), - to(decl().bind("decl"))).bind("declref")), - unless(hasDescendant(decl(equalsBoundNode("decl")))) + hasDescendant(declRefExpr(hasType(pointerType(pointee(isRefCounted())))).bind("node")) ), &refCountedInsideLambdaChecker); @@ -923,14 +917,14 @@ void DiagnosticsMatcher::RefCountedInsideLambdaChecker::run( const MatchFinder::MatchResult &Result) { DiagnosticsEngine &Diag = Result.Context->getDiagnostics(); unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID( - DiagnosticIDs::Error, "Refcounted variable %0 of type %1 cannot be captured by a lambda"); + DiagnosticIDs::Error, "Refcounted variable %0 of type %1 cannot be used inside a lambda"); unsigned noteID = Diag.getDiagnosticIDs()->getCustomDiagID( DiagnosticIDs::Note, "Please consider using a smart pointer"); - const DeclRefExpr *declref = Result.Nodes.getNodeAs("declref"); + const DeclRefExpr *node = Result.Nodes.getNodeAs("node"); - Diag.Report(declref->getLocStart(), errorID) << declref->getFoundDecl() << - declref->getType()->getPointeeType(); - Diag.Report(declref->getLocStart(), noteID); + Diag.Report(node->getLocStart(), errorID) << node->getFoundDecl() << + node->getType()->getPointeeType(); + Diag.Report(node->getLocStart(), noteID); } void DiagnosticsMatcher::ExplicitOperatorBoolChecker::run( diff --git a/build/clang-plugin/tests/TestNoRefcountedInsideLambdas.cpp b/build/clang-plugin/tests/TestNoRefcountedInsideLambdas.cpp index 20486bbcae8..9cc92cf935c 100644 --- a/build/clang-plugin/tests/TestNoRefcountedInsideLambdas.cpp +++ b/build/clang-plugin/tests/TestNoRefcountedInsideLambdas.cpp @@ -19,76 +19,40 @@ void take(...); void foo() { R* ptr; SmartPtr sp; - take([&](R* argptr) { - R* localptr; - ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}} - argptr->method(); - localptr->method(); + take([&]() { + ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be used inside a lambda}} expected-note{{Please consider using a smart pointer}} }); - take([&](SmartPtr argsp) { - SmartPtr localsp; + take([&]() { sp->method(); - argsp->method(); - localsp->method(); }); - take([&](R* argptr) { - R* localptr; - take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}} - take(argptr); - take(localptr); + take([&]() { + take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be used inside a lambda}} expected-note{{Please consider using a smart pointer}} }); - take([&](SmartPtr argsp) { - SmartPtr localsp; + take([&]() { take(sp); - take(argsp); - take(localsp); }); - take([=](R* argptr) { - R* localptr; - ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}} - argptr->method(); - localptr->method(); + take([=]() { + ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be used inside a lambda}} expected-note{{Please consider using a smart pointer}} }); - take([=](SmartPtr argsp) { - SmartPtr localsp; + take([=]() { sp->method(); - argsp->method(); - localsp->method(); }); - take([=](R* argptr) { - R* localptr; - take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}} - take(argptr); - take(localptr); + take([=]() { + take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be used inside a lambda}} expected-note{{Please consider using a smart pointer}} }); - take([=](SmartPtr argsp) { - SmartPtr localsp; + take([=]() { take(sp); - take(argsp); - take(localsp); }); - take([ptr](R* argptr) { - R* localptr; - ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}} - argptr->method(); - localptr->method(); + take([ptr]() { + ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be used inside a lambda}} expected-note{{Please consider using a smart pointer}} }); - take([sp](SmartPtr argsp) { - SmartPtr localsp; + take([sp]() { sp->method(); - argsp->method(); - localsp->method(); }); - take([ptr](R* argptr) { - R* localptr; - take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}} - take(argptr); - take(localptr); + take([ptr]() { + take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be used inside a lambda}} expected-note{{Please consider using a smart pointer}} }); - take([sp](SmartPtr argsp) { - SmartPtr localsp; + take([sp]() { take(sp); - take(argsp); - take(localsp); }); }