Unreachable Case

Flags 'Case' blocks that will never execute.

Remarks

Not all unreachable 'Case' blocks may be flagged, depending on expression complexity.

Reasoning

Unreachable code is certainly unintended, and is probably either redundant, or a bug.

Default severity

Warning

Inspection type

CodeQualityIssues

Examples

This example should trigger a result

MyModule (StandardModule)
Private Sub Example(ByVal value As Long) Select Case value Case 0 To 99 ' ... Case 50 ' unreachable: case is covered by a preceding condition. ' ... Case Is < 100 ' ... Case < 0 ' unreachable: case is covered by a preceding condition. ' ... End Select End Sub

This example should trigger a result

MyModule (StandardModule)
'If the cumulative result of multiple 'Case' statements 'cover the entire range of possible values for a data type, 'then all remaining 'Case' statements are unreachable Private Sub ExampleAllValuesCoveredIntegral(ByVal value As Long, ByVal result As Long) Select Case result Case Is < 100 ' ... Case Is > -100 ' ... 'all possible values are covered by preceding 'Case' statements Case value * value ' unreachable ' ... Case value + value ' unreachable ' ... Case Else ' unreachable ' ... End Select End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Public Enum ProductID Widget = 1 Gadget = 2 Gizmo = 3 End Enum Public Sub ExampleEnumCaseElse(ByVal product As ProductID) 'Enums are evaluated as the 'Long' data type. So, in this example, 'even though all the ProductID enum values have a 'Case' statement, 'the 'Case Else' will still execute for any value of the 'product' 'parameter that is not a ProductID. Select Case product Case Widget ' ... Case Gadget ' ... Case Gizmo ' ... Case Else 'is reachable ' Raise an error for unrecognized/unhandled ProductID End Select End Sub

This example should trigger a result

MyModule (StandardModule)
'The inspection flags Range Clauses that are not of the required form: '[x] To [y] where [x] less than or equal to [y] Private Sub ExampleInvalidRangeExpression(ByVal value As String) Select Case value Case "Beginning" To "End" ' ... Case "Start" To "Finish" ' unreachable: incorrect form. ' ... Case Else ' ... End Select End Sub

Rubberduck.CodeAnalysis.Inspections.Concrete.UnreachableCaseInspection.cs (Prerelease-v2.5.9.6289)