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

CA1859 improperly handles explicit interface implementations #6416

Closed
stephentoub opened this issue Jan 6, 2023 · 2 comments
Closed

CA1859 improperly handles explicit interface implementations #6416

stephentoub opened this issue Jan 6, 2023 · 2 comments

Comments

@stephentoub
Copy link
Member

Analyzer

Diagnostic ID: CA1859

Analyzer source

6ae2683

Describe the bug

The analyzer appears to not be factoring in whether methods from interfaces are implemented explicitly, resulting in warnings that, if fixed, will result in broken code.

Steps To Reproduce

[Fact]
public static async Task Repro()
{
    const string Source = @"
        #nullable enable

        interface IFoo
        {
            int M();
        }

        internal class C : IFoo
        {
            int IFoo.M() => 42;
        }

        internal class Use
        {
            static int Bar()
            {
                IFoo f = new C();
                return f.M();
            }
        }
        ";

    await TestCSAsync(Source);
}

Expected behavior

No warning.

Actual behavior

info CA1859: Change type of variable 'f' from 'IFoo' to 'C' for improved performance

but if the developer tries to change the variable to type C, it'll fail to compile because there's no available M() to call.

cc: @geeknoid, @buyaa-n

@geeknoid
Copy link
Member

geeknoid commented Jan 6, 2023

Just fixed that one a few minutes ago. Will be in my next PR.

@Youssef1313
Copy link
Member

Closing as fixed per #6418 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants