From a08c59664f28f817f4304feb68679a0662113fae Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Thu, 2 Jul 2020 09:04:40 -0700 Subject: [PATCH] Fix AD0001 for IDE0076 Fixes #45465 Ensure we normalize the target scope passed to target symbol resolver. It doesn't handle the recently added 'namespaceanddescendants' scope in legacy target string format and expects it to be normalized to 'namespace' scope. --- ...veUnnecessaryAttributeSuppressionsTests.cs | 19 +++++++++++++++++++ .../SuppressMessageAttributeState.cs | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/src/Analyzers/CSharp/Tests/RemoveUnnecessarySuppressions/RemoveUnnecessaryAttributeSuppressionsTests.cs b/src/Analyzers/CSharp/Tests/RemoveUnnecessarySuppressions/RemoveUnnecessaryAttributeSuppressionsTests.cs index 03c6ac49623ca..8e68f85e67c30 100644 --- a/src/Analyzers/CSharp/Tests/RemoveUnnecessarySuppressions/RemoveUnnecessaryAttributeSuppressionsTests.cs +++ b/src/Analyzers/CSharp/Tests/RemoveUnnecessarySuppressions/RemoveUnnecessaryAttributeSuppressionsTests.cs @@ -5,6 +5,7 @@ #nullable enable using System.Threading.Tasks; +using Microsoft.CodeAnalysis.RemoveUnnecessarySuppressions; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; @@ -182,5 +183,23 @@ public void M() {{ }} }}"; await VerifyCS.VerifyCodeFixAsync(input, input); } + + [Fact, WorkItem(45465, "https://github.com/dotnet/roslyn/issues/45465")] + public async Task LegacyModeGlobalSuppressionWithNamespaceAndDescendantsScope() + { + var target = "N:N.InvalidChild"; + var input = $@" +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(""Category"", ""Id: Title"", Scope = ""namespaceanddescendants"", Target = {{|#0:""{target}""|}})] + +namespace N +{{ + class C {{ }} +}}"; + var expectedDiagnostic = VerifyCS.Diagnostic(AbstractRemoveUnnecessaryAttributeSuppressionsDiagnosticAnalyzer.LegacyFormatTargetDescriptor) + .WithLocation(0) + .WithArguments(target); + + await VerifyCS.VerifyCodeFixAsync(input, expectedDiagnostic, input); + } } } diff --git a/src/Analyzers/Core/Analyzers/RemoveUnnecessarySuppressions/SuppressMessageAttributeState.cs b/src/Analyzers/Core/Analyzers/RemoveUnnecessarySuppressions/SuppressMessageAttributeState.cs index f4e7b3990db5f..d1e8d9be2ea77 100644 --- a/src/Analyzers/Core/Analyzers/RemoveUnnecessarySuppressions/SuppressMessageAttributeState.cs +++ b/src/Analyzers/Core/Analyzers/RemoveUnnecessarySuppressions/SuppressMessageAttributeState.cs @@ -127,6 +127,11 @@ public bool HasValidTarget( // Compilation wide suppression with a non-null target is considered invalid. return targetSymbolString == null; } + else if (targetScope == TargetScope.NamespaceAndDescendants) + { + // TargetSymbolResolver expects the callers to normalize 'NamespaceAndDescendants' and 'Namespace' scopes to 'Namespace' scope. + targetScope = TargetScope.Namespace; + } var resolver = new TargetSymbolResolver(_compilation, targetScope, targetSymbolString); resolvedSymbols = resolver.Resolve(out targetHasDocCommentIdFormat);