mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-01-27 03:48:33 +00:00
[clang-tidy] Work around http://llvm.org/PR40392
The readability-else-after-return check should be smarter about cases where the variable defined in the condition is used in the `else` branch. This patch makes it just ignore such cases, but alternative solutions may be better (added a FIXME). llvm-svn: 351751
This commit is contained in:
parent
a2b04ad5c4
commit
efde822cd1
@ -18,16 +18,22 @@ namespace tidy {
|
||||
namespace readability {
|
||||
|
||||
void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
|
||||
const auto ControlFlowInterruptorMatcher =
|
||||
const auto InterruptsControlFlow =
|
||||
stmt(anyOf(returnStmt().bind("return"), continueStmt().bind("continue"),
|
||||
breakStmt().bind("break"),
|
||||
expr(ignoringImplicit(cxxThrowExpr().bind("throw")))));
|
||||
Finder->addMatcher(
|
||||
compoundStmt(forEach(
|
||||
ifStmt(unless(isConstexpr()),
|
||||
hasThen(stmt(
|
||||
anyOf(ControlFlowInterruptorMatcher,
|
||||
compoundStmt(has(ControlFlowInterruptorMatcher))))),
|
||||
// FIXME: Explore alternatives for the
|
||||
// `if (T x = ...) {... return; } else { <use x> }`
|
||||
// pattern:
|
||||
// * warn, but don't fix;
|
||||
// * fix by pulling out the variable declaration out of
|
||||
// the condition.
|
||||
unless(hasConditionVariableStatement(anything())),
|
||||
hasThen(stmt(anyOf(InterruptsControlFlow,
|
||||
compoundStmt(has(InterruptsControlFlow))))),
|
||||
hasElse(stmt().bind("else")))
|
||||
.bind("if"))),
|
||||
this);
|
||||
|
@ -105,3 +105,15 @@ void foo() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern int *g();
|
||||
extern void h(int **x);
|
||||
|
||||
int *decl_in_condition() {
|
||||
if (int *x = g()) {
|
||||
return x;
|
||||
} else {
|
||||
h(&x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user