Description
ReferenceCopCodeFixProvider.FixableDiagnosticIds is implemented as:
public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return Enumerable.Empty<string>().ToImmutableArray<string>(); }
}
Every property access allocates a new ImmutableArray<string> by calling Enumerable.Empty<string>().ToImmutableArray(). The Roslyn infrastructure accesses FixableDiagnosticIds multiple times during analysis — once during registration, during fix-all computation, and potentially on each diagnostic evaluation.
Affected File
src/ReferenceCop.Roslyn.CodeFixes/ReferenceCopCodeFixProvider.cs — FixableDiagnosticIds property getter (line 13)
Impact
- Repeated allocation: While each
ImmutableArray<string> is small (empty), the allocation pattern is wasteful and goes against Roslyn analyzer best practices where SupportedDiagnostics and FixableDiagnosticIds should return cached static values.
- Analyzer hosting: Roslyn analyzer hosts (VS, MSBuild, OmniSharp) may query this property frequently. The per-access allocation creates unnecessary GC pressure in an environment where analyzers should be allocation-conscious.
- Inconsistency: The
ReferenceCopAnalyzer.SupportedDiagnostics correctly uses ImmutableArray.Create(...) which is more efficient, but this code fix provider does not follow the same pattern.
Suggested Fix
Cache the value as a static field:
private static readonly ImmutableArray<string> EmptyDiagnosticIds = ImmutableArray<string>.Empty;
public sealed override ImmutableArray<string> FixableDiagnosticIds => EmptyDiagnosticIds;
Or simply:
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray<string>.Empty;
ImmutableArray<string>.Empty is a pre-allocated singleton — no allocation at all.
Additional Note
The empty FixableDiagnosticIds and the boilerplate RegisterCodeFixesAsync / MakeUppercaseAsync methods suggest this code fix provider is still the Roslyn template scaffold and has not been implemented yet. Consider either implementing it or removing the project from the solution to avoid shipping dead code that registers with the Roslyn host.
Description
ReferenceCopCodeFixProvider.FixableDiagnosticIdsis implemented as:Every property access allocates a new
ImmutableArray<string>by callingEnumerable.Empty<string>().ToImmutableArray(). The Roslyn infrastructure accessesFixableDiagnosticIdsmultiple times during analysis — once during registration, during fix-all computation, and potentially on each diagnostic evaluation.Affected File
src/ReferenceCop.Roslyn.CodeFixes/ReferenceCopCodeFixProvider.cs—FixableDiagnosticIdsproperty getter (line 13)Impact
ImmutableArray<string>is small (empty), the allocation pattern is wasteful and goes against Roslyn analyzer best practices whereSupportedDiagnosticsandFixableDiagnosticIdsshould return cached static values.ReferenceCopAnalyzer.SupportedDiagnosticscorrectly usesImmutableArray.Create(...)which is more efficient, but this code fix provider does not follow the same pattern.Suggested Fix
Cache the value as a static field:
Or simply:
ImmutableArray<string>.Emptyis a pre-allocated singleton — no allocation at all.Additional Note
The empty
FixableDiagnosticIdsand the boilerplateRegisterCodeFixesAsync/MakeUppercaseAsyncmethods suggest this code fix provider is still the Roslyn template scaffold and has not been implemented yet. Consider either implementing it or removing the project from the solution to avoid shipping dead code that registers with the Roslyn host.