diff --git a/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs b/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs index 7e18c0095c95..f660dbe8eb00 100644 --- a/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs +++ b/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs @@ -12479,6 +12479,11 @@ public void TestCategoryBasedBulkAnalyzerDiagnosticConfiguration(DiagnosticSever dotnet_analyzer_diagnostic.category-{category}.severity = error"; TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, expectedDiagnosticSeverity: ReportDiagnostic.Warn); + // Verify category based configuration to warning + /warnaserror reports errors. + analyzerConfigText = $@" +[*.cs] +dotnet_analyzer_diagnostic.category-{category}.severity = warning"; + TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, warnAsError: true, expectedDiagnosticSeverity: ReportDiagnostic.Error); // Verify disabled by default analyzer is not enabled by category based configuration. analyzer = new NamedTypeAnalyzerWithConfigurableEnabledByDefault(isEnabledByDefault: false, defaultSeverity); @@ -12559,6 +12564,12 @@ public void TestBulkAnalyzerDiagnosticConfiguration(DiagnosticSeverity defaultSe dotnet_analyzer_diagnostic.severity = error"; TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, expectedDiagnosticSeverity: ReportDiagnostic.Warn); + // Verify bulk configuration to warning + /warnaserror reports errors. + analyzerConfigText = $@" +[*.cs] +dotnet_analyzer_diagnostic.severity = warning"; + TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, warnAsError: true, expectedDiagnosticSeverity: ReportDiagnostic.Error); + // Verify disabled by default analyzer is not enabled by bulk configuration. analyzer = new NamedTypeAnalyzerWithConfigurableEnabledByDefault(isEnabledByDefault: false, defaultSeverity); analyzerConfigText = $@" @@ -12636,7 +12647,8 @@ private void TestBulkAnalyzerConfigurationCore( bool errorlog, ReportDiagnostic expectedDiagnosticSeverity, string rulesetText = null, - bool noWarn = false) + bool noWarn = false, + bool warnAsError = false) { var diagnosticId = analyzer.Descriptor.Id; var dir = Temp.CreateDirectory(); @@ -12654,6 +12666,11 @@ private void TestBulkAnalyzerConfigurationCore( arguments = arguments.Append($"/nowarn:{diagnosticId}"); } + if (warnAsError) + { + arguments = arguments.Append($"/warnaserror"); + } + if (errorlog) { arguments = arguments.Append($"/errorlog:errorlog"); diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerOptionsExtensions.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerOptionsExtensions.cs index c0f2fda23b53..1ef1e3684300 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerOptionsExtensions.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerOptionsExtensions.cs @@ -66,6 +66,10 @@ public static bool TryGetSeverityFromBulkConfiguration( if (analyzerConfigOptions.TryGetValue(categoryBasedKey, out var value) && AnalyzerConfigSet.TryParseSeverity(value, out severity)) { + // '/warnaserror' should bump Warning bulk configuration to Error. + if (severity == ReportDiagnostic.Warn && compilation.Options.GeneralDiagnosticOption == ReportDiagnostic.Error) + severity = ReportDiagnostic.Error; + return true; } @@ -74,6 +78,10 @@ public static bool TryGetSeverityFromBulkConfiguration( if (analyzerConfigOptions.TryGetValue(DotnetAnalyzerDiagnosticSeverityKey, out value) && AnalyzerConfigSet.TryParseSeverity(value, out severity)) { + // '/warnaserror' should bump Warning bulk configuration to Error. + if (severity == ReportDiagnostic.Warn && compilation.Options.GeneralDiagnosticOption == ReportDiagnostic.Error) + severity = ReportDiagnostic.Error; + return true; }