Is there a possibility to suppress warnings of sonarcloud in source code?

11.7k Views Asked by At

I am facing an issue with some sonarcloud warnings that will not be fixed in the nearest future (and should be disabled for now).

The warnings are raised by sonarcloud during build on CI. There are no any local analysers. Is there a way to suppress some specific warnings in source code on project, file, class and/or method levels?

The official documentation is not of much help, at least I did not manage to find the solution.

Does sonarcloud respect some

#pragma warning disable S3881

or other attribute based approach or some global settings.

3

There are 3 best solutions below

1
On BEST ANSWER

There are multiple ways of doing so (at least in java, but i think it might help you anyways)

  1. the GUI way - where you can define a Key and a file Pattern to be matched for a specific rule. This allows you to eg. exclude tests or folders for a rule enter image description here

  2. The comment way (java version) - in Java you can ignore whole lines from the scanner by trailing them with //NOSONAR <optional reason> - i assume that this also works within C# (but please verify, if suitable and let us know)

  3. The Annoation way (java version) - but looks similar to #pragma warning disable

    @SuppressWarnings("squid:S2068")
    

    is the way, how we can disable a rule for a codeblock in Java - so i would assume your approache with #pragma warning disable S3881 is the right direction but it should be #pragma warning disable csharpsquid:S3881.

As said i am not a c# developer and not really familiar with that language, but i hope the java ways and the gui way, will help you move into the right direction.

1
On

Yes SonarCloud respects the #pragma syntax:

#pragma warning disable S3881
// The statement that fails validation
#pragma warning restore S3881

Where you replace S3881 with the number that fails for you.

0
On

The #pragma syntax works, but I find it ugly. I prefer adding the attribute, and I found by experimenting that the following one works:

[SuppressMessage("SonarLint", "S125", Justification = "Ignored intentionally as a demo")]
public void When_X_Expect_Y()
{
    //string x = "trigger 'S125 - commented out code' issue in SonarLint";
}

The first argument of SuppressMessage doesn't seem to matter. The second one is crucial and effective.