Help: Suppressing Roslyn rules in generated code when TreatWarningAsErrors is true
nschuessler opened this issue · 3 comments
We wrote a rule that flags string.Format
when there are 4 or fewer arguments (because string.Concat
or the $"" format which is string.Concat
underneath are better options).
Unfortunately, System.Text.Json
generates serialization code with an error message that violates this rule. With TreatWarningsAsErrors
enabled this fails the build.
There are a few scenarios we've tried to suppress it, but it appears to be ignored by .editor.config
and the globalsuppressions system (I didn't personally verify this one -- verified by customer of our rules).
The only way to suppress it is on the command line, globally, for the whole project.
Q1: What is the most supported way to do fine tuned configuration of suppressions? I thought .editor.config
superseeded .rulesets
but maybe that's not the case?
Q2: Should the analyzer itself be looking at settings and not supporting generated code if it sees something in .editor.config
(i.e. the following initialization code should have a conditional on the generated code analyis:
public override void Initialize(AnalysisContext context)
{
// This allows parallel analysis of methods and types
// and requires proper synchronization if needed in the analyzer
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(this.OnStartCompilation);
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
}
Q3: How does // <autogenerated />
and #pragma warning disable
fit in with suppression (i.e. if we could inject the latter, the former is already in the generated code and doesn't affect anything).
Q4: We found the following issue on this matter that seems related and resolved in .Net 8, but still seems to repro / doesn't seem to help in the latest bits (Roslyn 4.10).
Adding <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
to the file with TreatWarningsAsErrors
as either true or false doesn't seem to have any effect. What should we be doing here?
Project file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Contoso.OurAnalyzers" Version="311.21.1022.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>
Editor config entries:
dotnet_diagnostic.CPR105.severity = none
dotnet_diagnostic.CPR138.severity = none
dotnet_diagnostic.CPR126.severity = none
Why is this analyzer using GeneratedCodeAnalysisFlags.ReportDiagnostics
? As a performance analyzer, it seems like it should not be reporting diagnostics in generated code, as there is no way for a user to address those warnings.
I was either following an example, or when I was writing tests against the code library it was failing to pass in the generated scenario. There is also I think a analyzer rule for Roslyn that flag it if you don't specify the initialization that may have fiddled with it as a result.
In any case, what is the scenario where generated code should be analyzed?
Should the default be for most analyzers .None
then?