[clang-tools-extra] 3fd4213 - [clang-tidy] Do not trigger cppcoreguidelines-avoid-const-or-ref-data-members on lambda captures

Carlos Galvez via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 19 01:27:00 PDT 2022


Author: Carlos Galvez
Date: 2022-08-19T08:26:34Z
New Revision: 3fd4213059a4ea9453809aeccd1bfc7d115d24b2

URL: https://github.com/llvm/llvm-project/commit/3fd4213059a4ea9453809aeccd1bfc7d115d24b2
DIFF: https://github.com/llvm/llvm-project/commit/3fd4213059a4ea9453809aeccd1bfc7d115d24b2.diff

LOG: [clang-tidy] Do not trigger cppcoreguidelines-avoid-const-or-ref-data-members on lambda captures

Lambdas are implemented as regular classes internally,
and the captured variables end up as members there.
Do not diagnose those - the check should cover only
regular classes and structs.

Differential Revision: https://reviews.llvm.org/D131780

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
index 5f340736c8dde..aa530ff9e098c 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
@@ -15,12 +15,23 @@ using namespace clang::ast_matchers;
 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
index 01aed9dfdc1e2..becc3ee8ba9d8 100644
--- 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,41 @@ TemplatedConst<const int> t2{123};
 TemplatedConstRef<const int &> t3{123};
 TemplatedRefRef<int &&> t4{123};
 TemplatedRef<int &> 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]{};
+
+  auto iv = [=]{
+    auto c1 = x1;
+    auto c2 = x2;
+    auto c3 = x3;
+    auto c4 = x4;
+    auto c5 = x5;
+  };
+
+  auto ir = [&]{
+    auto c1 = x1;
+    auto c2 = x2;
+    auto c3 = x3;
+    auto c4 = x4;
+    auto c5 = x5;
+  };
+}


        


More information about the cfe-commits mailing list