31

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?

6 Answers6

16

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.

Sjeiti
  • 2,468
  • 1
  • 31
  • 33
Mithfindel
  • 4,553
  • 1
  • 23
  • 32
  • 3
    Great, thanks. A note for future folks that land on this page: the rule key pattern is : –  Jan 14 '14 at 16:30
  • 4
    @mith The link is broken. Please make your answer self contained. – 1010 Dec 01 '15 at 14:18
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.

Nikhil
  • 194
  • 1
  • 8
11

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.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
9

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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • I have a Console project and want to disable the rule "Console logging should not be used" on it. I use the UI as you said but it seems MSBuild don't get theses exclusions (anything found in Agent logs, or .sonarqube logs). do I have to have a configuration file or MSbuild should retreive Sonar configuration for projects ? Rule Key Pattern:csharpsquid:S2228 File Path Pattern:**/ChangeLog/**/*.* – BonOeil Jan 27 '16 at 08:31
  • BonOeil, you should probably ask a separate question for that. – Matthias Braun Jan 27 '16 at 14:01
7

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 {...
assembler
  • 3,098
  • 12
  • 43
  • 84
Dhammadip
  • 87
  • 1
  • 4
  • 1
    Just a tip: If you want to get the exact code to put in @SuppressWarning, you can go to the SonarQube Dashboard, and then click into the literal "Why is this an issue?" you're interested on, and a small window will open down the screen; look the right side and you'll see something like "java:S1234". That's code. – ATorras Nov 09 '20 at 16:43
3

Yes, it is possible.

Rajan
  • 101
  • 1
  • 2