[clang-tidy] Fix bug in bugprone-use-after-move check
Summary:
The bugprone-use-after-move check exhibits false positives for certain uses of
the C++17 if/switch init statements. These false positives are caused by a bug
in the ExprSequence calculations.
This revision adds tests for the false positives and fixes the corresponding
sequence calculation.
Reviewers: gribozavr
Subscribers: xazax.hun, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67292
llvm-svn: 371396
diff --git a/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp b/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
index 0a1558e..6ed595e 100644
--- 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<IfStmt>(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<SwitchStmt>(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<WhileStmt>(Parent)) {