[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:
Alexander Kornienko 2019-01-21 16:26:54 +00:00
parent a2b04ad5c4
commit efde822cd1
2 changed files with 22 additions and 4 deletions

View File

@ -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);

View File

@ -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;
}
}