[PATCH] D49918: [clang-tidy] Sequence declaration in while statement before the condition

Martin Böhme via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 27 07:35:18 PDT 2018


mboehme created this revision.
mboehme added a reviewer: ilya-biryukov.
Herald added subscribers: cfe-commits, xazax.hun.

Fixes https://bugs.llvm.org/show_bug.cgi?id=36516.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49918

Files:
  clang-tidy/utils/ExprSequence.cpp
  test/clang-tidy/bugprone-use-after-move.cpp


Index: test/clang-tidy/bugprone-use-after-move.cpp
===================================================================
--- test/clang-tidy/bugprone-use-after-move.cpp
+++ test/clang-tidy/bugprone-use-after-move.cpp
@@ -1132,15 +1132,18 @@
   }
 }
 
-// If a variable is declared in an if statement, the declaration of the variable
-// (which is treated like a reinitialization by the check) is sequenced before
-// the evaluation of the condition (which constitutes a use).
-void ifStmtSequencesDeclAndCondition() {
+// If a variable is declared in an if or while statement, the declaration of the
+// variable (which is treated like a reinitialization by the check) is sequenced
+// before the evaluation of the condition (which constitutes a use).
+void ifAndWhileStmtSequencesDeclAndCondition() {
   for (int i = 0; i < 10; ++i) {
     if (A a = A()) {
       std::move(a);
     }
   }
+  while (A a = A()) {
+    std::move(a);
+  }
 }
 
 namespace PR33020 {
Index: clang-tidy/utils/ExprSequence.cpp
===================================================================
--- clang-tidy/utils/ExprSequence.cpp
+++ clang-tidy/utils/ExprSequence.cpp
@@ -144,6 +144,12 @@
       // evaluation of the condition.
       if (S == TheIfStmt->getConditionVariableDeclStmt())
         return TheIfStmt->getCond();
+    } else if (const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
+      // While statement: If a variable is declared inside the condition, the
+      // expression used to initialize the variable is sequenced before the
+      // evaluation of the condition.
+      if (S == TheWhileStmt->getConditionVariableDeclStmt())
+        return TheWhileStmt->getCond();
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49918.157690.patch
Type: text/x-patch
Size: 1690 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180727/b1fda7c9/attachment.bin>


More information about the cfe-commits mailing list