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);