Skip to content

Incorrect "Cast is redundant" when negating Int32-typecast-to-Int64 #34553

@verelpode

Description

@verelpode

VS 15.9.3 incorrectly claims that the following typecast is redundant. The use of the typecast does change the behavior. Without the typecast, OverflowException occurs when Int32.MinValue is negated. With the typecast to Int64, it always succeeds without overflow, therefore the typecast is not redundant.

Int32 input32 = Int32.MinValue; // Can also be a parameter or variable, not a constant.
Int64 output64_a = checked(-input32); // Throws OverflowException.
Int64 output64_b = checked(-(Int64)input32); // Does not throw OverflowException but IDE claims "Cast is redundant".

Thanks for investigating.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugIDE-CodeStyleBuilt-in analyzers, fixes, and refactoringshelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions