From 69b5962ccdb91e5078c00c1ba90d068b1ac06bef Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 20:17:53 +0200 Subject: [PATCH] Fix analyzer RCS1085 (#1461) --- ChangeLog.md | 1 + .../Analysis/UseAutoPropertyAnalyzer.cs | 2 +- .../RCS1085UseAutoPropertyTests.cs | 29 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index f279354439..1b9117d4ab 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix analyzer [RCS1246](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1246) ([PR](https://github.com/dotnet/roslynator/pull/1460)) +- Fix analyzer [RCS1085](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1085) ([PR](https://github.com/dotnet/roslynator/pull/1461)) ## [4.12.2] - 2024-04-23 diff --git a/src/Analyzers/CSharp/Analysis/UseAutoPropertyAnalyzer.cs b/src/Analyzers/CSharp/Analysis/UseAutoPropertyAnalyzer.cs index 422e2e1b58..16db72c43d 100644 --- a/src/Analyzers/CSharp/Analysis/UseAutoPropertyAnalyzer.cs +++ b/src/Analyzers/CSharp/Analysis/UseAutoPropertyAnalyzer.cs @@ -486,7 +486,7 @@ public override void VisitArgument(ArgumentSyntax node) { CancellationToken.ThrowIfCancellationRequested(); - if (node.RefOrOutKeyword.IsKind(SyntaxKind.RefKeyword, SyntaxKind.OutKeyword)) + if (node.RefOrOutKeyword.IsKind(SyntaxKind.RefKeyword, SyntaxKind.OutKeyword, SyntaxKind.InKeyword)) { ExpressionSyntax expression = node.Expression?.WalkDownParentheses(); diff --git a/src/Tests/Analyzers.Tests/RCS1085UseAutoPropertyTests.cs b/src/Tests/Analyzers.Tests/RCS1085UseAutoPropertyTests.cs index a65b724594..3b72a81dbc 100644 --- a/src/Tests/Analyzers.Tests/RCS1085UseAutoPropertyTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1085UseAutoPropertyTests.cs @@ -952,4 +952,33 @@ class C } "); } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseAutoProperty)] + public async Task TestNoDiagnostic_BackingFieldUsedByReference() + { + await VerifyNoDiagnosticAsync(@" +using System.Buffers; + +class Repro(ReadOnlySequence data) +{ + public ReadOnlySequence Data => _data; + private readonly ReadOnlySequence _data = data; + + public void Method(IBufferWriter writer) + { + Write1(in _data, writer); + Write2(in _data, writer); + } + + private static void Write1(in ReadOnlySequence data, IBufferWriter writer) + { + data.CopyTo(writer.GetSpan((int)data.Length)); + } + + private static void Write2(ref readonly ReadOnlySequence data, IBufferWriter writer) + { + data.CopyTo(writer.GetSpan((int)data.Length)); + } +}"); + } }