RCS1085 suggests auto-property if backing field is used as in-parameter
ovska opened this issue · comments
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?