Is there any way to get SonarQube to only warn about incomplete Switch statements?

自作多情 提交于 2020-01-15 08:23:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!