Roslyn: Convert to switch replaces with broken code.

Created on 7 Jul 2019  路  6Comments  路  Source: dotnet/roslyn

if (o is string text &&
    int.TryParse(text, out var n) &&
    n < 5 && n > -5)
{
}
else
{
}

Is refactored to:

switch (o)
{
    case string text when int.TryParse(text, out var n) &&
    n < 5:
        break;
    default:
        break;
}

Not ideal that it trims off && n > -5

I expected:

switch (o)
{
    case string text when int.TryParse(text, out var n) &&
                        n < 5 && n > -5:
        break;
    default:
        break;
}

Animation

Area-IDE Bug help wanted

All 6 comments

I think the purpose is to speedily get you into switch form. You may have to do some fixups as necessary if your existing constructs use features (like larger scoping) that aren't well supported by a switch expression.

I agree, getting 90% of the way there is better than being forced to do all the typing myself.

Looks like best effort could be significantly improved though. There's no logical reason IMO that && should be disappearing. It might even be possible to make the refactoring understand the variable scoping change.

I missed that a whole operand was gone after the &&, not just the operator. That's a more serious bug because it could be missed if you were moving fast and didn't have a test covering it.

Note: the bit about the clause being stripped off should def just be fixed. I wasn't referring to that bit :)

this should be fixed by #38238

Yup. Have validated this is fixed. LMK if there are other issues we need to closeout. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

binki picture binki  路  3Comments

glennblock picture glennblock  路  3Comments

joshua-mng picture joshua-mng  路  3Comments

NikChao picture NikChao  路  3Comments

MadsTorgersen picture MadsTorgersen  路  3Comments