Search code examples
c#sonarlintsonarlint-vs

How to ignore Sonar rule to specific line of code in C#?


I use SonarLint. I want the sonar test not to be performed for a specific line. I used // NOSONAR but it didn't work. Does not specify the error number.

SS

The error still appears in SonarLint when I use //NOSONAR. But in the main project (SonarQube) the error no longer appears.


Solution

  • This is Visual Studio suggestion, not SonarLint warning. Also, I believe NOSONAR is not supported in Visual Studio. You can either suppress in source:

    #pragma warning disable IDE0017
    <your code>
    #pragma warning restore IDE0017
    

    Or in suppression file:

    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0017:Simplify object initialization", Justification = "<Pending>", Scope = "member", Target = "~M:<your class and your method>")]