Search code examples
c#warningssuppress-warningssonarclouddotnet-sdk

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


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.


Solution

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