[clang-tidy] SimplifyBooleanExprCheck - use cast<> instead of dyn_cast<> to avoid...
authorSimon Pilgrim <llvm-dev@redking.me.uk>
Sat, 12 Feb 2022 19:59:13 +0000 (19:59 +0000)
committerSimon Pilgrim <llvm-dev@redking.me.uk>
Sat, 12 Feb 2022 19:59:30 +0000 (19:59 +0000)
The IfStmt pointer is always referenced inside the replaceCompoundReturnWithCondition call, so assert the cast is correct instead of returning nullptr

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

index 9c2ddf1..61ba4b8 100644 (file)
@@ -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);
 }