[PATCH] D50447: [clang-tidy] Add a whitelistClasses option in performance-for-range-copy check.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 9 07:02:25 PDT 2018


hokein updated this revision to Diff 159919.
hokein marked an inline comment as done.
hokein added a comment.

Adress review comment - ignore the case in the check.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D50447

Files:
  clang-tidy/performance/ForRangeCopyCheck.cpp
  test/clang-tidy/performance-for-range-copy.cpp


Index: test/clang-tidy/performance-for-range-copy.cpp
===================================================================
--- test/clang-tidy/performance-for-range-copy.cpp
+++ test/clang-tidy/performance-for-range-copy.cpp
@@ -260,3 +260,8 @@
     bool result = ConstOperatorInvokee != Mutable();
   }
 }
+
+void IgnoreLoopVariableNotUsedInLoopBody() {
+  for (auto _ : View<Iterator<S>>()) {
+  }
+}
Index: clang-tidy/performance/ForRangeCopyCheck.cpp
===================================================================
--- clang-tidy/performance/ForRangeCopyCheck.cpp
+++ clang-tidy/performance/ForRangeCopyCheck.cpp
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ForRangeCopyCheck.h"
+#include "../utils/DeclRefExprUtils.h"
 #include "../utils/ExprMutationAnalyzer.h"
 #include "../utils/FixItHintUtils.h"
 #include "../utils/TypeTraits.h"
@@ -79,15 +80,27 @@
       utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
   if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
     return false;
-  if (utils::ExprMutationAnalyzer(ForRange.getBody(), &Context)
-          .isMutated(&LoopVar))
-    return false;
-  diag(LoopVar.getLocation(),
-       "loop variable is copied but only used as const reference; consider "
-       "making it a const reference")
-      << utils::fixit::changeVarDeclToConst(LoopVar)
-      << utils::fixit::changeVarDeclToReference(LoopVar, Context);
-  return true;
+  // We omit the case where the loop variable is not used in the loop body. E.g.
+  //
+  // for (auto _ : benchmark_state) {
+  // }
+  //
+  // Because the fix (changing to `const auto &`) will introduce an unused
+  // compiler warning which can't be suppressed.
+  // Since this case is very rare, it is safe to ignore it.
+  if (!utils::ExprMutationAnalyzer(ForRange.getBody(), &Context)
+          .isMutated(&LoopVar) &&
+      !utils::decl_ref_expr::allDeclRefExprs(LoopVar, *ForRange.getBody(),
+                                             Context)
+           .empty()) {
+    diag(LoopVar.getLocation(),
+         "loop variable is copied but only used as const reference; consider "
+         "making it a const reference")
+        << utils::fixit::changeVarDeclToConst(LoopVar)
+        << utils::fixit::changeVarDeclToReference(LoopVar, Context);
+    return true;
+  }
+  return false;
 }
 
 } // namespace performance


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50447.159919.patch
Type: text/x-patch
Size: 2435 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180809/8068521c/attachment.bin>


More information about the cfe-commits mailing list