2016-08-11 16:18:39 +08:00
|
|
|
// RUN: %check_clang_tidy %s readability-else-after-return %t -- -- -std=c++11 -fexceptions
|
2015-01-15 03:37:54 +08:00
|
|
|
|
2017-11-28 22:57:47 +08:00
|
|
|
namespace std {
|
|
|
|
struct string {
|
|
|
|
string(const char *);
|
|
|
|
~string();
|
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
struct my_exception {
|
|
|
|
my_exception(const std::string &s);
|
|
|
|
};
|
|
|
|
|
2015-01-15 03:37:54 +08:00
|
|
|
void f(int a) {
|
|
|
|
if (a > 0)
|
|
|
|
return;
|
2016-08-11 02:05:47 +08:00
|
|
|
else // comment-0
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'else' after 'return'
|
|
|
|
// CHECK-FIXES: {{^}} // comment-0
|
2015-01-15 03:37:54 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (a > 0) {
|
|
|
|
return;
|
2016-08-11 02:05:47 +08:00
|
|
|
} else { // comment-1
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
|
|
|
|
// CHECK-FIXES: {{^}} } // comment-1
|
2015-01-15 03:37:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a > 0) {
|
|
|
|
f(0);
|
|
|
|
if (a > 10)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-28 06:42:20 +08:00
|
|
|
|
|
|
|
if (a > 0)
|
|
|
|
f(0);
|
|
|
|
else if (a > 10)
|
|
|
|
return;
|
2016-08-11 02:05:47 +08:00
|
|
|
else // comment-2
|
2016-11-05 00:32:14 +08:00
|
|
|
// CHECK-FIXES-NOT: {{^}} // comment-2
|
2015-04-28 06:42:20 +08:00
|
|
|
f(0);
|
2016-11-05 00:32:14 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-01-15 03:37:54 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 02:05:47 +08:00
|
|
|
void foo() {
|
|
|
|
for (unsigned x = 0; x < 42; ++x) {
|
|
|
|
if (x) {
|
|
|
|
continue;
|
2016-11-05 00:32:14 +08:00
|
|
|
} else { // comment-7
|
2016-08-11 02:05:47 +08:00
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'continue'
|
2016-11-05 00:32:14 +08:00
|
|
|
// CHECK-FIXES: {{^}} } // comment-7
|
2016-08-11 02:05:47 +08:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (x) {
|
|
|
|
break;
|
2016-11-05 00:32:14 +08:00
|
|
|
} else { // comment-8
|
2016-08-11 02:05:47 +08:00
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'break'
|
2016-11-05 00:32:14 +08:00
|
|
|
// CHECK-FIXES: {{^}} } // comment-8
|
2016-08-11 02:05:47 +08:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (x) {
|
|
|
|
throw 42;
|
2016-11-05 00:32:14 +08:00
|
|
|
} else { // comment-9
|
2016-08-11 02:05:47 +08:00
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'throw'
|
2016-11-05 00:32:14 +08:00
|
|
|
// CHECK-FIXES: {{^}} } // comment-9
|
2016-08-11 02:05:47 +08:00
|
|
|
x++;
|
|
|
|
}
|
2017-11-28 22:57:47 +08:00
|
|
|
if (x) {
|
|
|
|
throw my_exception("foo");
|
|
|
|
} else { // comment-10
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'throw'
|
|
|
|
// CHECK-FIXES: {{^}} } // comment-10
|
|
|
|
x++;
|
|
|
|
}
|
2016-08-11 02:05:47 +08:00
|
|
|
}
|
|
|
|
}
|