Skip to content

Commit

Permalink
Ensure that warnaserror works identically for warnings configured in …
Browse files Browse the repository at this point in the history
…ruleset and global config

1. Fixes #43051
2. Ensures consistent behavior between ruleset and global config

Enables users to migrate away from rulesets when they desire to have rules configured as warnings by default and bump it to errors in CI with warnaserror. They cannot do this via editorconfig settings as editorconfig always overrides command line options (nowarn and warnaserror).
  • Loading branch information
mavasani committed Sep 15, 2020
1 parent 0be240d commit 8c54593
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ internal static ReportDiagnostic GetDiagnosticReport(
{
// 4. Global analyzer config level
isSpecified = true;

// '/warnaserror' should promote warnings configured in global analyzer config to error.
if (report == ReportDiagnostic.Warn && generalDiagnosticOption == ReportDiagnostic.Error)
{
report = ReportDiagnostic.Error;
}
}
else
{
Expand Down
38 changes: 38 additions & 0 deletions src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12814,6 +12814,44 @@ void M()
VerifyOutput(dir, src, additionalFlags: new[] { "/warnaserror+", "/analyzerconfig:" + globalConfig.Path }, includeCurrentAssemblyAsAnalyzerReference: false);
}

[Theory, CombinatorialData]
[WorkItem(43051, "https://github.com/dotnet/roslyn/issues/43051")]
public void WarnAsErrorIsRespectedForForWarningsConfiguredInRulesetOrGlobalConfig(bool useGlobalConfig)
{
var dir = Temp.CreateDirectory();
var src = dir.CreateFile("temp.cs").WriteAllText(@"
class C
{
void M()
{
label1:;
}
}");
var additionalFlags = new[] { "/warnaserror+" };
if (useGlobalConfig)
{
var globalConfig = dir.CreateFile(".globalconfig").WriteAllText($@"
is_global = true
dotnet_diagnostic.CS0164.severity = warning;
");
additionalFlags = additionalFlags.Append("/analyzerconfig:" + globalConfig.Path).ToArray();
}
else
{
string ruleSetSource = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RuleSet Name=""Ruleset1"" Description=""Test"" ToolsVersion=""15.0"">
<Rules AnalyzerId=""Compiler"" RuleNamespace=""Compiler"">
<Rule Id=""CS0164"" Action=""Warning"" />
</Rules>
</RuleSet>
";
_ = dir.CreateFile("Rules.ruleset").WriteAllText(ruleSetSource);
additionalFlags = additionalFlags.Append("/ruleset:Rules.ruleset").ToArray();
}

VerifyOutput(dir, src, additionalFlags: additionalFlags, expectedErrorCount: 1, includeCurrentAssemblyAsAnalyzerReference: false);
}

[Fact]
[WorkItem(44087, "https://github.com/dotnet/roslyn/issues/44804")]
public void GlobalAnalyzerConfigSectionsOverrideCommandLine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
ElseIf syntaxTreeOptions IsNot Nothing AndAlso syntaxTreeOptions.TryGetGlobalDiagnosticValue(id, cancellationToken, report) Then
' 4. Global analyzer config level
isSpecified = True

' '/warnaserror' should promote warnings configured in global analyzer config to error.
If report = ReportDiagnostic.Warn AndAlso generalDiagnosticOption = ReportDiagnostic.Error Then
report = ReportDiagnostic.Error
End If
Else
report = If(isEnabledByDefault, ReportDiagnostic.Default, ReportDiagnostic.Suppress)
End If
Expand Down
33 changes: 33 additions & 0 deletions src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -10091,6 +10091,39 @@ dotnet_diagnostic.BC42024.severity = warning;
VerifyOutput(dir, src, includeCurrentAssemblyAsAnalyzerReference:=False, expectedWarningCount:=1, additionalFlags:={"/nowarn:BC42024", globalOption, specificOption})
End Sub

<Theory, CombinatorialData>
<WorkItem(43051, "https://github.com/dotnet/roslyn/issues/43051")>
Public Sub WarnAsErrorIsRespectedForForWarningsConfiguredInRulesetOrGlobalConfig(useGlobalConfig As Boolean)
Dim dir = Temp.CreateDirectory()
Dim src = dir.CreateFile("temp.vb").WriteAllText("
Class C
Private Sub M()
Dim a As String
End Sub
End Class")
Dim additionalFlags = {"/warnaserror+"}

If useGlobalConfig Then
Dim globalConfig = dir.CreateFile(".globalconfig").WriteAllText($"
is_global = true
dotnet_diagnostic.BC42024.severity = warning;
")
additionalFlags = additionalFlags.Append("/analyzerconfig:" & globalConfig.Path).ToArray()
Else
Dim ruleSetSource As String = "<?xml version=""1.0"" encoding=""utf-8""?>
<RuleSet Name=""Ruleset1"" Description=""Test"" ToolsVersion=""15.0"">
<Rules AnalyzerId=""Compiler"" RuleNamespace=""Compiler"">
<Rule Id=""BC42024"" Action=""Warning"" />
</Rules>
</RuleSet>
"
dir.CreateFile("Rules.ruleset").WriteAllText(ruleSetSource)
additionalFlags = additionalFlags.Append("/ruleset:Rules.ruleset").ToArray()
End If

VerifyOutput(dir, src, additionalFlags:=additionalFlags, expectedErrorCount:=1, includeCurrentAssemblyAsAnalyzerReference:=False)
End Sub

<Fact>
<WorkItem(44087, "https://github.com/dotnet/roslyn/issues/44804")>
Public Sub GlobalAnalyzerConfigSpecificDiagnosticOptionsOverrideGeneralCommandLineOptions()
Expand Down

0 comments on commit 8c54593

Please sign in to comment.