-3

I init a switch has only 1 case atm and expect adding more later.

switch(text) {
   case app1:
      return value
   default:
      return default value
}

How to suppress it? I've tried so far @SuppressWarnings("all"), @SuppressWarnings("incomplete-switch") but not working. Thanks

Connor Low
  • 5,900
  • 3
  • 31
  • 52

2 Answers2

3

You want //noinspection SwitchStatementWithTooFewBranches:

//noinspection SwitchStatementWithTooFewBranches
switch(text) {
  // .. 
}

You can disable most inspections for a given scope by hovering over an error, selecting "More actions...", and then expanding the fix menu: Replace 'switch' with 'if' > Suppress for statement

Connor Low
  • 5,900
  • 3
  • 31
  • 52
  • from mine, no option for suppress for statement, ah I realize this warning because of sonarlint. Do you know how to suppress it? – Zx binhjy xZ Aug 30 '22 at 03:27
  • @ZxbinhjyxZ see [How to suppress warnings sonarlint](https://stackoverflow.com/questions/47369526/how-to-suppress-warning-for-a-specific-method-with-intellij-sonarlint-plugin) – Connor Low Aug 30 '22 at 15:05
0

Either leave the warning - which is irritating, or create precursor code with a comment

//TODO When *text* can have alternatives, create a switch statement.

TODO comments will normally be listed in the IDE.

In this way you will keep your code at the correct technical stage without (unfounded?) assumption that more cases will follow.

Of course the real problem here is that a future alternative risks not to be covered. You hope that a future addition will give a future warning that not all cases are covered.

The solution could be made using OOP: instead a switch a method of the base class.

For instance with an enum:

public enum MyEnum { APP1;
    public String getAppName() {
        return "app1";
    }
}

MyEnum text = ...;
return text.getAppName();
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138