[clang-tools-extra] 4364539 - [clang-tidy] Fix crash in bugprone-redundant-branch-condition on ExprWithCleanups

Zinovy Nis via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 22:02:47 PST 2020


Author: Zinovy Nis
Date: 2020-11-14T08:35:21+03:00
New Revision: 4364539b3a4c5e73561e5ff29e6952a75e89dc43

URL: https://github.com/llvm/llvm-project/commit/4364539b3a4c5e73561e5ff29e6952a75e89dc43
DIFF: https://github.com/llvm/llvm-project/commit/4364539b3a4c5e73561e5ff29e6952a75e89dc43.diff

LOG: [clang-tidy] Fix crash in bugprone-redundant-branch-condition on ExprWithCleanups

Bug: https://bugs.llvm.org/show_bug.cgi?id=48008

Differential Revision: https://reviews.llvm.org/D91037

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
index 137356acbdba..abb8e8be9b2f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
@@ -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();
@@ -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) {

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
index 2bfd49a82955..dd001e836477 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
@@ -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


        


More information about the cfe-commits mailing list