Skip to content

Commit

Permalink
[CLI] Improve removing of unused symbols (#1550)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Oct 8, 2024
1 parent 0a0d094 commit bf2a457
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 11 additions & 5 deletions src/CommandLine/Commands/FindSymbolCommand.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -171,13 +170,20 @@ private static async Task<Project> RemoveSymbolsAsync(
Project project,
CancellationToken cancellationToken)
{
foreach (IGrouping<DocumentId, SyntaxReference> grouping in symbols
.SelectMany(f => f.DeclaringSyntaxReferences)
.GroupBy(f => project.GetDocument(f.SyntaxTree).Id))
foreach (IGrouping<DocumentId, (ISymbol Symbol, SyntaxReference Reference)> 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);

Expand Down
1 change: 1 addition & 0 deletions src/CommandLine/FindSymbols/UnusedSymbolUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

0 comments on commit bf2a457

Please sign in to comment.