diff --git a/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs b/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs index 726df7260335e..8e38f5de7ff54 100644 --- a/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs +++ b/src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs @@ -12497,6 +12497,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); @@ -12577,6 +12582,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 = $@" @@ -12654,7 +12665,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(); @@ -12672,6 +12684,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 c0f2fda23b531..1ef1e3684300a 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; }