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

Code fix for CS0206 when passing a property to a ref parameter #51515

Open
jnm2 opened this issue Feb 26, 2021 · 6 comments
Open

Code fix for CS0206 when passing a property to a ref parameter #51515

jnm2 opened this issue Feb 26, 2021 · 6 comments
Labels
Area-IDE Concept-Continuous Improvement Feature Request 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

@jnm2
Copy link
Contributor

jnm2 commented Feb 26, 2021

This is similar to #51512 and could also be linked from the big table in #23326 under CS0206, and CS1510 for the last example.

class C
{
    public int Property { get; set; }

    void M()
    {
        // ❌ CS0206 A property or indexer may not be passed as an out or ref parameter
        //     ↓↓↓↓↓↓↓↓
        M2(ref Property);
    }

    void M2(ref int p) { }
}

It would save me time if there was a light bulb fix 💡 Pass and update 'Property' using a temporary variable:

        var property = Property;
        M2(ref property);
        Property = property;

out parameters

        M2(out Property);

💡 Update 'Property' using a temporary variable:

        M2(out var property);
        Property = property;

Struct properties

#51512 comes into play and ideally these two concepts would work together.

        M2(ref StructProperty.InnerProperty);

💡 Copy and set updated 'StructProperty' value (message from #51512):

        var structProperty = StructProperty;
        var innerProperty = structProperty.InnerProperty;
        M2(ref innerProperty);
        structProperty.InnerProperty = innerProperty ;
        StructProperty = structProperty;

With an out parameter:

        M2(out StructProperty.InnerProperty);

💡 Copy and set updated 'StructProperty' value (message from #51512):

        M2(out var innerProperty);
        var structProperty = StructProperty;
        structProperty.InnerProperty = innerProperty;
        StructProperty = structProperty;

Non-lvalues (CS1510)

@RikkiGibson suggested that this could be extended to provide a fix for passing a ref to a non-lvalue for a different error code, CS1510:

        // ❌ CS1510 A ref or out value must be an assignable variable
        //      ↓↓↓↓↓
        M2(ref (2 + 2));

💡 Pass '2 + 2' using a temporary variable (stripping the parens in the message):

        var suggestedName = 2 + 2;
        M2(ref suggestedName)
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Feb 26, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Feb 26, 2021

should this be provided whenever a ref argument is not an lvalue?

@RikkiGibson RikkiGibson added Area-IDE IDE-CodeStyle Built-in analyzers, fixes, and refactorings labels Feb 26, 2021
@jnm2
Copy link
Contributor Author

jnm2 commented Feb 26, 2021

@RikkiGibson It shouldn't be provided on ref var x = ref Property; or return ref Property;. Is that what you mean?

@RikkiGibson
Copy link
Contributor

What I'm saying is, it feels like M2(ref (2 + 2)) could also have a fix to extract a local and pass it by ref.

@jnm2
Copy link
Contributor Author

jnm2 commented Feb 26, 2021

That's a really good idea, I hadn't considered that.

@Youssef1313
Copy link
Member

What I'm saying is, it feels like M2(ref (2 + 2)) could also have a fix to extract a local and pass it by ref.

Note if this is going to be added to the table in #23326, the error code for this is "CS1510".

@jinujoseph jinujoseph added Concept-Continuous Improvement help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 2, 2021
@jinujoseph jinujoseph added this to the Backlog milestone Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Concept-Continuous Improvement Feature Request 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