[java:S131] FP, if return is the default case

Qube: Community 9.9

Consider a method ile this:

public String foo(int bar)
{
    switch (bar)
    {
        case 1:
            return "lorem";
    }
    
    return "ipsum";
}

This switch (yes, it could be modernized) has some concrete cases and no default case. But the method returns immediately after the switch. This is basically the default case and the rule should accept it as the default case given.

The method would not even need to return right after the switch. But I think, this is a very common case where the rule just makes me rewrite the code with no benefit and no reduced bug potential or whatever.

Hi Marvin,

Thanks for your feedback! The concept behind this rule is that a switch statement should cover all possible branches in order to ensure that the developer did not just forget one case (“exhaustive switch”).

However, I think you have a fair point if all case clauses of a switch statement end with a return. Because then (and only then) having code in the default clause and moving that code after the switch statement instead are both equivalent. Users should not be forced to refactor their code to have a default clause in that case.

I created a ticket here to improve the rule.

Cheers,
Marco

1 Like

Great news. Thanks!