问题
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.
回答1:
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..
来源:https://stackoverflow.com/questions/49658261/is-there-any-way-to-get-sonarqube-to-only-warn-about-incomplete-switch-statement