Home > Software design >  C#, Blazor, VS: How to suppress IDE0051 and IDE0052 for all *.razor.cs and *.cshtml.cs files?
C#, Blazor, VS: How to suppress IDE0051 and IDE0052 for all *.razor.cs and *.cshtml.cs files?

Time:06-03

For various reasons unused private members are in fact used in those files. The warning for that is annoying.

I want that warning in normal code files, but not for Razor.

There's a magic .editorconfig file that can suppress certain warnings for either a directory, or even file extensions.

The problem is, I don't know the option to disable warnings for IDE0051 and IDE0052. Those rules don't have names usually used in .editorconfig file. But maybe there is an undocumented option to suppress warnings by the code like "IDE0051"?

I'm not interested in analyzing each file separately, that's the whole point of it.

How to disable specific warnings for a file extension?

My workaround is this put into .editorconfig file:

[*.razor.cs]
generated_code = true

[*.chtml.cs]
generated_code = true

I don't like it because it disables ALL warnings, and I would prefer that most of them stayed. BTW, it is not generated code.

The root of the problem is that Blazor access some members but it is not done in plain C# for the analyzer or compiler to see. These are members that, according to the VS, have no references. In fact - there are implicit references. If I remove the members the code would compile, but the program will break on runtime.

I know, I can just disable those warnings on the project level. But again, I'm looking how to do that only for a specific file extension, not for all C# files in the project.

CodePudding user response:

[*.razor.cs]
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.IDE0052.severity = none

[*.chtml.cs]
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.IDE0052.severity = none
  • Related