Skip to content

Commit db4ea75

Browse files
ovskajosefpihrt
andauthored
Fix RCS1085 with ref backing field (#1718)
* Fix RCS1085 with ref backing field Fixes #1717 * Update ChangeLog.md --------- Co-authored-by: Josef Pihrt <[email protected]>
1 parent feade75 commit db4ea75

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
- Fix analyzer [RCS1172](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1172) ([PR](https://github.com/dotnet/roslynator/pull/1710))
1919
- [CLI] Fix `loc` command ([PR](https://github.com/dotnet/roslynator/pull/1711))
20+
- Exclude ref-field backed properties from [RCS1085](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1085) ([PR](https://github.com/dotnet/roslynator/pull/1718) by @ovska)
2021

2122
## [4.14.1] - 2025-10-05
2223

src/Analyzers/CSharp/Analysis/UseAutoPropertyAnalyzer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ private static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context
108108
return;
109109
}
110110

111+
#if ROSLYN_4_4
112+
if (fieldSymbol.RefKind is RefKind.Ref or RefKind.RefReadOnly)
113+
return;
114+
#endif
115+
111116
if (!CheckPreprocessorDirectives(property))
112117
return;
113118

src/Tests/Analyzers.Tests/RCS1085UseAutoPropertyTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,4 +981,30 @@ private static void Write2(ref readonly ReadOnlySequence<byte> data, IBufferWrit
981981
}
982982
}");
983983
}
984+
985+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseAutoProperty)]
986+
public async Task TestNoDiagnostic_BackingFieldRef()
987+
{
988+
await VerifyNoDiagnosticAsync(
989+
@"
990+
public ref struct Example(ref int value1, ref int value2)
991+
{
992+
private ref int _value1 = ref value1;
993+
private readonly ref int _value2 = ref value2;
994+
995+
public int Value1
996+
{
997+
get => _value1;
998+
set => _value1 = value;
999+
}
1000+
1001+
public int Value2
1002+
{
1003+
get => _value2;
1004+
set => _value2 = value;
1005+
}
1006+
}
1007+
"
1008+
);
1009+
}
9841010
}

0 commit comments

Comments
 (0)