[PATCH] D73762: [clang] New warning for for-loops where the iteration does not match the loop condition

Eugene Zelenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 19:09:31 PST 2020


Eugene.Zelenko added a comment.

It's good question where this check belongs. There are two loop related checks in CLang-tidy: bugprone-infinite-loop and bugprone-too-small-loop-variable.



================
Comment at: clang/lib/Sema/SemaStmt.cpp:1754
+    if (!Cond->isRelationalOp()) return;
+    const auto Opcode = Cond->getOpcode();
+
----------------
Please don't use auto unless type is spelled in same statement or iterator.


================
Comment at: clang/lib/Sema/SemaStmt.cpp:1760
+    const VarDecl *RightOperand = nullptr;
+    if (const DeclRefExpr *DRE =
+            dyn_cast<DeclRefExpr>(Cond->getLHS()->IgnoreParenImpCasts())) {
----------------
Please use const auto *, because type is spelled in same statement.


================
Comment at: clang/lib/Sema/SemaStmt.cpp:1764
+    }
+    if (const DeclRefExpr *DRE =
+            dyn_cast<DeclRefExpr>(Cond->getRHS()->IgnoreParenImpCasts())) {
----------------
Please use const auto *, because type is spelled in same statement.


================
Comment at: clang/lib/Sema/SemaStmt.cpp:1780
+    // Check that the loop step is an increment/decrement operation.
+    const UnaryOperator* Increment = dyn_cast<UnaryOperator>(Third);
+    if (!Increment) return;
----------------
Please use const auto *, because type is spelled in same statement.

Please run clang-format over patch.


================
Comment at: clang/lib/Sema/SemaStmt.cpp:1784
+    const VarDecl *IncrementOperand = nullptr;
+    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
+            Increment->getSubExpr()->IgnoreParenImpCasts())) {
----------------
Please use const auto *, because type is spelled in same statement.


================
Comment at: clang/lib/Sema/SemaStmt.cpp:1813
+    // One fix-it to reverse the condition operator.
+    const auto ReverseOpcode = BinaryOperator::reverseComparisonOp(Opcode);
+    const StringRef OperatorFixIt =
----------------
Please don't use auto unless type is spelled in same statement or iterator.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D73762





More information about the cfe-commits mailing list