Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit 90174ea

Browse files
Bart KoelmanBart Koelman
authored andcommitted
AV2210: Removed check on warning level, because since .NET 5, the default warning level depends on the target framework and is driven by the AnalysisLevel MSBuild property (see https://devblogs.microsoft.com/dotnet/automatically-find-latent-bugs-in-your-code-with-net-5/ and https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevel), which analyzers do not have access to by default (it would require user intervention as described at https://stackoverflow.com/questions/69215975/roslyn-codefix-msbuild-properties-metadata-and-unit-testing). So to get a good out-of-the-box experience, we'd need to duplicate the logic from Microsoft.NET.Sdk.Analyzers.targets, which only applies to SDK-style projects and is subject to change in each .NET release.
Setting the warning level to 9999 as recommended in https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel does not work anymore since .NET 5, because it gets overwritten at build time by Microsoft.NET.Sdk.Analyzers.targets (dotnet/sdk#21599).
1 parent 98f3f39 commit 90174ea

4 files changed

Lines changed: 13 additions & 55 deletions

File tree

docs/Overview.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ This analyzer reports when:
162162
- a comparison with a nullable value type contains a redundant null check.
163163

164164
### [AV2210](https://github.com/dennisdoomen/CSharpGuidelines/blob/7a66f7468da6ce1477753a02e416e04bc9a44e45/_pages/2200_FrameworkGuidelines.md#av2210): Build with the highest warning level ![](/images/warn.png "severity: warning")
165-
This analyzer reports when:
166-
- the compiler warning level is below [9999](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel) in the build settings
167-
- warnings are not treated as errors in the build settings.
165+
This analyzer reports when warnings are not treated as errors in the build settings.
168166

169167
Note: this analyzer requires [Full Solution Analysis](/docs/Full%20Solution%20Analysis.md) enabled.
170168

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Framework/BuildWithTheHighestWarningLevelSpecs.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,27 @@ public sealed class BuildWithTheHighestWarningLevelSpecs : CSharpGuidelinesAnaly
1010
protected override string DiagnosticId => BuildWithTheHighestWarningLevelAnalyzer.DiagnosticId;
1111

1212
[Fact]
13-
internal void When_warning_level_is_9999_with_warnings_as_errors_it_must_be_skipped()
13+
internal void When_compiling_with_warnings_as_errors_it_must_be_skipped()
1414
{
1515
// Arrange
1616
ParsedSourceCode source = new TypeSourceCodeBuilder()
17-
.CompileAtWarningLevel(9999)
1817
.CompileWithWarningAsError()
1918
.Build();
2019

2120
// Act and assert
2221
VerifyGuidelineDiagnostic(source);
2322
}
2423

25-
[Fact]
26-
internal void When_warning_level_is_below_9999_it_must_be_reported()
27-
{
28-
// Arrange
29-
ParsedSourceCode source = new TypeSourceCodeBuilder()
30-
.CompileAtWarningLevel(5)
31-
.CompileWithWarningAsError()
32-
.Build();
33-
34-
// Act and assert
35-
VerifyGuidelineDiagnostic(source,
36-
"Build with warning level 9999");
37-
}
38-
3924
[Fact]
4025
internal void When_compiling_with_warnings_not_as_errors_it_must_be_reported()
4126
{
4227
// Arrange
4328
ParsedSourceCode source = new TypeSourceCodeBuilder()
44-
.CompileAtWarningLevel(9999)
4529
.Build();
4630

4731
// Act and assert
4832
VerifyGuidelineDiagnostic(source,
49-
"Build with -warnaserror");
33+
"Pass -warnaserror to the compiler or add <TreatWarningsAsErrors>True</TreatWarningsAsErrors> to your project file");
5034
}
5135

5236
protected override DiagnosticAnalyzer CreateAnalyzer()

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/TestDataBuilders/SourceCodeBuilderExtensions.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ public static TBuilder WithOutputKind<TBuilder>([NotNull] this TBuilder source,
8383
return source;
8484
}
8585

86-
[NotNull]
87-
public static TBuilder CompileAtWarningLevel<TBuilder>([NotNull] this TBuilder source, int warningLevel)
88-
where TBuilder : SourceCodeBuilder
89-
{
90-
Guard.NotNull(source, nameof(source));
91-
92-
source.Editor.UpdateTestContext(context => context.CompileAtWarningLevel(warningLevel));
93-
94-
return source;
95-
}
96-
9786
[NotNull]
9887
public static TBuilder CompileWithWarningAsError<TBuilder>([NotNull] this TBuilder source)
9988
where TBuilder : SourceCodeBuilder

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Framework/BuildWithTheHighestWarningLevelAnalyzer.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,27 @@ namespace CSharpGuidelinesAnalyzer.Rules.Framework
99
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1010
public sealed class BuildWithTheHighestWarningLevelAnalyzer : DiagnosticAnalyzer
1111
{
12-
private const string Title = "Compiler warnings are not treated as errors or warning level is too low";
13-
private const string WarningLevelMessageFormat = "Build with warning level 9999";
14-
private const string WarningAsErrorMessageFormat = "Build with -warnaserror";
15-
private const string Description = "Build with the highest warning level.";
12+
private const string Title = "Compiler warnings are not treated as errors";
1613

17-
// The highest warning level used to be 4, until C# 9 added warning level 5. Microsoft now recommends to use 9999, which should include all
18-
// future warning levels. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel.
19-
private const int MaxWarningLevel = 9999;
14+
private const string MessageFormat =
15+
"Pass -warnaserror to the compiler or add <TreatWarningsAsErrors>True</TreatWarningsAsErrors> to your project file";
16+
17+
private const string Description = "Build with the highest warning level";
2018

2119
public const string DiagnosticId = "AV2210";
2220

2321
[NotNull]
2422
private static readonly AnalyzerCategory Category = AnalyzerCategory.Framework;
2523

2624
[NotNull]
27-
private static readonly DiagnosticDescriptor WarningLevelRule = new DiagnosticDescriptor(DiagnosticId, Title, WarningLevelMessageFormat,
28-
Category.DisplayName, DiagnosticSeverity.Warning, true, Description, Category.GetHelpLinkUri(DiagnosticId));
29-
30-
[NotNull]
31-
private static readonly DiagnosticDescriptor WarningAsErrorRule = new DiagnosticDescriptor(DiagnosticId, Title, WarningAsErrorMessageFormat,
32-
Category.DisplayName, DiagnosticSeverity.Warning, true, Description, Category.GetHelpLinkUri(DiagnosticId));
25+
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category.DisplayName,
26+
DiagnosticSeverity.Warning, true, Description, Category.GetHelpLinkUri(DiagnosticId));
3327

3428
[NotNull]
3529
private static readonly Action<CompilationAnalysisContext> AnalyzeCompilationOptionsAction = AnalyzeCompilationOptions;
3630

3731
[ItemNotNull]
38-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(WarningLevelRule, WarningAsErrorRule);
32+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
3933

4034
public override void Initialize([NotNull] AnalysisContext context)
4135
{
@@ -47,16 +41,9 @@ public override void Initialize([NotNull] AnalysisContext context)
4741

4842
private static void AnalyzeCompilationOptions(CompilationAnalysisContext context)
4943
{
50-
CompilationOptions options = context.Compilation.Options;
51-
52-
if (options.GeneralDiagnosticOption != ReportDiagnostic.Error)
53-
{
54-
context.ReportDiagnostic(Diagnostic.Create(WarningAsErrorRule, Location.None));
55-
}
56-
57-
if (options.WarningLevel < MaxWarningLevel)
44+
if (context.Compilation.Options.GeneralDiagnosticOption != ReportDiagnostic.Error)
5845
{
59-
context.ReportDiagnostic(Diagnostic.Create(WarningLevelRule, Location.None));
46+
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None));
6047
}
6148
}
6249
}

0 commit comments

Comments
 (0)