How to disable Sonar rules for specific files?

后端 未结 6 744
囚心锁ツ
囚心锁ツ 2021-01-03 21:35

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

相关标签:
6条回答
  • 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
    0 讨论(0)
  • 2021-01-03 22:02

    You can set specific files and rules under sonar-project.properties with following contents:

    # Ignore Issues
    sonar.issue.ignore.multicriteria=e1,e2
    # Skip Bold Check
    sonar.issue.ignore.multicriteria.e1.ruleKey=Web:BoldCheck
    sonar.issue.ignore.multicriteria.e1.resourceKey=**/*.html
    # Skip Tag Check
    sonar.issue.ignore.multicriteria.e2.ruleKey=Web:S1234
    sonar.issue.ignore.multicriteria.e2.resourceKey=**/*.html
    

    Change the ruleKey and resourceKey based on your specific need.

    0 讨论(0)
  • 2021-01-03 22:07

    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.

    0 讨论(0)
  • 2021-01-03 22:13

    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:

    enter image description here

    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.

    0 讨论(0)
  • 2021-01-03 22:14

    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.

    0 讨论(0)
  • 2021-01-03 22:16

    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 {...
    
    0 讨论(0)
提交回复
热议问题