Abhijeet
Abhijeet

Reputation: 13856

How to Ignore specific file from Visual Studio Warnings Analyzer?

Is there any way to suppress all error codes in one specific file?

Surprisingly I couldn't find this. This is pretty common scenario isn't it?

Documentation describes it seems:

  1. we have to disable it line by line or
  2. globally across all files.

https://learn.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2022#:~:text=You%20can%20suppress%20violations%20in,can%20use%20the%20SuppressMessage%20attribute.

Upvotes: 7

Views: 2752

Answers (5)

Jervis
Jervis

Reputation: 124

  1. use .editorconfig file:

    [**/Migrations/**.cs]
    generated_code = true
    dotnet_analyzer_diagnostic.severity = none
    
  2. or use .csproj:

    <Project>
        <ItemGroup>
        <Compile Remove="Migrations\**\*.cs" />
        <None Include="Migrations\**\*.cs" />
        </ItemGroup>  
    </Project>
    

Upvotes: 1

palapapa
palapapa

Reputation: 949

For me, I wanted to disable the analyzer for EF Core migration .cs files. Somehow using dotnet_analyzer_diagnostic.severity = none didn't work. I had to use:

[Migrations/**]
generated_code = true

This is documented here.

Upvotes: 2

pfx
pfx

Reputation: 23244

Make a section in the .editorconfig file for that given class; specify its file name [{YourClass.cs}] or path [{Folder/YourClass.cs}]in a section and set the severity for all rules to none.

See the All rules scope in the documentation.

[{YourClass.cs}]
dotnet_analyzer_diagnostic.severity = none

Upvotes: 7

mohammadAli
mohammadAli

Reputation: 445

Use #pragma warning ( push ), then #pragma warning ( disable ), then put your code, then use #pragma warning ( pop ) as described here:

Replace warningCode below with the real codes

 #pragma warning( push )
 #pragma warning( disable : WarningCode)
 //code with warning
 #pragma warning( pop )

Upvotes: 0

PMF
PMF

Reputation: 17195

You can add this snippet at the very beginning of the file:

// <autogenerated />

This suppresses most warnings on that file.

Upvotes: 6

Related Questions