[clang-tidy] Simplify redundant branch condition check

Differential Revision: https://reviews.llvm.org/D97151
This commit is contained in:
Stephen Kelly 2020-12-29 23:28:28 +00:00
parent df42f9950d
commit a5feefa3c7
2 changed files with 17 additions and 14 deletions

View File

@ -48,23 +48,23 @@ void RedundantBranchConditionCheck::registerMatchers(MatchFinder *Finder) {
.bind(CondVarStr); .bind(CondVarStr);
Finder->addMatcher( Finder->addMatcher(
ifStmt( ifStmt(
hasCondition(ignoringParenImpCasts(anyOf( hasCondition(anyOf(
declRefExpr(hasDeclaration(ImmutableVar)).bind(OuterIfVar1Str), declRefExpr(hasDeclaration(ImmutableVar)).bind(OuterIfVar1Str),
binaryOperator(hasOperatorName("&&"), binaryOperator(
hasEitherOperand(ignoringParenImpCasts( hasOperatorName("&&"),
declRefExpr(hasDeclaration(ImmutableVar)) hasEitherOperand(declRefExpr(hasDeclaration(ImmutableVar))
.bind(OuterIfVar2Str))))))), .bind(OuterIfVar2Str))))),
hasThen(hasDescendant( hasThen(hasDescendant(
ifStmt(hasCondition(ignoringParenImpCasts( ifStmt(hasCondition(anyOf(
anyOf(declRefExpr(hasDeclaration(varDecl( declRefExpr(hasDeclaration(
equalsBoundNode(CondVarStr)))) varDecl(equalsBoundNode(CondVarStr))))
.bind(InnerIfVar1Str), .bind(InnerIfVar1Str),
binaryOperator( binaryOperator(
hasAnyOperatorName("&&", "||"), hasAnyOperatorName("&&", "||"),
hasEitherOperand(ignoringParenImpCasts( hasEitherOperand(
declRefExpr(hasDeclaration(varDecl( declRefExpr(hasDeclaration(varDecl(
equalsBoundNode(CondVarStr)))) equalsBoundNode(CondVarStr))))
.bind(InnerIfVar2Str)))))))) .bind(InnerIfVar2Str))))))
.bind(InnerIfStr))), .bind(InnerIfStr))),
forFunction(functionDecl().bind(FuncStr))) forFunction(functionDecl().bind(FuncStr)))
.bind(OuterIfStr), .bind(OuterIfStr),

View File

@ -26,6 +26,9 @@ public:
: ClangTidyCheck(Name, Context) {} : ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
return TK_IgnoreUnlessSpelledInSource;
}
}; };
} // namespace bugprone } // namespace bugprone