[PATCH] D147419: [clang-tidy] ignore NRVO const variables in performance-no-automatic-move.

Logan Gnanapragasam via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 2 14:45:59 PDT 2023


gnanabit updated this revision to Diff 510374.
gnanabit added a comment.

Fix typo in `CHECK-MESSAGES` (should warn about `obj2`, not `obj1`).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147419

Files:
  clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp
@@ -33,10 +33,15 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents automatic move [performance-no-automatic-move]
 }
 
-Obj PositiveSelfConstValue() {
-  const Obj obj = Make<Obj>();
-  return obj;
-  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents automatic move [performance-no-automatic-move]
+Obj PositiveCantNrvo(bool b) {
+  const Obj obj1;
+  const Obj obj2;
+  if (b) {
+    return obj1;
+    // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: constness of 'obj1' prevents automatic move [performance-no-automatic-move]
+  }
+  return obj2;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj2' prevents automatic move [performance-no-automatic-move]
 }
 
 // FIXME: Ideally we would warn here too.
@@ -54,18 +59,32 @@
 // Negatives.
 
 StatusOr<Obj> Temporary() {
-  return Make<const Obj>();
+  return Make<Obj>();
 }
 
 StatusOr<Obj> ConstTemporary() {
   return Make<const Obj>();
 }
 
-StatusOr<Obj> Nrvo() {
+StatusOr<Obj> ConvertingMoveConstructor() {
   Obj obj = Make<Obj>();
   return obj;
 }
 
+Obj ConstNrvo() {
+  const Obj obj = Make<Obj>();
+  return obj;
+}
+
+Obj NotNrvo(bool b) {
+  Obj obj1;
+  Obj obj2;
+  if (b) {
+    return obj1;
+  }
+  return obj2;
+}
+
 StatusOr<Obj> Ref() {
   Obj &obj = Make<Obj &>();
   return obj;
Index: clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp
+++ clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp
@@ -16,6 +16,12 @@
 
 namespace clang::tidy::performance {
 
+namespace {
+
+AST_MATCHER(VarDecl, isNRVOVariable) { return Node.isNRVOVariable(); }
+
+} // namespace
+
 NoAutomaticMoveCheck::NoAutomaticMoveCheck(StringRef Name,
                                            ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
@@ -23,8 +29,9 @@
           utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
 
 void NoAutomaticMoveCheck::registerMatchers(MatchFinder *Finder) {
-  const auto ConstLocalVariable =
+  const auto NonNrvoConstLocalVariable =
       varDecl(hasLocalStorage(), unless(hasType(lValueReferenceType())),
+              unless(isNRVOVariable()),
               hasType(qualType(
                   isConstQualified(),
                   hasCanonicalType(matchers::isExpensiveToCopy()),
@@ -48,7 +55,7 @@
                        cxxConstructExpr(
                            hasDeclaration(LValueRefCtor),
                            hasArgument(0, ignoringParenImpCasts(declRefExpr(
-                                              to(ConstLocalVariable)))))
+                                              to(NonNrvoConstLocalVariable)))))
                            .bind("ctor_call")))))),
       this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147419.510374.patch
Type: text/x-patch
Size: 3172 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230402/b728a288/attachment-0001.bin>


More information about the cfe-commits mailing list