Undetected example of unreachable code when using switch on an enum

244 views
Skip to first unread message

Brian Sperlongano

unread,
Jan 5, 2017, 2:25:14 PM1/5/17
to SonarQube
Hello!

I have found an example of unreachable code that is not detected by SonarQube when using switch() on enum types.

For example:

enum SomeEnum {A,B}

void print(SomeEnum e) {
  switch(e) {
    case A:
      //code
      break;
    case B:
      //code
      break;
    default:
      break;
  }
}

In this example, the "default" case is unreachable.  If SomeEnum.A or SomeEnum.B is passed in, the applicabe case and break will be triggered.  If null is passed in, the switch statement will cause a NullPointerException to be thrown.

Since there are no remaining possibilities, the default label and subsequent break are non-reachable and non-testable code and thus should be eliminated.

Thanks,
Brian

G. Ann Campbell

unread,
Jan 5, 2017, 3:26:19 PM1/5/17
to SonarQube
Hi Brian,

You're right that the default case is currently unreachable. But what about when your colleague expands the Enum?

We've specified a rule to address this issue, but it hasn't been implemented yet in Java:
  1. RSPEC-3562
  2. "switch" statements should cover all cases


Ann

Brian Sperlongano

unread,
Jan 5, 2017, 3:42:01 PM1/5/17
to SonarQube
Hi Ann,

If a colleague expands the enum, that is exactly the problem - the formerly unreachable default case suddenly becomes reachable, when you probably meant to add a new case!

I agree with the RSPEC description, though it doesn't specifically cover the unreachable default scenario which I think should be included for the above reason.

Thanks,
Brian

Michael Piefel

unread,
Jan 9, 2017, 8:14:38 AM1/9/17
to SonarQube

Am Donnerstag, 5. Januar 2017 21:26:19 UTC+1 schrieb G. Ann Campbell:
Hi Brian,

You're right that the default case is currently unreachable. But what about when your colleague expands the Enum?

We've specified a rule to address this issue, but it hasn't been implemented yet in Java:
  1. RSPEC-3562
  2. "switch" statements should cover all cases


Hi Ann, just wanted to add that this would be highly welcome for Java as well. The need for a default case to pacify Sonarqube when switching over enums has bugged me for quite some time now. If possible, I prefer to cover all cases of an enum, and consider a default to be harmful in these cases.

Greetings, Mike

Michael Piefel

unread,
Jan 9, 2017, 8:45:34 AM1/9/17
to SonarQube


Am Montag, 9. Januar 2017 14:14:38 UTC+1 schrieb Michael Piefel:
Hi Ann, just wanted to add that this would be highly welcome for Java as well. The need for a default case to pacify Sonarqube when switching over enums has bugged me for quite some time now. If possible, I prefer to cover all cases of an enum, and consider a default to be harmful in these cases.

Huh, I didn’t realize that  squid:SwitchLastCaseIsDefaultCheck actually checks for a complete coverage with enum values now. I take everything back, Sonarqube rocks.

G. Ann Campbell

unread,
Jan 9, 2017, 8:48:18 AM1/9/17
to Michael Piefel, SonarQube
Thanks for the kind words @Michael.


:-)
Ann



---
G. Ann CAMPBELL | SonarSource
Product Manager

--
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/urhHx9dySQk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/aa67c4db-058a-456f-9670-1878926d0e8b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages