@@ -351,6 +351,9 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
351
351
}
352
352
353
353
bool VisitIfStmt (IfStmt *If) {
354
+ // Skip any if's that have a condition var or an init statement.
355
+ if (If->hasInitStorage () || If->hasVarStorage ())
356
+ return true ;
354
357
/*
355
358
* if (true) ThenStmt(); -> ThenStmt();
356
359
* if (false) ThenStmt(); -> <Empty>;
@@ -461,14 +464,17 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
461
464
* if (Cond) return false; return true; -> return !Cond;
462
465
*/
463
466
auto *If = cast<IfStmt>(*First);
464
- ExprAndBool ThenReturnBool =
465
- checkSingleStatement (If->getThen (), parseReturnLiteralBool);
466
- if (ThenReturnBool && ThenReturnBool.Bool != TrailingReturnBool.Bool ) {
467
- if (Check->ChainedConditionalReturn ||
468
- (!PrevIf && If->getElse () == nullptr )) {
469
- Check->replaceCompoundReturnWithCondition (
470
- Context, cast<ReturnStmt>(*Second), TrailingReturnBool.Bool , If,
471
- ThenReturnBool.Item );
467
+ if (!If->hasInitStorage () && !If->hasVarStorage ()) {
468
+ ExprAndBool ThenReturnBool =
469
+ checkSingleStatement (If->getThen (), parseReturnLiteralBool);
470
+ if (ThenReturnBool &&
471
+ ThenReturnBool.Bool != TrailingReturnBool.Bool ) {
472
+ if (Check->ChainedConditionalReturn ||
473
+ (!PrevIf && If->getElse () == nullptr )) {
474
+ Check->replaceCompoundReturnWithCondition (
475
+ Context, cast<ReturnStmt>(*Second), TrailingReturnBool.Bool ,
476
+ If, ThenReturnBool.Item );
477
+ }
472
478
}
473
479
}
474
480
} else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
@@ -481,7 +487,8 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
481
487
: isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt ()
482
488
: cast<DefaultStmt>(*First)->getSubStmt ();
483
489
auto *SubIf = dyn_cast<IfStmt>(SubStmt);
484
- if (SubIf && !SubIf->getElse ()) {
490
+ if (SubIf && !SubIf->getElse () && !SubIf->hasInitStorage () &&
491
+ !SubIf->hasVarStorage ()) {
485
492
ExprAndBool ThenReturnBool =
486
493
checkSingleStatement (SubIf->getThen (), parseReturnLiteralBool);
487
494
if (ThenReturnBool &&
0 commit comments