@@ -3592,6 +3592,41 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor>
3592
3592
return newCaseLabel;
3593
3593
}
3594
3594
3595
+ bool hasSwitchCases (Stmt* inStmt)
3596
+ {
3597
+ Stmt* stmt = inStmt;
3598
+ // Unwrap any surrounding `{ ... }` so we can look
3599
+ // at the statement inside.
3600
+ while (auto blockStmt = as<BlockStmt>(stmt))
3601
+ {
3602
+ stmt = blockStmt->body ;
3603
+ continue ;
3604
+ }
3605
+
3606
+ if (auto seqStmt = as<SeqStmt>(stmt))
3607
+ {
3608
+ // Walk through the children looking for cases
3609
+ for (auto childStmt : seqStmt->stmts )
3610
+ {
3611
+ if (hasSwitchCases (childStmt))
3612
+ {
3613
+ return true ;
3614
+ }
3615
+ }
3616
+ }
3617
+ else if (auto caseStmt = as<CaseStmt>(stmt))
3618
+ {
3619
+ return true ;
3620
+ }
3621
+ else if (auto defaultStmt = as<DefaultStmt>(stmt))
3622
+ {
3623
+ // A 'default:' is a kind of case.
3624
+ return true ;
3625
+ }
3626
+
3627
+ return false ;
3628
+ }
3629
+
3595
3630
// Given a statement that appears as (or in) the body
3596
3631
// of a `switch` statement
3597
3632
void lowerSwitchCases (Stmt* inStmt, SwitchStmtInfo* info)
@@ -3734,6 +3769,14 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor>
3734
3769
// First emit code to compute the condition:
3735
3770
auto conditionVal = getSimpleVal (context, lowerRValueExpr (context, stmt->condition ));
3736
3771
3772
+ // Check for any cases or default.
3773
+ if (!hasSwitchCases (stmt->body ))
3774
+ {
3775
+ // If we don't have any case/default then nothing inside switch can be executed (other than condition)
3776
+ // so we are done.
3777
+ return ;
3778
+ }
3779
+
3737
3780
// Remember the initial block so that we can add to it
3738
3781
// after we've collected all the `case`s
3739
3782
auto initialBlock = builder->getBlock ();
0 commit comments