Skip to content

Commit 05b42b7

Browse files
authored
Merge pull request #124 from bkoelman/warning-level
Updated AV2210 to require warning level 9999
2 parents f25ab40 + 2d8bba9 commit 05b42b7

File tree

5 files changed

+20
-55
lines changed

5 files changed

+20
-55
lines changed

docs/Overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ This analyzer reports when:
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")
165165
This analyzer reports when:
166-
- the compiler warning level is below 4 in the build settings
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
167167
- warnings are not treated as errors in the build settings.
168168

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

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

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

1212
[Fact]
13-
internal void When_warning_level_is_set_to_four_it_must_be_skipped()
13+
internal void When_warning_level_is_9999_with_warnings_as_errors_it_must_be_skipped()
1414
{
1515
// Arrange
1616
ParsedSourceCode source = new TypeSourceCodeBuilder()
17-
.CompileAtWarningLevel(4)
17+
.CompileAtWarningLevel(9999)
1818
.CompileWithWarningAsError()
1919
.Build();
2020

@@ -23,66 +23,25 @@ internal void When_warning_level_is_set_to_four_it_must_be_skipped()
2323
}
2424

2525
[Fact]
26-
internal void When_warning_level_is_set_to_three_it_must_be_reported()
26+
internal void When_warning_level_is_below_9999_it_must_be_reported()
2727
{
2828
// Arrange
2929
ParsedSourceCode source = new TypeSourceCodeBuilder()
30-
.CompileAtWarningLevel(3)
30+
.CompileAtWarningLevel(5)
3131
.CompileWithWarningAsError()
3232
.Build();
3333

3434
// Act and assert
3535
VerifyGuidelineDiagnostic(source,
36-
"Build with warning level 4.");
36+
"Build with warning level 9999.");
3737
}
3838

3939
[Fact]
40-
internal void When_warning_level_is_set_to_two_it_must_be_reported()
41-
{
42-
// Arrange
43-
ParsedSourceCode source = new TypeSourceCodeBuilder()
44-
.CompileAtWarningLevel(2)
45-
.CompileWithWarningAsError()
46-
.Build();
47-
48-
// Act and assert
49-
VerifyGuidelineDiagnostic(source,
50-
"Build with warning level 4.");
51-
}
52-
53-
[Fact]
54-
internal void When_warning_level_is_set_to_one_it_must_be_reported()
55-
{
56-
// Arrange
57-
ParsedSourceCode source = new TypeSourceCodeBuilder()
58-
.CompileAtWarningLevel(1)
59-
.CompileWithWarningAsError()
60-
.Build();
61-
62-
// Act and assert
63-
VerifyGuidelineDiagnostic(source,
64-
"Build with warning level 4.");
65-
}
66-
67-
// Note: at warning level 0, analyzers do not even run. So a test for that is omitted here.
68-
69-
[Fact]
70-
internal void When_compiling_with_warnings_as_errors_it_must_be_skipped()
71-
{
72-
// Arrange
73-
ParsedSourceCode source = new TypeSourceCodeBuilder()
74-
.CompileWithWarningAsError()
75-
.Build();
76-
77-
// Act and assert
78-
VerifyGuidelineDiagnostic(source);
79-
}
80-
81-
[Fact]
82-
internal void When_compiling_with_warnings_not_as_errors_it_must_be_skipped()
40+
internal void When_compiling_with_warnings_not_as_errors_it_must_be_reported()
8341
{
8442
// Arrange
8543
ParsedSourceCode source = new TypeSourceCodeBuilder()
44+
.CompileAtWarningLevel(9999)
8645
.Build();
8746

8847
// Act and assert

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ namespace CSharpGuidelinesAnalyzer.Rules.Framework
1010
public sealed class BuildWithTheHighestWarningLevelAnalyzer : DiagnosticAnalyzer
1111
{
1212
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 4.";
13+
private const string WarningLevelMessageFormat = "Build with warning level 9999.";
1414
private const string WarningAsErrorMessageFormat = "Build with -warnaserror.";
1515
private const string Description = "Build with the highest warning level.";
1616

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;
20+
1721
public const string DiagnosticId = "AV2210";
1822

1923
[NotNull]
@@ -50,7 +54,7 @@ private static void AnalyzeCompilationOptions(CompilationAnalysisContext context
5054
context.ReportDiagnostic(Diagnostic.Create(WarningAsErrorRule, Location.None));
5155
}
5256

53-
if (options.WarningLevel < 4)
57+
if (options.WarningLevel < MaxWarningLevel)
5458
{
5559
context.ReportDiagnostic(Diagnostic.Create(WarningLevelRule, Location.None));
5660
}

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Framework/FavorAsyncAwaitOverTaskContinuationAnalyzer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,15 @@ public TaskTypeInfo([NotNull] Compilation compilation)
100100
GenericTaskType = KnownTypes.SystemThreadingTasksTaskT(compilation);
101101
TaskType = KnownTypes.SystemThreadingTasksTask(compilation);
102102

103-
ContinueWithMethodGroup = GetTaskContinueWithMethodGroup();
103+
ContinueWithMethodGroup = GetTaskContinueWithMethodGroup(TaskType, GenericTaskType);
104104
}
105105

106106
[ItemNotNull]
107-
private ImmutableArray<ISymbol> GetTaskContinueWithMethodGroup()
107+
private static ImmutableArray<ISymbol> GetTaskContinueWithMethodGroup([CanBeNull] INamedTypeSymbol taskType,
108+
[CanBeNull] INamedTypeSymbol genericTaskType)
108109
{
109-
ImmutableArray<ISymbol> taskContinueWithMethodGroup = TaskType?.GetMembers("ContinueWith") ?? ImmutableArray<ISymbol>.Empty;
110-
ImmutableArray<ISymbol> genericTaskContinueWithMethodGroup = GenericTaskType?.GetMembers("ContinueWith") ?? ImmutableArray<ISymbol>.Empty;
110+
ImmutableArray<ISymbol> taskContinueWithMethodGroup = taskType?.GetMembers("ContinueWith") ?? ImmutableArray<ISymbol>.Empty;
111+
ImmutableArray<ISymbol> genericTaskContinueWithMethodGroup = genericTaskType?.GetMembers("ContinueWith") ?? ImmutableArray<ISymbol>.Empty;
111112

112113
return taskContinueWithMethodGroup.Union(genericTaskContinueWithMethodGroup).ToImmutableArray();
113114
}

src/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<LangVersion>8.0</LangVersion>
5+
<WarningLevel>9999</WarningLevel>
56
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
67

78
<!-- Required for code coverage -->

0 commit comments

Comments
 (0)