Turning Sonar off for certain code

JavaJenkinsSonarqubeFindbugs

Java Problem Overview


Is it possible to turn off sonar (www.sonarsource.org) measurements for specific blocks of code, which one doesn't want to be measured?

An example is the "Preserve Stack Trace" warning which Findbugs outputs. When leaving the server, I might well want to only pass the message back to the client, not including the actual exception which I just caught, if that exception is unknown to the client (because the client doesn't have the JAR in which that exception was contained for example).

Java Solutions


Solution 1 - Java

You can annotate a class or a method with SuppressWarnings

@java.lang.SuppressWarnings("squid:S00112")

squid:S00112 in this case is a Sonar issue ID. You can find this ID in the Sonar UI. Go to Issues Drilldown. Find an issue you want to suppress warnings on. In the red issue box in your code is there a Rule link with a definition of a given issue. Once you click that you will see the ID at the top of the page.

Solution 2 - Java

I recommend you try to suppress specific warnings by using @SuppressWarnings("squid:S2078").

For suppressing multiple warnings you can do it like this @SuppressWarnings({"squid:S2078", "squid:S2076"})

There is also the //NOSONAR comment that tells SonarQube to ignore all errors for a specific line.

Finally if you have the proper rights for the user interface you can issue a flag as a false positive directly from the interface.

The reason why I recommend suppression of specific warnings is that it's a better practice to block a specific issue instead of using //NOSONAR and risk a Sonar issue creeping in your code by accident.

You can read more about this in the FAQ

Edit: 6/30/16 SonarQube is now called SonarLint

In case you are wondering how to find the squid number. Just click on the Sonar message (ex. Remove this method to simply inherit it.) and the Sonar issue will expand.

On the bottom left it will have the squid number (ex. squid:S1185 Maintainability > Understandability)

So then you can suppress it by @SuppressWarnings("squid:S1185")

Solution 3 - Java

This is a FAQ. You can put //NOSONAR at the end of the line triggering the warning. > //NOSONAR > > For most languages, SonarQube supports the use of the generic mechanism: //NOSONAR at the end of the line of the issue. This will suppress all issues - now and in the future - that might be raised on the line.

I prefer using the FindBugs mechanism though, which consists in adding the @SuppressFBWarnings annotation:

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
    value = "NAME_OF_THE_FINDBUGS_RULE_TO_IGNORE",
    justification = "Why you choose to ignore it")

Solution 4 - Java

Use //NOSONAR on the line you get warning if it is something you cannot help your code with. It works!

Solution 5 - Java

I not be able to find squid number in sonar 5.6, with this annotation also works:

@SuppressWarnings({"pmd:AvoidCatchingGenericException", "checkstyle:com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck"})

Solution 6 - Java

Actually it's a tricky to understand which is the ID of issue so i used @SuppressWarnings("all") annotation which basically is applied of all rules but to be more precise is better to use the ID. Click to see picture -> Sonar Issue ID

So when click on "Why is this an issue?" in the red box, i you will get to a screen with explanation. On top of the screen in left side you can see rule name, and on the left the id of this rule. So to applied for this rule only you have to add this annotation to your class or method.

@SuppressWarnings("common-java:DuplicatedBlocks")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnt KutscheraView Question on Stackoverflow
Solution 1 - Javamaestr0View Answer on Stackoverflow
Solution 2 - JavawirednikoView Answer on Stackoverflow
Solution 3 - JavaJB NizetView Answer on Stackoverflow
Solution 4 - JavaJainView Answer on Stackoverflow
Solution 5 - JavaNokerView Answer on Stackoverflow
Solution 6 - JavaPapsView Answer on Stackoverflow