CSharpGuidelines icon indicating copy to clipboard operation
CSharpGuidelines copied to clipboard

AV2210: Invalid warning level

Open bkoelman opened this issue 3 years ago • 2 comments

The rule at https://github.com/dennisdoomen/CSharpGuidelines/blob/master/_rules/2210.md states to use warning level 4, while the highest level is 5 at the moment.

But more importantly, since .NET 5, the warning level is driven by the AnalysisLevel MSBuild property: Microsoft.NET.Sdk.Analyzers.targets (which only applies to SDK-style projects) simply overwrites any explicit warning level with a value that depends on the target framework. For .NET 5, AnalysisLevel defaults to "5" (which maps to warning level 5) and for .NET 6 it defaults to "latest" (which maps to warning level 6, which does not actually exist, so csc.exe uses warning level 5 instead). Because of this, the recommendation to use warning level 9999 has no effect, except for .NET Framework projects that use the legacy .csproj format.

For more background info, see the links in the description of commit https://github.com/bkoelman/CSharpGuidelinesAnalyzer/commit/90174eafccf08514c856a1a0a1fa6adcfc15521a.

Since .NET 6, AnalysisLevel also affects which built-in analyzers (formerly FxCop) are activated (see https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevelcategory). We could advise on using (some of) those in AV2210, but they aren't widely used yet and are still quite buggy. Turning on all of them usually leads to many false positives and/or crashes.

So to keep it simple, I think the rule text should be changed from "use Warning Level 4 for the C# compiler" to "use the highest available warning Level for the C# compiler" (which is what you'll get by default).

bkoelman avatar Feb 02 '22 16:02 bkoelman

@dennisdoomen Awaiting feedback before creating a PR for this.

bkoelman avatar Feb 03 '22 11:02 bkoelman

I still have to wrap my head around the consequences of this. I probably should try myself on FluentAssertions and see what happens.

dennisdoomen avatar Feb 03 '22 12:02 dennisdoomen