'if' can be replaced with 'switch'
Reports if statements that can be replaced with switch statements.
The replacement result is usually shorter and clearer.
Example:
After the quick-fix is applied:
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
IfCanBeSwitch- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
This inspection only reports if the language level of the project or module is 7 or higher.
Use the Minimum number of 'if' condition branches field to specify the minimum number of if condition branches for an if statement to have to be reported. Note that the terminal else branch (without if) is not counted.
Use the Suggest switch on numbers option to enable the suggestion of switch statements on primitive and boxed numbers and characters.
Use the Suggest switch on enums option to enable the suggestion of switch statements on enum constants.
Use the Only suggest on null-safe expressions option to suggest switch statements that can't introduce a NullPointerException only.
Inspection options
Here you can find the description of settings available for the 'if' can be replaced with 'switch' inspection, and the reference of their default values.
- Minimum number of 'if' condition branches
Default value:
3- Suggest switch on numbers
Default value:
Not selected- Suggest switch on enums
Default value:
Not selected- Only suggest on null-safe expressions
Default value:
Selected
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |