How to disable Sonar rules for specific files?

三世轮回 提交于 2019-12-30 07:53:40

问题


I've got a project I'm working on and some of the files violate some of the rules, but in ways that are not real issues, and are thus distracting noise. However, I don't want to disable these rules globally, and I would prefer not to have to mark 'em as false positives one by one.

Is there a way to disable Sonar rules for specific files, and if so, how?


回答1:


Since SonarQube 4.0, you can define issue exclusion patterns based on rule key and file path pattern.

On previous versions, you can rely upon the Switch Off Violations plugin.




回答2:


Building on Mithfindel's answer and dpk's comment, this removes the warning

System.out and System.err should not be used as loggers

from all classes in packages named log (or any subpackage thereof) by adding an ignore pattern for the rule squid:S106:

For a list of the keys to all your rules go to your profile under Quality Profiles and select Download to get a .csv file containing all the rules' keys.

I'm using SonarQube version 4.1.1.




回答3:


You can annotate a class or a method with SuppressWarnings.

Here is an example:

@java.lang.SuppressWarnings("squid:S00111") squid:S00111 in this case is a Sonar issue ID. You can find this issue id from the Sonar web ui.




回答4:


Yes, it is possible.

  • 1.Goto Administration tab->Analysis Scope->Issues

  • 2.There , you will find "Ignore Issues on Multiple Criteria".

  • 3.Provide Rule ID in "Rule Key pattern" textbox [Rule ID can be found by clicking on the particular rule and find it in top right corner]

  • 4.Provide Filepath for which you need to ignore rule in "File Path Pattern" textbox

  • 5.Click on Save Issues settings

  • Image to know where Rule ID will be present in the page
  • Image where Rule key pattern and File Key pattern text boxes are present



回答5:


Using below annotation we can ignore the rule from the specific files

For one rule

@java.lang.SuppressWarnings("squid:S2696")
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {....

For more than one rule

@java.lang.SuppressWarnings({"squid:S2696", "squid:S1172", "squid:CommentedOutCodeLine"})
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {...


来源:https://stackoverflow.com/questions/21103175/how-to-disable-sonar-rules-for-specific-files

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