carlosgalvezp created this revision.
Herald added subscribers: shchenz, kbarton, xazax.hun, nemanjai.
Herald added a project: All.
carlosgalvezp requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
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.


Repository:
  rG LLVM Github Monorepo

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,22 @@
 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};
+  auto y1 = [x1]{};
+
+  const int x2{123};
+  auto y2 = [x2]{};
+
+  const int& x3{123};
+  auto y3 = [x3]{};
+
+  int&& x4{123};
+  auto y4 = [x4]{};
+
+  int& x5{x1};
+  auto y5 = [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(


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,22 @@
 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};
+  auto y1 = [x1]{};
+
+  const int x2{123};
+  auto y2 = [x2]{};
+
+  const int& x3{123};
+  auto y3 = [x3]{};
+
+  int&& x4{123};
+  auto y4 = [x4]{};
+
+  int& x5{x1};
+  auto y5 = [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(
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to