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

[CLI] Improve removing of unused symbols #1550

Merged
merged 6 commits into from
Oct 8, 2024
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
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