[PATCH] D91037: [clang-tidy][bugprone-use-after-mnove] Warn on std::move for consts

Zinovy Nis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 11:42:36 PST 2020


zinovy.nis created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
zinovy.nis requested review of this revision.

Fix crash in bugprone-redundant-branch-condition on ExprWithCleanups.
Bug: https://bugs.llvm.org/show_bug.cgi?id=48008.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91037

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


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
@@ -1073,6 +1073,29 @@
   }
 }
 
+// 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: {{^\ *$}}
+    }
+  }
+  return 0;
+}
+
 //===--- Special Negatives ------------------------------------------------===//
 
 // Aliasing
Index: clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
@@ -82,7 +82,13 @@
 
   // 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_or_null<BinaryOperator>(InnerIf->getCond());
+  if (!BinOpCond)
+    if (auto *ExprWithCleanupsCond =
+            dyn_cast_or_null<ExprWithCleanups>(InnerIf->getCond()))
+      BinOpCond = dyn_cast_or_null<BinaryOperator>(
+          *ExprWithCleanupsCond->children().begin());
+
   if (isa<DeclRefExpr>(InnerIf->getCond()->IgnoreParenImpCasts()) ||
       (BinOpCond && BinOpCond->getOpcode() == BO_LOr)) {
     SourceLocation IfBegin = InnerIf->getBeginLoc();
@@ -129,7 +135,13 @@
     // 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 = dyn_cast_or_null<BinaryOperator>(InnerIf->getCond());
+    if (!CondOp)
+      if (auto *ExprWithCleanupsCond =
+              dyn_cast_or_null<ExprWithCleanups>(InnerIf->getCond()))
+        CondOp = dyn_cast_or_null<BinaryOperator>(
+            *ExprWithCleanupsCond->children().begin());
+
     const auto *LeftDRE =
         dyn_cast<DeclRefExpr>(CondOp->getLHS()->IgnoreParenImpCasts());
     if (LeftDRE && LeftDRE->getDecl() == CondVar) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91037.303729.patch
Type: text/x-patch
Size: 2727 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201108/ba5cd604/attachment.bin>


More information about the cfe-commits mailing list