summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalcolm Parsons <malcolm.parsons@gmail.com>2016-11-04 16:32:14 +0000
committerMalcolm Parsons <malcolm.parsons@gmail.com>2016-11-04 16:32:14 +0000
commitc35be369621b98168fe9d30c518f13e04576288b (patch)
tree075ceb0d4f7fc127b7d64b2e872bc43505e74a70
parent85bc64c73464169942741f782fc4db680d6711de (diff)
downloadbcm5719-llvm-c35be369621b98168fe9d30c518f13e04576288b.tar.gz
bcm5719-llvm-c35be369621b98168fe9d30c518f13e04576288b.zip
[clang-tidy] Fixed readability-else-after-return for cascade statements
Summary: Fix generated by this check changed program semantics in the case where 'if' was a part (direct child) of other statement. Fixes PR30652. Patch by Paweł Żukowski. Reviewers: malcolm.parsons, alexfh, djasper Subscribers: mgehre, omtcyfz, cfe-commits Differential Revision: https://reviews.llvm.org/D26125 llvm-svn: 285999
-rw-r--r--clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp2
-rw-r--r--clang-tools-extra/test/clang-tidy/readability-else-after-return.cpp44
2 files changed, 37 insertions, 9 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
index 21e5224542d..6c676636a6a 100644
--- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -23,7 +23,7 @@ void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
stmt(anyOf(returnStmt().bind("return"), continueStmt().bind("continue"),
breakStmt().bind("break"), cxxThrowExpr().bind("throw")));
Finder->addMatcher(
- stmt(forEach(
+ compoundStmt(forEach(
ifStmt(hasThen(stmt(
anyOf(ControlFlowInterruptorMatcher,
compoundStmt(has(ControlFlowInterruptorMatcher))))),
diff --git a/clang-tools-extra/test/clang-tidy/readability-else-after-return.cpp b/clang-tools-extra/test/clang-tidy/readability-else-after-return.cpp
index 4e721413943..782c929b1c4 100644
--- a/clang-tools-extra/test/clang-tidy/readability-else-after-return.cpp
+++ b/clang-tools-extra/test/clang-tidy/readability-else-after-return.cpp
@@ -29,32 +29,60 @@ void f(int a) {
else if (a > 10)
return;
else // comment-2
- // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'else' after 'return'
- // CHECK-FIXES: {{^}} // comment-2
+ // CHECK-FIXES-NOT: {{^}} // comment-2
f(0);
+
+ if (a > 0)
+ if (a < 10)
+ return;
+ else // comment-3
+ // CHECK-FIXES-NOT: {{^}} // comment-3
+ f(0);
+ else
+ if (a > 10)
+ return;
+ else // comment-4
+ // CHECK-FIXES-NOT: {{^}} // comment-4
+ f(0);
+
+ if (a > 0) {
+ if (a < 10)
+ return;
+ else // comment-5
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
+ // CHECK-FIXES: {{^}} // comment-5
+ f(0);
+ } else {
+ if (a > 10)
+ return;
+ else // comment-6
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
+ // CHECK-FIXES: {{^}} // comment-6
+ f(0);
+ }
}
void foo() {
for (unsigned x = 0; x < 42; ++x) {
if (x) {
continue;
- } else { // comment-3
+ } else { // comment-7
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'continue'
- // CHECK-FIXES: {{^}} } // comment-3
+ // CHECK-FIXES: {{^}} } // comment-7
x++;
}
if (x) {
break;
- } else { // comment-4
+ } else { // comment-8
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'break'
- // CHECK-FIXES: {{^}} } // comment-4
+ // CHECK-FIXES: {{^}} } // comment-8
x++;
}
if (x) {
throw 42;
- } else { // comment-5
+ } else { // comment-9
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'throw'
- // CHECK-FIXES: {{^}} } // comment-5
+ // CHECK-FIXES: {{^}} } // comment-9
x++;
}
}
OpenPOWER on IntegriCloud