Skip to content

Commit bf2a457

Browse files
authored
[CLI] Improve removing of unused symbols (#1550)
1 parent 0a0d094 commit bf2a457

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Fix analyzer [RCS0053](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0053) ([PR](https://github.com/dotnet/roslynator/pull/1547))
1313
- Fix analyzer [RCS1223](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1223) ([PR](https://github.com/dotnet/roslynator/pull/1552))
14+
- [CLI] Improve removing of unused symbols ([PR](https://github.com/dotnet/roslynator/pull/1550))
1415

1516
## [4.12.7] - 2024-10-01
1617

src/CommandLine/Commands/FindSymbolCommand.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

3-
using System;
43
using System.Collections.Generic;
54
using System.Collections.Immutable;
65
using System.Diagnostics;
@@ -171,13 +170,20 @@ private static async Task<Project> RemoveSymbolsAsync(
171170
Project project,
172171
CancellationToken cancellationToken)
173172
{
174-
foreach (IGrouping<DocumentId, SyntaxReference> grouping in symbols
175-
.SelectMany(f => f.DeclaringSyntaxReferences)
176-
.GroupBy(f => project.GetDocument(f.SyntaxTree).Id))
173+
foreach (IGrouping<DocumentId, (ISymbol Symbol, SyntaxReference Reference)> grouping in symbols
174+
.SelectMany(s => s.DeclaringSyntaxReferences.Select(r => (Symbol: s, Reference: r)))
175+
.GroupBy(f => project.GetDocument(f.Reference.SyntaxTree)!.Id))
177176
{
178-
foreach (SyntaxReference reference in grouping.OrderByDescending(f => f.Span.Start))
177+
foreach ((ISymbol symbol, SyntaxReference reference) in grouping.OrderByDescending(f => f.Reference.Span.Start))
179178
{
180179
Document document = project.GetDocument(grouping.Key);
180+
181+
if (document is null)
182+
{
183+
Debug.Fail($"Document not found for a symbol declaration '{symbol.ToDisplayString(SymbolDisplayFormats.Test)}'");
184+
continue;
185+
}
186+
181187
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);
182188
SyntaxNode node = root.FindNode(reference.Span);
183189

src/CommandLine/FindSymbols/UnusedSymbolUtility.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal static class UnusedSymbolUtility
1919
MetadataName.Parse("Microsoft.CodeAnalysis.CodeRefactorings.ExportCodeRefactoringProviderAttribute"),
2020
MetadataName.Parse("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzerAttribute"),
2121
MetadataName.Parse("System.Composition.ExportAttribute"),
22+
MetadataName.Parse("Microsoft.Extensions.Options.OptionsValidatorAttribute"),
2223
});
2324

2425
private static readonly MetadataNameSet _methodAttributeSymbols = new(new[]

0 commit comments

Comments
 (0)