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

Carlos Galvez via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 17 23:37:47 PDT 2022


carlosgalvezp updated this revision to Diff 453535.
carlosgalvezp added a comment.

Update implicit lambda captures to use the captured variables
inside the lambdas.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131780/new/

https://reviews.llvm.org/D131780

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


Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
@@ -167,3 +167,41 @@
 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 rv = [&]{
+    auto c1 = x1;
+    auto c2 = x2;
+    auto c3 = x3;
+    auto c4 = x4;
+    auto c5 = x5;
+  };
+}
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
+++ 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(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131780.453535.patch
Type: text/x-patch
Size: 2436 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220818/05861954/attachment-0001.bin>


More information about the cfe-commits mailing list