Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Convert to switch expression" refactoring for ternary statements #75973

Open
rabuckley opened this issue Nov 19, 2024 · 1 comment
Open

"Convert to switch expression" refactoring for ternary statements #75973

rabuckley opened this issue Nov 19, 2024 · 1 comment

Comments

@rabuckley
Copy link

Brief description:

Light up the "Convert to switch expression" refactoring for ternary statements.

This is a style preference.

Code example that the analyzer should report:

Currently, the code

static string? Case1(ReadOnlySpan<char> source)
{
    var i = source.IndexOf(':');

    var substring =  i < 0
        ? null
        : source[i..].ToString();

    return substring;
}

Sequentially has the possible refactorings,

  1. Replace conditional expression with statements
  2. Convert to switch statement
  3. Convert to switch expression

It would be nice if it would suggest all three in the first instance, rather than requiring one or two transformations first. I.e., suggest the refactoring from ternary to,

static string? Case1(ReadOnlySpan<char> source)
{
    var i = source.IndexOf(':');

    var substring = i switch
    {
        < 0 => null,
        _ => source[i..].ToString(),
    };

    return substring;
}
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 19, 2024
@CyrusNajmabadi
Copy link
Member

Not sure how i feel about this. Conditional is much terser and more idiomatic here for me. If anything, i'd support moving a two-branch switch expr to a conditional expr.

@CyrusNajmabadi CyrusNajmabadi added this to the Backlog milestone Nov 20, 2024
@CyrusNajmabadi CyrusNajmabadi removed the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants