Skip to content

Commit

Permalink
AV2210: Removed check on warning level, because since .NET 5, the def…
Browse files Browse the repository at this point in the history
…ault 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).
  • Loading branch information
renamorales309 committed Feb 2, 2022
1 parent 42da7c5 commit 23aec9e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 55 deletions.
4 changes: 1 addition & 3 deletions docs/Overview.md
Expand Up @@ -162,9 +162,7 @@ This analyzer reports when:
- a comparison with a nullable value type contains a redundant null check.

### [AV2210](https://github.com/dennisdoomen/CSharpGuidelines/blob/7a66f7468da6ce1477753a02e416e04bc9a44e45/_pages/2200_FrameworkGuidelines.md#av2210): Build with the highest warning level ![](/images/warn.png "severity: warning")
This analyzer reports when:
- 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
- warnings are not treated as errors in the build settings.
This analyzer reports when warnings are not treated as errors in the build settings.

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

Expand Down
Expand Up @@ -10,43 +10,27 @@ public sealed class BuildWithTheHighestWarningLevelSpecs : CSharpGuidelinesAnaly
protected override string DiagnosticId => BuildWithTheHighestWarningLevelAnalyzer.DiagnosticId;

[Fact]
internal void When_warning_level_is_9999_with_warnings_as_errors_it_must_be_skipped()
internal void When_compiling_with_warnings_as_errors_it_must_be_skipped()
{
// Arrange
ParsedSourceCode source = new TypeSourceCodeBuilder()
.CompileAtWarningLevel(9999)
.CompileWithWarningAsError()
.Build();

// Act and assert
VerifyGuidelineDiagnostic(source);
}

[Fact]
internal void When_warning_level_is_below_9999_it_must_be_reported()
{
// Arrange
ParsedSourceCode source = new TypeSourceCodeBuilder()
.CompileAtWarningLevel(5)
.CompileWithWarningAsError()
.Build();

// Act and assert
VerifyGuidelineDiagnostic(source,
"Build with warning level 9999");
}

[Fact]
internal void When_compiling_with_warnings_not_as_errors_it_must_be_reported()
{
// Arrange
ParsedSourceCode source = new TypeSourceCodeBuilder()
.CompileAtWarningLevel(9999)
.Build();

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

protected override DiagnosticAnalyzer CreateAnalyzer()
Expand Down
Expand Up @@ -83,17 +83,6 @@ public static TBuilder WithOutputKind<TBuilder>([NotNull] this TBuilder source,
return source;
}

[NotNull]
public static TBuilder CompileAtWarningLevel<TBuilder>([NotNull] this TBuilder source, int warningLevel)
where TBuilder : SourceCodeBuilder
{
Guard.NotNull(source, nameof(source));

source.Editor.UpdateTestContext(context => context.CompileAtWarningLevel(warningLevel));

return source;
}

[NotNull]
public static TBuilder CompileWithWarningAsError<TBuilder>([NotNull] this TBuilder source)
where TBuilder : SourceCodeBuilder
Expand Down
Expand Up @@ -9,33 +9,27 @@ namespace CSharpGuidelinesAnalyzer.Rules.Framework
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class BuildWithTheHighestWarningLevelAnalyzer : DiagnosticAnalyzer
{
private const string Title = "Compiler warnings are not treated as errors or warning level is too low";
private const string WarningLevelMessageFormat = "Build with warning level 9999";
private const string WarningAsErrorMessageFormat = "Build with -warnaserror";
private const string Description = "Build with the highest warning level.";
private const string Title = "Compiler warnings are not treated as errors";

// 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
// future warning levels. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel.
private const int MaxWarningLevel = 9999;
private const string MessageFormat =
"Pass -warnaserror to the compiler or add <TreatWarningsAsErrors>True</TreatWarningsAsErrors> to your project file";

private const string Description = "Build with the highest warning level";

public const string DiagnosticId = "AV2210";

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

[NotNull]
private static readonly DiagnosticDescriptor WarningLevelRule = new DiagnosticDescriptor(DiagnosticId, Title, WarningLevelMessageFormat,
Category.DisplayName, DiagnosticSeverity.Warning, true, Description, Category.GetHelpLinkUri(DiagnosticId));

[NotNull]
private static readonly DiagnosticDescriptor WarningAsErrorRule = new DiagnosticDescriptor(DiagnosticId, Title, WarningAsErrorMessageFormat,
Category.DisplayName, DiagnosticSeverity.Warning, true, Description, Category.GetHelpLinkUri(DiagnosticId));
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category.DisplayName,
DiagnosticSeverity.Warning, true, Description, Category.GetHelpLinkUri(DiagnosticId));

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

[ItemNotNull]
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(WarningLevelRule, WarningAsErrorRule);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

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

private static void AnalyzeCompilationOptions(CompilationAnalysisContext context)
{
CompilationOptions options = context.Compilation.Options;

if (options.GeneralDiagnosticOption != ReportDiagnostic.Error)
{
context.ReportDiagnostic(Diagnostic.Create(WarningAsErrorRule, Location.None));
}

if (options.WarningLevel < MaxWarningLevel)
if (context.Compilation.Options.GeneralDiagnosticOption != ReportDiagnostic.Error)
{
context.ReportDiagnostic(Diagnostic.Create(WarningLevelRule, Location.None));
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None));
}
}
}
Expand Down

0 comments on commit 23aec9e

Please sign in to comment.