[clang-tools-extra] 7ad94bd - [clang-tidy] ContainerSizeEmptyCheck::check - simplify isa<> and dyn_cast<> repeated calls

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 12 03:31:44 PST 2022


Author: Simon Pilgrim
Date: 2022-02-12T11:31:27Z
New Revision: 7ad94bd74bb5e2c984f541bc1921a9eda73ed973

URL: https://github.com/llvm/llvm-project/commit/7ad94bd74bb5e2c984f541bc1921a9eda73ed973
DIFF: https://github.com/llvm/llvm-project/commit/7ad94bd74bb5e2c984f541bc1921a9eda73ed973.diff

LOG: [clang-tidy] ContainerSizeEmptyCheck::check - simplify isa<> and dyn_cast<> repeated calls

Just use dyn_cast<> to determine literal + container values from the binop

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index 548fed9a47c3..d399c957c7c7 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -218,23 +218,22 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
     Hint = FixItHint::CreateReplacement(BinCmpRewritten->getSourceRange(),
                                         ReplacementText);
   } else if (BinaryOp) { // Determine the correct transformation.
+    const auto *LiteralLHS =
+        llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts());
+    const auto *LiteralRHS =
+        llvm::dyn_cast<IntegerLiteral>(BinaryOp->getRHS()->IgnoreImpCasts());
+    const bool ContainerIsLHS = !LiteralLHS;
+
+    uint64_t Value = 0;
+    if (LiteralLHS)
+      Value = LiteralLHS->getValue().getLimitedValue();
+    else if (LiteralRHS)
+      Value = LiteralRHS->getValue().getLimitedValue();
+    else
+      return;
+
     bool Negation = false;
-    const bool ContainerIsLHS =
-        !llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts());
     const auto OpCode = BinaryOp->getOpcode();
-    uint64_t Value = 0;
-    if (ContainerIsLHS) {
-      if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>(
-              BinaryOp->getRHS()->IgnoreImpCasts()))
-        Value = Literal->getValue().getLimitedValue();
-      else
-        return;
-    } else {
-      Value =
-          llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts())
-              ->getValue()
-              .getLimitedValue();
-    }
 
     // Constant that is not handled.
     if (Value > 1)


        


More information about the cfe-commits mailing list