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

Intellisense suggests switch expression on ref returns #40928

Open
aalmada opened this issue Jan 13, 2020 · 3 comments
Open

Intellisense suggests switch expression on ref returns #40928

aalmada opened this issue Jan 13, 2020 · 3 comments
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@aalmada
Copy link

aalmada commented Jan 13, 2020

Version Used:
Visual Studio 2019 Version 16.5.0 Preview 1.0

Steps to Reproduce:

  1. Add the following method:
public ref int GetItem(int[] array, int index)
{
    switch (index)
    {
        case 0: 
            return ref array[0];
        case 1:
            return ref array[0];
        default:
            return ref array[0];
    }
}
  1. Apply the quick action convert switch statement to expression to the switch.

Expected Behavior:

Either, not suggest the action, if not possible, or convert to valid source code.

Actual Behavior:

Converts the method to

public ref int GetItem(int[] array, int index)
{
    return index switch
    {
        0 => ref array[0],
        1 => ref array[0],
        _ => ref array[0],
    };
}

resulting in the error error CS1525: Invalid expression term 'ref' for each case line.

@jinujoseph jinujoseph added Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings labels Jan 14, 2020
@jinujoseph jinujoseph added this to the Backlog milestone Jan 14, 2020
@Rabadash8820
Copy link
Contributor

I see two other related Issues that have already been closed: #42130 and #40198, which were marked closed after PR #40236 was merged.

However, correct me if I'm wrong @CyrusNajmabadi, but that PR doesn't actually make ref returns in switch expressions valid syntax, it just makes it so IntelliSense won't suggest that as a Quick Action. I personally think (as I think the openers of those other Issues would agree) that ref switches should be valid syntax, or at least, I don't see any reason why it shouldn't. Curious to hear if this is actually a planned addition to the language.

@CyrusNajmabadi
Copy link
Member

Curious to hear if this is actually a planned addition to the language.

There is no plan to add that. If you would like that added, please make a feature request over at dotnet/csharplang. Thanks!

@Rabadash8820
Copy link
Contributor

Just opened dotnet/csharplang#3326 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
None yet
Development

No branches or pull requests

5 participants