diff --git a/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp b/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp --- a/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp +++ b/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp @@ -145,17 +145,24 @@ return ForRange->getBody(); } else if (const auto *TheIfStmt = dyn_cast(Parent)) { // If statement: - // - Sequence init statement before variable declaration. + // - Sequence init statement before variable declaration, if present; + // before condition evaluation, otherwise. // - Sequence variable declaration (along with the expression used to // initialize it) before the evaluation of the condition. - if (S == TheIfStmt->getInit()) - return TheIfStmt->getConditionVariableDeclStmt(); + if (S == TheIfStmt->getInit()) { + if (TheIfStmt->getConditionVariableDeclStmt() != nullptr) + return TheIfStmt->getConditionVariableDeclStmt(); + return TheIfStmt->getCond(); + } if (S == TheIfStmt->getConditionVariableDeclStmt()) return TheIfStmt->getCond(); } else if (const auto *TheSwitchStmt = dyn_cast(Parent)) { // Ditto for switch statements. - if (S == TheSwitchStmt->getInit()) - return TheSwitchStmt->getConditionVariableDeclStmt(); + if (S == TheSwitchStmt->getInit()) { + if (TheSwitchStmt->getConditionVariableDeclStmt() != nullptr) + return TheSwitchStmt->getConditionVariableDeclStmt(); + return TheSwitchStmt->getCond(); + } if (S == TheSwitchStmt->getConditionVariableDeclStmt()) return TheSwitchStmt->getCond(); } else if (const auto *TheWhileStmt = dyn_cast(Parent)) { diff --git a/clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp b/clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp --- a/clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp +++ b/clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp @@ -1190,6 +1190,12 @@ std::move(a2); } } + for (int i = 0; i < 10; ++i) { + A a1; + if (A a2 = std::move(a1); a2) { + std::move(a2); + } + } for (int i = 0; i < 10; ++i) { A a1; if (A a2 = std::move(a1); A a3 = std::move(a2)) { @@ -1199,6 +1205,13 @@ while (A a = A()) { std::move(a); } + for (int i = 0; i < 10; ++i) { + A a1; + switch (A a2 = std::move(a1); a2) { + case true: + std::move(a2); + } + } for (int i = 0; i < 10; ++i) { A a1; switch (A a2 = a1; A a3 = std::move(a2)) {