[clang-tools-extra] 7674bd4 - [clang-tidy] Merges separate isa<>/assert/unreachable/dyn_cast<>/cast<> calls

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 29 08:42:13 PDT 2021


Author: Simon Pilgrim
Date: 2021-09-29T16:35:29+01:00
New Revision: 7674bd4d44921d2d110cfb56f2674d4e6e8a68e3

URL: https://github.com/llvm/llvm-project/commit/7674bd4d44921d2d110cfb56f2674d4e6e8a68e3
DIFF: https://github.com/llvm/llvm-project/commit/7674bd4d44921d2d110cfb56f2674d4e6e8a68e3.diff

LOG: [clang-tidy] Merges separate isa<>/assert/unreachable/dyn_cast<>/cast<> calls

We can directly use cast<> instead of separate dyn_cast<> with assertions as cast<> will perform this for us.

Similarly we can replace a if(isa<>)+cast<>/dyn_cast<> with if(dyn_cast<>)

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp b/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
index 40ba3913467f2..0290789e76bfe 100644
--- a/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
@@ -120,9 +120,7 @@ bool UnrollLoopsCheck::hasKnownBounds(const Stmt *Statement,
   if (isa<WhileStmt, DoStmt>(Statement))
     return false;
   // The last loop type is a for loop.
-  const auto *ForLoop = dyn_cast<ForStmt>(Statement);
-  if (!ForLoop)
-    llvm_unreachable("Unknown loop");
+  const auto *ForLoop = cast<ForStmt>(Statement);
   const Stmt *Initializer = ForLoop->getInit();
   const Expr *Conditional = ForLoop->getCond();
   const Expr *Increment = ForLoop->getInc();
@@ -142,8 +140,7 @@ bool UnrollLoopsCheck::hasKnownBounds(const Stmt *Statement,
     if (!Op->isIncrementDecrementOp())
       return false;
 
-  if (isa<BinaryOperator>(Conditional)) {
-    const auto *BinaryOp = dyn_cast<BinaryOperator>(Conditional);
+  if (const auto *BinaryOp = dyn_cast<BinaryOperator>(Conditional)) {
     const Expr *LHS = BinaryOp->getLHS();
     const Expr *RHS = BinaryOp->getRHS();
     // If both sides are value dependent or constant, loop bounds are unknown.
@@ -173,8 +170,7 @@ bool UnrollLoopsCheck::hasLargeNumIterations(const Stmt *Statement,
     assert(CXXLoopBound && "CXX ranged for loop has no loop bound");
     return exprHasLargeNumIterations(CXXLoopBound, Context);
   }
-  const auto *ForLoop = dyn_cast<ForStmt>(Statement);
-  assert(ForLoop && "Unknown loop");
+  const auto *ForLoop = cast<ForStmt>(Statement);
   const Stmt *Initializer = ForLoop->getInit();
   const Expr *Conditional = ForLoop->getCond();
   const Expr *Increment = ForLoop->getInc();
@@ -189,10 +185,9 @@ bool UnrollLoopsCheck::hasLargeNumIterations(const Stmt *Statement,
       InitValue = Evaluation->getInt().getExtValue();
     }
   }
-  assert(isa<BinaryOperator>(Conditional) &&
-         "Conditional is not a binary operator");
+
   int EndValue;
-  const auto *BinaryOp = dyn_cast<BinaryOperator>(Conditional);
+  const auto *BinaryOp = cast<BinaryOperator>(Conditional);
   if (!extractValue(EndValue, BinaryOp, Context))
     return true;
 


        


More information about the cfe-commits mailing list