Skip to content

CA1859 improperly handles default interface implementationsΒ #50328

@Dean-NC

Description

@Dean-NC

Analyzer

Diagnostic ID: CA1859: Use concrete types when possible for improved performance

Describe the bug

Analyzer isn't considering interfaces with default implementations, resulting in warnings that if fixed will cause a compilation error.

Steps To Reproduce

interface I1
{
  string Test() => string.Empty;
}

sealed class C1 : I1
{ }

static void UseClass(C1 c1)
{
  I1 i1 = c1;
  Console.WriteLine(i1.Test());
}

image

Expected behavior

No warning

Actual behavior

info: CA1859 Change type of variable 'i1' from 'Console1.I1' to 'Console1.C1' for improved performance

but changing the variable to type C1 will cause a compilation error because there's no available Test() to call.

Additional context

It looks like this was fixed for explicit implementations #6416 but not default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bughelp wantedIssues that we would accept external contributions on. Also known as up-for-grabs.untriagedRequest triage from a team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions