[PATCH] D96142: [clang-tidy] Simplify too-small loop variable check

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 5 06:53:55 PST 2021


steveire created this revision.
steveire added reviewers: aaron.ballman, njames93.
Herald added subscribers: nullptr.cpp, xazax.hun.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Don't issue a warning from within a template declaration which is most
likely not actionable.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96142

Files:
  clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.h
  clang-tools-extra/test/clang-tidy/checkers/bugprone-too-small-loop-variable.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-too-small-loop-variable.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-too-small-loop-variable.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-too-small-loop-variable.cpp
@@ -65,7 +65,6 @@
 template <class T>
 void doSomething() {
   for (T i = 0; i < size(); ++i) {
-    // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: loop variable has narrower type 'short' than iteration's upper bound 'long' [bugprone-too-small-loop-variable]
   }
 }
 
Index: clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.h
@@ -34,6 +34,9 @@
   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 
 private:
   const unsigned MagnitudeBitsUpperLimit;
Index: clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -52,9 +52,7 @@
 ///
 void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
   StatementMatcher LoopVarMatcher =
-      expr(
-          ignoringParenImpCasts(declRefExpr(to(varDecl(hasType(isInteger()))))))
-          .bind(LoopVarName);
+      declRefExpr(to(varDecl(hasType(isInteger())))).bind(LoopVarName);
 
   // We need to catch only those comparisons which contain any integer cast.
   StatementMatcher LoopVarConversionMatcher = traverse(
@@ -64,17 +62,15 @@
 
   // We are interested in only those cases when the loop bound is a variable
   // value (not const, enum, etc.).
-  StatementMatcher LoopBoundMatcher =
-      expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
-                                       unless(integerLiteral()),
-                                       unless(hasType(isConstQualified())),
-                                       unless(hasType(enumType())))))
+  auto LoopBoundMatcher =
+      expr(allOf(hasType(isInteger()), unless(integerLiteral()),
+                 unless(hasType(isConstQualified())),
+                 unless(hasType(enumType()))))
           .bind(LoopUpperBoundName);
 
   // We use the loop increment expression only to make sure we found the right
   // loop variable.
-  StatementMatcher IncrementMatcher =
-      expr(ignoringParenImpCasts(hasType(isInteger()))).bind(LoopIncrementName);
+  auto IncrementMatcher = expr(hasType(isInteger())).bind(LoopIncrementName);
 
   Finder->addMatcher(
       forStmt(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96142.321753.patch
Type: text/x-patch
Size: 3067 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210205/01100ef9/attachment.bin>


More information about the cfe-commits mailing list