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

RCS1085 suggests auto-property if backing field is used as in-parameter #1455

Closed
ovska opened this issue May 5, 2024 · 0 comments · Fixed by #1461
Closed

RCS1085 suggests auto-property if backing field is used as in-parameter #1455

ovska opened this issue May 5, 2024 · 0 comments · Fixed by #1461
Assignees

Comments

@ovska
Copy link
Contributor

ovska commented May 5, 2024

Product and Version Used:
Roslynator.Analyzers 4.12.2

Steps to Reproduce:
Expose a backing field used as an in-parameter with a property.

Actual Behavior:

using System.Buffers;

class Repro(ReadOnlySequence<byte> data)
{                              // v RCS1085: use auto-implemented property
    public ReadOnlySequence<byte> Data => _data;
    private readonly ReadOnlySequence<byte> _data = data;

    public void Method(IBufferWriter<byte> writer)
    {
        Write1(in _data, writer);
        Write2(in _data, writer);
    }

    private static void Write1(in ReadOnlySequence<byte> data, IBufferWriter<byte> writer)
    {
        data.CopyTo(writer.GetSpan((int)data.Length));
    }

    private static void Write2(ref readonly ReadOnlySequence<byte> data, IBufferWriter<byte> writer)
    {
        data.CopyTo(writer.GetSpan((int)data.Length));
    }
}

Auto-fixer changes usages to Write1(in this.Data, writer), which correctly is then flagged as an error:

CS8156 An expression cannot be used in this context because it may not be passed or returned by reference

Expected Behavior:
No diagnostic should be reported.

As far as I know, there's no way of using a property return value with in without a backing field. This diagnostic seems to be reliant on _data being readonly, as removing the modifer silences RCS1085 (and then correctly raises IDE0044 Make field readonly). Making the backing field anything else than private also suppresses the warning.

I can try to take a crack at this with some direction. Does there exist a neat built-in way in Roslynator to find all usages of the field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants