[clang-tools-extra] 5d1e3ed - [clang-tidy] SimplifyBooleanExprCheck - use cast<> instead of dyn_cast<> to avoid dereference of nullptr

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 12 11:59:39 PST 2022


Author: Simon Pilgrim
Date: 2022-02-12T19:59:30Z
New Revision: 5d1e3ed3e257535fbd51706023685d2c2d9eba51

URL: https://github.com/llvm/llvm-project/commit/5d1e3ed3e257535fbd51706023685d2c2d9eba51
DIFF: https://github.com/llvm/llvm-project/commit/5d1e3ed3e257535fbd51706023685d2c2d9eba51.diff

LOG: [clang-tidy] SimplifyBooleanExprCheck - use cast<> instead of dyn_cast<> to avoid dereference of nullptr

The IfStmt pointer is always referenced inside the replaceCompoundReturnWithCondition call, so assert the cast is correct instead of returning nullptr

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 9c2ddf139a128..61ba4b857636c 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -750,7 +750,7 @@ void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
 void SimplifyBooleanExprCheck::replaceCaseCompoundReturnWithCondition(
     const MatchFinder::MatchResult &Result, bool Negated) {
   const auto *CaseDefault = Result.Nodes.getNodeAs<CaseStmt>(CaseId);
-  const auto *If = dyn_cast<IfStmt>(CaseDefault->getSubStmt());
+  const auto *If = cast<IfStmt>(CaseDefault->getSubStmt());
   replaceCompoundReturnWithCondition(Result, Negated, If);
 }
 
@@ -758,14 +758,14 @@ void SimplifyBooleanExprCheck::replaceDefaultCompoundReturnWithCondition(
     const MatchFinder::MatchResult &Result, bool Negated) {
   const SwitchCase *CaseDefault =
       Result.Nodes.getNodeAs<DefaultStmt>(DefaultId);
-  const auto *If = dyn_cast<IfStmt>(CaseDefault->getSubStmt());
+  const auto *If = cast<IfStmt>(CaseDefault->getSubStmt());
   replaceCompoundReturnWithCondition(Result, Negated, If);
 }
 
 void SimplifyBooleanExprCheck::replaceLabelCompoundReturnWithCondition(
     const MatchFinder::MatchResult &Result, bool Negated) {
   const auto *Label = Result.Nodes.getNodeAs<LabelStmt>(LabelId);
-  const auto *If = dyn_cast<IfStmt>(Label->getSubStmt());
+  const auto *If = cast<IfStmt>(Label->getSubStmt());
   replaceCompoundReturnWithCondition(Result, Negated, If);
 }
 


        


More information about the cfe-commits mailing list