[clang-tools-extra] r274019 - [clang-tidy] Do not match on lambdas.

Samuel Benzaquen via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 28 07:19:41 PDT 2016


Author: sbenza
Date: Tue Jun 28 09:19:41 2016
New Revision: 274019

URL: http://llvm.org/viewvc/llvm-project?rev=274019&view=rev
Log:
[clang-tidy] Do not match on lambdas.

We match on the generated FunctionDecl of the lambda and try to fix it.
This causes a crash.
The right behavior is to ignore lambdas, because they are a definition.

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp?rev=274019&r1=274018&r2=274019&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp Tue Jun 28 09:19:41 2016
@@ -32,10 +32,14 @@ SourceRange getTypeRange(const ParmVarDe
 void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
   const auto ConstParamDecl =
       parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
-  Finder->addMatcher(functionDecl(unless(isDefinition()),
-                                  has(typeLoc(forEach(ConstParamDecl))))
-                         .bind("func"),
-                     this);
+  Finder->addMatcher(
+      functionDecl(unless(isDefinition()),
+                   // Lambdas are always their own definition, but they
+                   // generate a non-definition FunctionDecl too. Ignore those.
+                   unless(cxxMethodDecl(ofClass(cxxRecordDecl(isLambda())))),
+                   has(typeLoc(forEach(ConstParamDecl))))
+          .bind("func"),
+      this);
 }
 
 // Re-lex the tokens to get precise location of last 'const'

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp?rev=274019&r1=274018&r2=274019&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp Tue Jun 28 09:19:41 2016
@@ -90,3 +90,7 @@ void ConstNotVisible(CONCAT(cons, t) int
 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
 // We warn, but we can't give a fix
 // CHECK-FIXES: void ConstNotVisible(CONCAT(cons, t) int i);
+
+// Regression test. We should not warn (or crash) on lambda expressions
+auto lambda_with_name = [](const int n) {};
+auto lambda_without_name = [](const int) {};




More information about the cfe-commits mailing list