Skip to content

"if statement can be simplified" does not respect preprocessor #36117

@RayKoopa

Description

@RayKoopa

Version Used:
VS 2019 16.1.1 (cannot test in Beta at the moment as it broke compiling my main project :( ).

Steps to Reproduce:

  1. Create sample C# project with the following Main method:
private static int Main()
{
    bool check = true/*of course, some actual calculation would be done here*/;
#if DEBUG
    if (check)
        return 3;
#endif
    return 2;
}

This code has the requirements of returning 3 in DEBUG builds if check is true, otherwise 2. In non-DEBUG builds, it always returns 2.

Expected Behavior:
The if statement cannot be simplified due to the constraints put on it by the preprocessor #if DEBUG.

Actual Behavior:
However, VS suggests IDE0046: 'if' statement can be simplified to create the following:

private static int Main()
{
    bool check = true;
#if DEBUG
    return check ? 3 : 2;

#endif
}

This breaks the logic of any non-DEBUG build as the return is completely gone from them (it would even break compilation for normal methods requiring a return). The above requirements are not met anymore.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions