3

Using Java, SonarQube is complaining about switch statements on enum values not having a default: case.

The reasoning given is:

"The requirement for a final default clause is defensive programming. The clause should either take appropriate action, or contain a suitable comment as to why no action is taken. When the switch covers all current values of an enum - and especially when it doesn't - a default case should still be used because there is no guarantee that the enum won't be extended."

I do not agree with the above statements - I want the following behavior to generate a warning:

  • Modifying an enum so that the switch no longer covers every case.

By requiring a default case - we will not get a warning if the enum changes, and the switch will no longer handles all the cases.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
CasaDelGato
  • 603
  • 7
  • 17
  • You've tagged this with both 'sonarqube-scan' and 'sonarlint'. Are you seeing it in both contexts? If so, please provide your versions of SonarLint and the SonarJava installed in your server, as well as your SonarLint flavor. If not, please distinguish and provide the relevant versions. – G. Ann - SonarSource Team Apr 04 '18 at 18:50
  • I admit to getting a bit confused as to which parts do what. Our project is connected to a SonarQube server, "Version 6.6 (build 32724) " I'm working in Eclipse, which has SonarLint 3.2.0 installed. – CasaDelGato Apr 05 '18 at 15:36
  • @CasaDelGato : considering that my answer is the only one here, please consider selecting my answer as the correct one. Thanks – Rann Lifshitz May 21 '18 at 03:42

1 Answers1

2

SonarQube allows you to add custom rules.

Assuming there is no existing enum related rule warning on incomplete switch case coverage, you can simply add your own rule on SQ and have it enforced on all the SonarLint instances connected to your SQ server..

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42