Skip to content

switch expression casting from/to enum and ushort suggests invalid fix in VS 2019 16..4.0 Preview 5.0 #40167

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


    private static ProgrammingProgress ProgrammingSuccessResponse(ProgrammingProgress currentState, stMsg_t responseMessage) =>
        responseMessage.stTxMsg.stHeader.wCmd // not my code
        switch
        {
            (ushort)eMonCommsCommands_t.MC_CMD_PROG_START => FromSuccessfulResponse(currentState, ProgrammingProgressState.StartedReceiving),
            (ushort)eMonCommsCommands_t.MC_CMD_PROG_DATA => FromSuccessfulResponse(currentState, ProgrammingProgressState.Receiving),
            (ushort)eMonCommsCommands_t.MC_CMD_PROG_END => FromSuccessfulResponse(currentState, ProgrammingProgressState.StoppedReceiving),
            _ => ErrorState(currentState, responseMessage)
        };

where responseMessage.stTxMsg.stHeader.wCmd is an enum and (ushort)eMonCommsCommands_t.MC_CMD_PROG_START is a ushort.

This works OK because of the cast - however an attempt to increase legibility like this:

private static ProgrammingProgress ProgrammingSuccessResponse(ProgrammingProgress currentState, stMsg_t responseMessage) =>
(eMonCommsCommands_t)responseMessage.stTxMsg.stHeader.wCmd // VS - suggests the case is redundant - but removing it
switch
{
eMonCommsCommands_t.MC_CMD_PROG_START => FromSuccessfulResponse(currentState, ProgrammingProgressState.StartedReceiving),
eMonCommsCommands_t.MC_CMD_PROG_DATA => FromSuccessfulResponse(currentState, ProgrammingProgressState.Receiving),
eMonCommsCommands_t.MC_CMD_PROG_END => FromSuccessfulResponse(currentState, ProgrammingProgressState.StoppedReceiving),
_ => ErrorState(currentState, responseMessage)
};

and Visual Studio ( 2019 16..4.0 Preview 5.0) suggests the cast is redundant - removing the cast causes the each switch clause except the default to barf.


Original Comments

Visual Studio Feedback System on 11/21/2019, 00:06 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugDeveloper CommunityThe issue was originally reported on https://developercommunity.visualstudio.comIDE-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