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();
     }
   }
 


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();
     }
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to