Roslyn: How to tell the analyzer to ignore generated code?

Created on 21 Sep 2020  路  2Comments  路  Source: dotnet/roslyn

Hello, I wrote a custom code analyzer and it runs on my solution code, but it also runs on generated code.
For example when AssemblyInfo.cs files get generated, my analyzer also analyzes these files and throws warnings inside of them. How can I tell my custom analyzer to not analyze generated code?

I know I could add something like this into my .editorconfig:

[*.{AssemblyAttributes.cs,AssemblyInfo.cs}]
generated_code = true
dotnet_diagnostic.***.severity = none
...

But is there a better way to do it?

Area-Analyzers Question Resolution-Answered

Most helpful comment

You can do that using ConfigureGeneratedCodeAnalysis on the AnalysisContext (that is the parameter of the Initialize method):

context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

All 2 comments

You can do that using ConfigureGeneratedCodeAnalysis on the AnalysisContext (that is the parameter of the Initialize method):

context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

Thank you, this is exactly what I needed!

Was this page helpful?
0 / 5 - 0 ratings