Skip to content

Perf Phase 3: commonize duplicated helper-shaped code #459

Description

@credfeto

Part of #455.

Duplicated helper-shaped code across analyzers, to be commonized into Helpers//Extensions/ (netstandard2.0-safe):

  • Type-cache builder: "loop over specs → GetTypeByMetadataName → build Dictionary keyed on SourceClass" is duplicated 4× (ProhibitedMethods:247, ProhibitedMethodInvocations:223, ProhibitedClasses:109, ProhibitedClassesInTestAssemblies:120). Extract e.g. CompilationExtensions.BuildTypeCache(...).
  • Invocation → IMethodSymbol resolution: MethodSymbolHelper.FindInvokedMemberSymbol exists and is used by 4 analyzers, but 3 more hand-roll the same shape (ProhibitedMethods:264, ProhibitedSubstituteForUsageInTestBase:176, ArgumentException:73). Either migrate them, or (better, combined with the Phase 1 sub-issue) add a name-prefilter-aware overload so the shared helper does the cheap check first for everyone.
  • Qualified-name construction: Helpers/Mapping.cs exists but each *Spec record struct re-implements its own QualifiedName concat, and ProhibitedMethodWithStrict:120 hand-rolls the interpolation. Consolidate (this item may largely dissolve once the Phase 2 sub-issue removes string keys).
  • Non-nested type declaration walker: GetNonNestedTypeDeclarations + the type-name switch is duplicated nearly verbatim between FileNameMustMatchTypeNameDiagnosticsAnalyzer.cs:98-151 and OneTypePerDocumentAnalysisDiagnosticsAnalyzer.cs:141-225. Extract a shared syntactic helper.
  • Per-parameter type-name resolution: the GetFullTypeName + per-ParameterSyntax cache pattern is duplicated in ParameterNameDiagnosticsAnalyzer.cs:129 and ParameterOrderingDiagnosticsAnalyzer.cs:155 (the cache itself is ineffective — see the Phase 4 sub-issue). Rationalize into Helpers/ParameterHelpers.
  • Normalize spec-type inconsistency: ProhibitedClassesDiagnosticsAnalyzer.ProhibitedClassSpec is a sealed class (line 230) where every sibling analyzer uses readonly record struct.

Verification: full test suite green, no change in the set of diagnostics produced, existing per-analyzer tests still pass unmodified (behavior is identical, only the implementation is shared).

Metadata

Metadata

Assignees

No one assigned

    Labels

    AI-WorkWork for an AI AgentMediumMedium Priority

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions