Index: clang-tidy/readability/IdentifierNamingCheck.cpp =================================================================== --- clang-tidy/readability/IdentifierNamingCheck.cpp +++ clang-tidy/readability/IdentifierNamingCheck.cpp @@ -396,7 +396,7 @@ if (isa(D) && NamingStyles[SK_ObjcIvar]) return SK_ObjcIvar; - + if (isa(D) && NamingStyles[SK_Typedef]) return SK_Typedef; @@ -506,7 +506,7 @@ return SK_ParameterPack; if (!Type.isNull() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_PointerParameter]) - return SK_PointerParameter; + return SK_PointerParameter; if (NamingStyles[SK_Parameter]) return SK_Parameter; @@ -557,7 +557,7 @@ if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable]) return SK_StaticVariable; - + if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_LocalPointer]) return SK_LocalPointer; @@ -694,6 +694,25 @@ Range, SourceMgr); } +/// Determine if the DeclRef is direct descentent of a LambdaExpr +/// ignoring ImplicitCasts. +static bool isLambda(const DeclRefExpr *DeclRef, ASTContext *Context) { + const ASTContext::DynTypedNodeList &Parents = Context->getParents(*DeclRef); + if (!Parents.empty()) { + const Stmt *ST = Parents[0].get(); + // Ignore ImplicitCastExpr if we find one. + if (ST && isa(ST)) { + const ASTContext::DynTypedNodeList &CastParents = + Context->getParents(*ST); + if (!CastParents.empty()) + ST = CastParents[0].get(); + } + if (ST && isa(ST)) + return true; + } + return false; +} + void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *Decl = Result.Nodes.getNodeAs("classRef")) { @@ -788,9 +807,19 @@ if (const auto *DeclRef = Result.Nodes.getNodeAs("declRef")) { SourceRange Range = DeclRef->getNameInfo().getSourceRange(); - addUsage(NamingCheckFailures, DeclRef->getDecl(), Range, - Result.SourceManager); - return; + bool LambdaDeclRef = isLambda(DeclRef, Result.Context); + if (LambdaDeclRef) { + std::string captureText = + Lexer::getSourceText(CharSourceRange::getTokenRange(Range), + *Result.SourceManager, getLangOpts()); + if (!(captureText == "=" || captureText == "&")) + LambdaDeclRef = false; + } + if (!LambdaDeclRef) { + addUsage(NamingCheckFailures, DeclRef->getDecl(), Range, + Result.SourceManager); + return; + } } if (const auto *Decl = Result.Nodes.getNodeAs("decl")) { Index: test/clang-tidy/readability-identifier-naming.cpp =================================================================== --- test/clang-tidy/readability-identifier-naming.cpp +++ test/clang-tidy/readability-identifier-naming.cpp @@ -501,3 +501,159 @@ // CHECK-FIXES: {{^}} int * const lc_PointerB = nullptr;{{$}} } + +bool LambdaCaptureTest() { + bool Columns=false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns=false; + auto ptr = [&]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest1() { + bool Columns=false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns=false; + auto ptr = [=]{ + return Columns; + // CHECK-FIXES: {{^}} return columns; + }(); + + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest2() { + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [&Columns]{ +// CHECK-FIXES: {{^}} auto ptr = [&columns]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest3() { + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [Columns]{ +// CHECK-FIXES: {{^}} auto ptr = [columns]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest4() { + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [&]{ + if (true) + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest5() { + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [](bool Columns) { +// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: invalid case style for parameter 'Columns' +// CHECK-FIXES: {{^}} auto ptr = [](bool a_columns) { + return Columns; +// CHECK-FIXES: {{^}} return a_columns; + }; + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest6() { + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [Columns]() ->bool { +// CHECK-FIXES: {{^}} auto ptr = [columns]() ->bool { + [=]() -> bool { + return Columns; +// CHECK-FIXES: {{^}} return columns; + }; + return Columns; +// CHECK-FIXES: {{^}} return columns; + }; + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest7() { + int a; + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [&Columns, a]{ +// CHECK-FIXES: {{^}} auto ptr = [&columns, a]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest8() { + int a; + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [a, Columns]{ +// CHECK-FIXES: {{^}} auto ptr = [a, columns]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest9() { + int Rows; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for local variable 'Rows' +// CHECK-FIXES: {{^}} int rows; + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [Rows, Columns]{ +// CHECK-FIXES: {{^}} auto ptr = [rows, columns]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +} + +bool LambdaCaptureTest10() { + int Rows; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for local variable 'Rows' +// CHECK-FIXES: {{^}} int rows; + bool Columns = false; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'Columns' +// CHECK-FIXES: {{^}} bool columns = false; + auto ptr = [&Rows, &Columns]{ +// CHECK-FIXES: {{^}} auto ptr = [&rows, &columns]{ + return Columns; +// CHECK-FIXES: {{^}} return columns; + }(); + return Columns; +// CHECK-FIXES: {{^}} return columns; +}