diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp @@ -15,12 +15,23 @@ namespace clang { namespace tidy { namespace cppcoreguidelines { +namespace { + +AST_MATCHER(FieldDecl, isMemberOfLambda) { + return Node.getParent()->isLambda(); +} + +} // namespace void AvoidConstOrRefDataMembersCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher( - fieldDecl(hasType(hasCanonicalType(referenceType()))).bind("ref"), this); - Finder->addMatcher( - fieldDecl(hasType(qualType(isConstQualified()))).bind("const"), this); + Finder->addMatcher(fieldDecl(unless(isMemberOfLambda()), + hasType(hasCanonicalType(referenceType()))) + .bind("ref"), + this); + Finder->addMatcher(fieldDecl(unless(isMemberOfLambda()), + hasType(qualType(isConstQualified()))) + .bind("const"), + this); } void AvoidConstOrRefDataMembersCheck::check( diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp @@ -167,3 +167,25 @@ TemplatedConstRef t3{123}; TemplatedRefRef t4{123}; TemplatedRef t5{t1.t}; + +// Lambdas capturing const or ref members should not trigger warnings +void lambdas() +{ + int x1{123}; + const int x2{123}; + const int& x3{123}; + int&& x4{123}; + int& x5{x1}; + + auto v1 = [x1]{}; + auto v2 = [x2]{}; + auto v3 = [x3]{}; + auto v4 = [x4]{}; + auto v5 = [x5]{}; + + auto r1 = [&x1]{}; + auto r2 = [&x2]{}; + auto r3 = [&x3]{}; + auto r4 = [&x4]{}; + auto r5 = [&x5]{}; +}