Add fallthrough exception to java:S128

Rule java:S128 flags switch cases without a terminating break (or other exit condition such as return or throw).

Sometimes the “fallthrough” is deliberate. Some versions of this rule in other languages (cpp for one), make exceptions for when a standard annotation is used for marking a fallthrough as intentional. It would be nice to add this capability to java:S128. Unfortunately, Java doesn’t have a standard way to do this. (There apparently is some kind of annotation but it only works at method granularity.) There are various schemes in some style guides but they aren’t universally used. So it would need a parameter with a regex. For max flexibility it should look at either the line with the case label or the line above.

Hello @MisterPi ,

While such a behaviour is not properly documented, you can place a comment in your case block to indicate that the fall through is deliberate.

switch (i) {
      case 0:
        // fallthrough
        i = 1;
      case 1:
        throw new IllegalStateException("Something is wrong!");

A ticket has been created to document the feature.



1 Like

Thanks, that’s good to know. I can’t figure out exactly how it works though. Here is the example from javascript:S128 (typescript is the same):

  case 3:                               // Ends with comment when fall-through is intentional
    console.log("this case falls through")
    // fall through
  case 4:                                // Use of throw statement

while the same thing in php:S128 is:

  case 4:
    echo 'Second case, which falls through';
    // no break        <- comment is used when fall-through is intentional in a non-empty case body
  default:                 // For the last case, use of break statement is optional

So I can’t tell exactly how the exception works. If I am to assume that the rule works the same way for all languages, then it would seem to be that the case has to end with a line consisting of only a comment. Whereas your example has the case start with a comment. Also, there doesn’t seem to be any requirement for what the comment says as it seems “fallthrough” (with or without a space) and “no break” both work.

My personal style has been to add the comment above the next case line (as in the two examples I quoted in javascript:S128 and php:S128) or on the next case line (after the colon), using the phrase “Deliberate fallthrough.” But the exception should be narrow enough to reduce false negatives. Something like “a comment anywhere in the case body” could let too many actual violations slip through since developers should be routinely adding comments whether or not there’s a fallthrough.

From what I can see looking at our own violations and nearby cases not flagged, it seems the case has to end with a comment of either the // or /* form.

But then there’s this one, which has a comment at both the beginning and the end, yet is still flagged:

Can you please explain? Is it that the comment in line 184 is indented the same as line 185, so SQ recognizes it as being attached to the case statement in line 185 rather than part of the case startling at line 169?

I played around a bit with SonarLint bound to an 8.6 server, and experimentally determined that it looks for a comment ANYWHERE within the case, meaning after the colon at the end of the case line to before the next case (so something like “/* fallthru */ case 2:” works for the case above that falls through to case 2, not for case 2 itself). It has to be some variant of “fall through” with either one or two words, “fall” can be “falls” but not “falling”; “through” can be “thru” – the “// no break” example for js doesn’t work for java.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.