diff --git a/ChangeLog.md b/ChangeLog.md index 237df80c85..d7640e7ac1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix analyzer [RCS0053](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0053) ([PR](https://github.com/dotnet/roslynator/pull/1547)) - Fix analyzer [RCS1223](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1223) ([PR](https://github.com/dotnet/roslynator/pull/1552)) +- [CLI] Improve removing of unused symbols ([PR](https://github.com/dotnet/roslynator/pull/1550)) ## [4.12.7] - 2024-10-01 diff --git a/src/CommandLine/Commands/FindSymbolCommand.cs b/src/CommandLine/Commands/FindSymbolCommand.cs index 4178c6dd2b..1f31bc7e08 100644 --- a/src/CommandLine/Commands/FindSymbolCommand.cs +++ b/src/CommandLine/Commands/FindSymbolCommand.cs @@ -1,6 +1,5 @@ // Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; @@ -171,13 +170,20 @@ private static async Task RemoveSymbolsAsync( Project project, CancellationToken cancellationToken) { - foreach (IGrouping grouping in symbols - .SelectMany(f => f.DeclaringSyntaxReferences) - .GroupBy(f => project.GetDocument(f.SyntaxTree).Id)) + foreach (IGrouping grouping in symbols + .SelectMany(s => s.DeclaringSyntaxReferences.Select(r => (Symbol: s, Reference: r))) + .GroupBy(f => project.GetDocument(f.Reference.SyntaxTree)!.Id)) { - foreach (SyntaxReference reference in grouping.OrderByDescending(f => f.Span.Start)) + foreach ((ISymbol symbol, SyntaxReference reference) in grouping.OrderByDescending(f => f.Reference.Span.Start)) { Document document = project.GetDocument(grouping.Key); + + if (document is null) + { + Debug.Fail($"Document not found for a symbol declaration '{symbol.ToDisplayString(SymbolDisplayFormats.Test)}'"); + continue; + } + SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken); SyntaxNode node = root.FindNode(reference.Span); diff --git a/src/CommandLine/FindSymbols/UnusedSymbolUtility.cs b/src/CommandLine/FindSymbols/UnusedSymbolUtility.cs index c79c1c32e3..f718d9c9de 100644 --- a/src/CommandLine/FindSymbols/UnusedSymbolUtility.cs +++ b/src/CommandLine/FindSymbols/UnusedSymbolUtility.cs @@ -19,6 +19,7 @@ internal static class UnusedSymbolUtility MetadataName.Parse("Microsoft.CodeAnalysis.CodeRefactorings.ExportCodeRefactoringProviderAttribute"), MetadataName.Parse("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzerAttribute"), MetadataName.Parse("System.Composition.ExportAttribute"), + MetadataName.Parse("Microsoft.Extensions.Options.OptionsValidatorAttribute"), }); private static readonly MetadataNameSet _methodAttributeSymbols = new(new[]