How to suppress warning for a specific method with Intellij SonarLint plugin
Asked Answered
M

3

88

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.

Macfarlane answered 18/11, 2017 at 18:23 Comment(5)
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?Eth
@dkanejs Intellij can suppress the warning using annotations like @SuppressWarnings("specific suppress id").Macfarlane
Ah I understand it is what you're asking now.Eth
docs.sonarqube.org/display/PLUG/… ?Eth
You only want to suppress warnings (related to Sonar lint) in a specific method ? You're looking for something to exist like @SuppressWarnings("sonar")Eth
E
149

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

Eth answered 21/11, 2017 at 11:21 Comment(3)
One thing to note is that IntelliJ will generate @SuppressWarnings("ALL") which SonarLint will ignore - it has to be "all".Bernard
How to add comments/justification while suppressing the warning?Barley
Am I correct to assume there's no such capability for Python as of 2022-12-24? docs.sonarqube.org/latest/analyzing-source-code/languages/…Semipalmate
C
1

This annotation for scala method helped me

object Solution {
    @SuppressWarnings(Array("scala:S3776"))
    def threeSum(nums: Array[Int]): List[List[Int]] = {
        val sortedNums = nums.sorted
Chauffer answered 29/10, 2023 at 14:17 Comment(0)
S
0

UPDATE : This submenu doesn't exist on SonarLint warnings (IDEA 2020.3.3, SonarLint 4.14.2.28348).

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.

Sneck answered 13/6, 2020 at 6:5 Comment(3)
That submenu doesn't exist on SonarLint warnings (IDEA 2020.3.3, SonarLint 4.14.2.28348).Morocco
or maybe exists for Java files but not for Kotlin filesPinery
This "Suppress for statement" is for an IntelliJ "inspection", not from the SonarLint plugin.Arginine

© 2022 - 2024 — McMap. All rights reserved.