8

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.

uyusuk
  • 311
  • 1
  • 2
  • 15

2 Answers2

9

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>")]
Peska
  • 3,980
  • 3
  • 23
  • 40
1

Another way is to use SuppressMessageAttribute with methods or classes. It allows suppressing a warning in only certain parts of your project or file. For example, you can suppress a warning in a method or class scope:

[System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Style",
    "IDE0017:Simplify object initialization",
    Justification = "<Pending>")]
void SetName(string name)
{
    var person = new Person();
    person.Name = name;
}

or

[System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Style",
    "IDE0017:Simplify object initialization",
    Justification = "<Pending>")]
class MyClass
{
}
Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16