Skip to content

RCS1124 may add unnecessary cast for nullable value types #1571

@RenderMichael

Description

@RenderMichael

For the following code:

internal struct S;

internal static S? M()
{
    S? i = new S();
    return i;
}

RCS1124 is emitted, to inline the i variable. However, the fixer results in this:

internal struct S;

internal static S? M()
{
    return (S?)new S();
}

This unnecessary cast does not need to be emitted.

It's worth noting that a non-nullable local doesn't have this problem:

internal struct S;

internal static S? M()
{
    S i = new S();
    return i;
}

->

internal struct S;

internal static S? M()
{
    return new S();
}

(there's also no analyzer to remove this unnecessary cast, even Roslyn's IDE0004 doesn't trigger, but that may be another issue)

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