Skip to content

Commit

Permalink
[clang-tidy] Fix crash in bugprone-redundant-branch-condition on Expr…
Browse files Browse the repository at this point in the history
  • Loading branch information
irishrover committed Nov 14, 2020
1 parent ac06b1a commit 4364539
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Expand Up @@ -82,7 +82,9 @@ void RedundantBranchConditionCheck::check(const MatchFinder::MatchResult &Result

// For standalone condition variables and for "or" binary operations we simply
// remove the inner `if`.
const auto *BinOpCond = dyn_cast<BinaryOperator>(InnerIf->getCond());
const auto *BinOpCond =
dyn_cast<BinaryOperator>(InnerIf->getCond()->IgnoreParenImpCasts());

if (isa<DeclRefExpr>(InnerIf->getCond()->IgnoreParenImpCasts()) ||
(BinOpCond && BinOpCond->getOpcode() == BO_LOr)) {
SourceLocation IfBegin = InnerIf->getBeginLoc();
Expand Down Expand Up @@ -129,7 +131,8 @@ void RedundantBranchConditionCheck::check(const MatchFinder::MatchResult &Result
// For "and" binary operations we remove the "and" operation with the
// condition variable from the inner if.
} else {
const auto *CondOp = cast<BinaryOperator>(InnerIf->getCond());
const auto *CondOp =
cast<BinaryOperator>(InnerIf->getCond()->IgnoreParenImpCasts());
const auto *LeftDRE =
dyn_cast<DeclRefExpr>(CondOp->getLHS()->IgnoreParenImpCasts());
if (LeftDRE && LeftDRE->getDecl() == CondVar) {
Expand Down
Expand Up @@ -1073,6 +1073,34 @@ void positive_comma_after_condition() {
}
}

// ExprWithCleanups doesn't crash
int positive_expr_with_cleanups() {
class RetT {
public:
RetT(const int _code) : code_(_code) {}
bool Ok() const { return code_ == 0; }
static RetT Test(bool &_isSet) { return 0; }

private:
int code_;
};

bool isSet = false;
if (RetT::Test(isSet).Ok() && isSet) {
if (RetT::Test(isSet).Ok() && isSet) {
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'isSet' [bugprone-redundant-branch-condition]
// CHECK-FIXES: if (RetT::Test(isSet).Ok() ) {
}
}
if (isSet) {
if ((RetT::Test(isSet).Ok() && isSet)) {
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'isSet' [bugprone-redundant-branch-condition]
// CHECK-FIXES: if ((RetT::Test(isSet).Ok() )) {
}
}
return 0;
}

//===--- Special Negatives ------------------------------------------------===//

// Aliasing
Expand Down

0 comments on commit 4364539

Please sign in to comment.