From 8c545930d3d1e552fffb14e9a77c6c8cb84ae825 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Tue, 15 Sep 2020 09:28:52 -0700 Subject: [PATCH] Ensure that warnaserror works identically for warnings configured in 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). --- .../Compilation/CSharpDiagnosticFilter.cs | 6 +++ .../Test/CommandLine/CommandLineTests.cs | 38 +++++++++++++++++++ .../VisualBasicDiagnosticFilter.vb | 5 +++ .../Test/CommandLine/CommandLineTests.vb | 33 ++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/src/Compilers/CSharp/Portable/Compilation/CSharpDiagnosticFilter.cs b/src/Compilers/CSharp/Portable/Compilation/CSharpDiagnosticFilter.cs index 203c2863e8dc2..77a865c800ffa 100644 --- a/src/Compilers/CSharp/Portable/Compilation/CSharpDiagnosticFilter.cs +++ b/src/Compilers/CSharp/Portable/Compilation/CSharpDiagnosticFilter.cs @@ -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 { diff --git a/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs b/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs index c9d0e396b5c31..a22d8aa6875d8 100644 --- a/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs +++ b/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs @@ -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 = @" + + + + + +"; + _ = 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() diff --git a/src/Compilers/VisualBasic/Portable/Compilation/VisualBasicDiagnosticFilter.vb b/src/Compilers/VisualBasic/Portable/Compilation/VisualBasicDiagnosticFilter.vb index 5f3656b187db7..e7fffec945098 100644 --- a/src/Compilers/VisualBasic/Portable/Compilation/VisualBasicDiagnosticFilter.vb +++ b/src/Compilers/VisualBasic/Portable/Compilation/VisualBasicDiagnosticFilter.vb @@ -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 diff --git a/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb b/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb index 3caaa9bf5ffc7..4f6c27e3d335e 100644 --- a/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb +++ b/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb @@ -10091,6 +10091,39 @@ dotnet_diagnostic.BC42024.severity = warning; VerifyOutput(dir, src, includeCurrentAssemblyAsAnalyzerReference:=False, expectedWarningCount:=1, additionalFlags:={"/nowarn:BC42024", globalOption, specificOption}) End Sub + + + 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 = " + + + + + +" + 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 + Public Sub GlobalAnalyzerConfigSpecificDiagnosticOptionsOverrideGeneralCommandLineOptions()