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: False positive for operators and multiple derived types #7317

Open
ThorstenReichert opened this issue May 22, 2024 · 1 comment · May be fixed by #7319
Open

CA1859: False positive for operators and multiple derived types #7317

ThorstenReichert opened this issue May 22, 2024 · 1 comment · May be fixed by #7319

Comments

@ThorstenReichert
Copy link

Analyzer

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

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 8.0.101

Describe the bug

CA1859 is falsely raised when multiple derivatives are involved together with custom operator overwrites. Applying the suggested fix leads to compilation errors.

Repro

public abstract class Base
{
    public static Base operator *(Base left, int right) => default;
}

public class A : Base;
public class B : Base;

public class Test
{
    private static Base Foo(Base input)
    //                  ^^^ CA1859
    {
        return input is A ? new B() : input * 4;
    }
}

Actual behavior

CA1859 is raised for Test.Foo method, suggesting to change the return type to B

Additional context

No warning is raised when either

  • the operator ist replaced with a method like static Base Multiply(Base, int)
  • the ternary operator in the return expression of Test.Foo is removed (e.g. return input * 4;)
@CollinAlpert
Copy link
Contributor

Thanks, this was an excellent bug report!

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

Successfully merging a pull request may close this issue.

2 participants