Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyNotFound exception in RemoveUnnecessaryInlineSuppressionsDiagn… #46048

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,12 @@ await ProcessReportedDiagnosticsAsync(diagnostics, tree, compilationWithAnalyzer
// Remove entries for unhandled diagnostic ids.
foreach (var id in unhandledIds)
{
foreach (var (pragma, _) in idToPragmasMap[id])
if (idToPragmasMap.TryGetValue(id, out var pragmas))
{
pragmasToIsUsedMap.Remove(pragma);
foreach (var (pragma, _) in pragmas)
{
pragmasToIsUsedMap.Remove(pragma);
}
}

if (idToSuppressMessageAttributesMap.TryGetValue(id, out var attributeNodes))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,23 +265,34 @@ void M()
}
}

public enum TestKind
{
Pragmas,
SuppressMessageAttributes,
PragmasAndSuppressMessageAttributes
}

[Theory, CombinatorialData]
public async Task TestDoNotRemoveUnsupportedDiagnosticSuppression(bool disable)
[WorkItem(46047, "https://github.com/dotnet/roslyn/issues/46047")]
public async Task TestDoNotRemoveUnsupportedDiagnosticSuppression(bool disable, TestKind testKind)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception was being hit for TestKind.SuppressMessageAttributes when there are no pragmas in input.

{
var disableOrRestore = disable ? "disable" : "restore";
var pragmas = new StringBuilder();
var suppressMessageAttribtes = new StringBuilder();
foreach (var id in UnsupportedDiagnosticIds)
{
pragmas.AppendLine($@"#pragma warning {disableOrRestore} {id}");
suppressMessageAttribtes.AppendLine($@"[System.Diagnostics.CodeAnalysis.SuppressMessage(""Category"", ""{id}"")]");
if (testKind == TestKind.Pragmas || testKind == TestKind.PragmasAndSuppressMessageAttributes)
pragmas.AppendLine($@"#pragma warning {disableOrRestore} {id}");

if (testKind == TestKind.SuppressMessageAttributes || testKind == TestKind.PragmasAndSuppressMessageAttributes)
suppressMessageAttribtes.AppendLine($@"[System.Diagnostics.CodeAnalysis.SuppressMessage(""Category"", ""{id}"")]");
}

var source = $@"{{|FixAllInDocument:{pragmas}{suppressMessageAttribtes}|}}class Class {{ }}";

// Compiler diagnostics cannot be suppressed with SuppressMessageAttribute.
// Hence, attribute suppressions for compiler diagnostics are always unnecessary.
if (!IsCompilerDiagnosticsTest)
if (!IsCompilerDiagnosticsTest || testKind == TestKind.Pragmas)
{
await TestMissingInRegularAndScriptAsync(source);
}
Expand Down