Skip to content

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

Open
@ThorstenReichert

Description

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;)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions