Turning Sonar off for certain code

263,363

Solution 1

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 2

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 3

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 4

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

Solution 5

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"})
Share:
263,363

Related videos on Youtube

Ant Kutschera
Author by

Ant Kutschera

Ant is a freelance Java architect and developer. He has been writing a blog and white papers since 2004 and writes about anything he finds interesting, related to Java or software. Most recently he has been working on large enterprise systems involving Angular and Java EE. He believes very strongly in being involved in all parts of the software life cycle.

Updated on July 08, 2022

Comments

  • Ant Kutschera
    Ant Kutschera almost 2 years

    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).

  • Marcel Stör
    Marcel Stör almost 11 years
    Agreed. However, I'm not sure if Sonar correctly interprets @SuppressFBWarnings (added to avoid clashes with java.lang.SuppressWarnings) and also ignores it.
  • JB Nizet
    JB Nizet almost 11 years
    AFAIK, Sonar uses FindBugs. So if FindBugs handles these annotations, I don't see why they wouldn't work when running FindBugs through Sonar. Shouldn't be hard to test anyway.
  • wiredniko
    wiredniko almost 9 years
    The FAQ link is outdated. Here is the new FAQ
  • bcody
    bcody over 8 years
    I can confirm that this also works with a specific FindBugs rule-ID, e.g. @SuppressWarnings("findbugs:UI_INHERITANCE_UNSAFE_GETRESOURC‌​E").
  • AlexWien
    AlexWien about 8 years
    Thanks for the hint to ingnoe multiple sonar cube warnings
  • annedroiid
    annedroiid over 7 years
    The other issue with marking it as a false positive on the interface is that if for whatever reason the project is deleted and added again, all of the flags you have set will be gone.
  • Ortomala Lokni
    Ortomala Lokni about 7 years
    The problem is that Eclipse displays a warning Unsupported @SuppressWarnings("squid:S00112"). You can configure Eclipse to ignore this but then a typo such as in @SuppressWarnings("uncheckedd") will not be detected.
  • Win4ster
    Win4ster about 6 years
    Could you please provide other link to a SonarQube FAQ? I see a login form instead of FAQ
  • Daniel C. Sobral
    Daniel C. Sobral over 5 years
    squid is for the Sonar-sourced Java database (SonarAnalyzer Java). pmd and checkstyle are on other rule repositories.
  • Rich Dougherty
    Rich Dougherty over 4 years
    See docs.codescan.io/hc/en-us/articles/… for docs on @SuppressWarnings and Sonar.
  • PAA
    PAA over 4 years
    @annedroiid - Is there any way if same can be done in pom.xml file ? I've raise question here: stackoverflow.com/questions/57789832/…
  • Gentleman
    Gentleman almost 4 years
    How do we add comments/justification when we suppress?
  • rilaby
    rilaby over 2 years
    Looks like this answer should be marked as accepted.
  • Laloi
    Laloi over 2 years
  • Larry_C
    Larry_C about 2 years
    A space is needed "// NOSONAR" before NOSONAR.