Skip to content

Commit f14219b

Browse files
Fix RCS1085 (#1120)
Co-authored-by: Josef Pihrt <josef@pihrt.net>
1 parent 12824d5 commit f14219b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
- Fix [RCS1211](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1211) ([#1095](https://github.com/JosefPihrt/Roslynator/pull/1095)).
3333
- Fix [RCS0005](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0005) ([#1114](https://github.com/JosefPihrt/Roslynator/pull/1114)).
3434
- Fix [RCS1176](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1176.md) ([#1122](https://github.com/JosefPihrt/Roslynator/pull/1122)).
35+
- Fix [RCS1085](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1085.md) ([#1120](https://github.com/josefpihrt/roslynator/pull/1120)).
3536

3637
## [4.3.0] - 2023-04-24
3738

src/CSharp/CSharp/SyntaxRewriters/WhitespaceRemover.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
4141

4242
if (trivia.IsKind(SyntaxKind.EndOfLineTrivia))
4343
{
44-
// We can only safely remove EndOfLineTrivia if it is not proceeded by a SingleLineComment
45-
int triviaIndex = trivia.Token.TrailingTrivia.IndexOf(trivia);
44+
SyntaxTriviaList triviaList = trivia.Token.TrailingTrivia;
45+
int triviaIndex = triviaList.IndexOf(trivia);
46+
47+
if (triviaIndex == -1)
48+
{
49+
triviaList = trivia.Token.LeadingTrivia;
50+
triviaIndex = triviaList.IndexOf(trivia);
51+
}
52+
4653
if (triviaIndex == 0)
4754
return Replacement;
4855

49-
SyntaxTrivia prevTrivia = trivia.Token.TrailingTrivia[triviaIndex - 1];
50-
if (!prevTrivia.IsKind(SyntaxKind.SingleLineCommentTrivia))
56+
// We can only safely remove EndOfLineTrivia if it is not proceeded by a SingleLineComment
57+
if (!triviaList[triviaIndex - 1].IsKind(SyntaxKind.SingleLineCommentTrivia))
5158
return Replacement;
5259
}
5360

src/Tests/Analyzers.Tests/RCS1085UseAutoPropertyTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,37 @@ class C
919919
class MyAttribute : Attribute
920920
{
921921
}
922+
");
923+
}
924+
925+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseAutoProperty)]
926+
public async Task TestFix_WithLeadingWhitespace()
927+
{
928+
await VerifyDiagnosticAndFixAsync(@"
929+
class C
930+
{
931+
public int [|P|]
932+
{
933+
934+
935+
get
936+
{
937+
return p;
938+
}
939+
940+
set
941+
{
942+
p = value;
943+
}
944+
}
945+
946+
private int p;
947+
}
948+
", @"
949+
class C
950+
{
951+
public int P { get; set; }
952+
}
922953
");
923954
}
924955
}

0 commit comments

Comments
 (0)