Skip to content

Commit

Permalink
Merge pull request #58460 from mavasani/Issue55541
Browse files Browse the repository at this point in the history
Handle /warnaserror with Warning analyzer bulk configuration in edito…
  • Loading branch information
mavasani authored Jan 12, 2022
2 parents 6511d3a + 3ae6b3c commit db732a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = $@"
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit db732a1

Please sign in to comment.