76

I want to suppress the Sonar lint plugin warning for some methods.
This question is not what I want Intellij SonarLint 2.3 - ignore rule.

Currently I have to annotate the method with @SuppressWarnings("all"),which suppresses all warnings.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
aristotll
  • 8,694
  • 6
  • 33
  • 53
  • Well if you don't want to put the suppression in the code or in a rule on Sonar, where do you want it to go? – syncdk Nov 20 '17 at 13:10
  • 1
    @dkanejs Intellij can suppress the warning using annotations like `@SuppressWarnings("specific suppress id")`. – aristotll Nov 20 '17 at 13:43
  • Ah I understand it is what you're asking now. – syncdk Nov 20 '17 at 13:44
  • 1
    https://docs.sonarqube.org/display/PLUG/Java+FAQ#JavaFAQ-SuppressWarnings ? – syncdk Nov 20 '17 at 13:46
  • 2
    You only want to suppress warnings (related to Sonar lint) in a specific method ? You're looking for something to exist like `@SuppressWarnings("sonar")` – syncdk Nov 20 '17 at 13:50

2 Answers2

130

You can use //NOSONAR or @SuppressWarnings() but you need to specify the rule.

From the SonarQube documentation:

The //NOSONAR tag is useful to deactivate all rules at a given line but is not suitable to deactivate all rules (or only a given rule) for all the lines of a method or a class. This is why support for @SuppressWarnings("all") has been added to SonarQube.

SINCE 2.8 of Java Plugin, you can also use @SuppressWarnings annotation with a list of rule keys:

@SuppressWarnings("squid:S2078")

or

@SuppressWarnings({"squid:S2078", "squid:S2076"}).

syncdk
  • 2,820
  • 3
  • 25
  • 31
  • 10
    One thing to note is that IntelliJ will generate @SuppressWarnings("ALL") which SonarLint will ignore - it has to be "all". – Vlad Schnakovszki May 17 '18 at 13:19
  • 3
    How to add comments/justification while suppressing the warning? – Gentleman Aug 07 '20 at 12:55
  • Am I correct to assume there's no such capability for Python as of 2022-12-24? https://docs.sonarqube.org/latest/analyzing-source-code/languages/python/ – huyz Dec 24 '22 at 09:43
0

You can Suppress an inspection in the editor it-self,

enter image description here

Refer to https://www.jetbrains.com/help/idea/disabling-and-enabling-inspections.html, It has various methods to get this done, there are many ways to customize the required behaviors.

prime
  • 14,464
  • 14
  • 99
  • 131