Home > database >  How to suppresswarnings for switch has only 1 case
How to suppresswarnings for switch has only 1 case

Time:08-30

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

CodePudding user response:

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

CodePudding user response:

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();
  • Related