Skip to content

Commit

Permalink
RCS1016 result doesn't compile fix (dotnet#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesHargreaves12 authored and JochemHarmes committed Oct 30, 2023
1 parent 67bc79c commit 0e6a5e8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Fix [RCS1016](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1016.md) ([#1090](https://github.com/josefpihrt/roslynator/pull/1090)).

### Fixed

Expand Down
16 changes: 13 additions & 3 deletions src/CSharp/CSharp/SyntaxRewriters/WhitespaceRemover.cs
Expand Up @@ -33,10 +33,20 @@ public static WhitespaceRemover GetInstance(TextSpan? span = null)

public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
if (trivia.IsWhitespaceOrEndOfLineTrivia()
&& (Span?.Contains(trivia.Span) != false))
{
if (Span?.Contains(trivia.Span) == false)
return base.VisitTrivia(trivia);

if (trivia.IsKind(SyntaxKind.WhitespaceTrivia))
return Replacement;

if (trivia.IsKind(SyntaxKind.EndOfLineTrivia)){
// We can only safely remove EndOfLineTrivia if it is not proceeded by a SingleLineComment
var triviaIndex = trivia.Token.TrailingTrivia.IndexOf(trivia);
if (triviaIndex == 0)
return Replacement;
var prevTrivia = trivia.Token.TrailingTrivia[triviaIndex - 1];
if (!prevTrivia.IsKind(SyntaxKind.SingleLineCommentTrivia))
return Replacement;
}

return base.VisitTrivia(trivia);
Expand Down
22 changes: 22 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1016UseBlockBodyTests.cs
Expand Up @@ -246,6 +246,28 @@ string P
", options: Options.AddConfigOption(ConfigOptionKeys.BodyStyle, ConfigOptionValues.BodyStyle_Block));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseBlockBodyOrExpressionBody)]
public async Task Test_GetterTrailingComment()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
string P [|=> null|]; // some comment
}
", @"
class C
{
string P
{
get
{
return null;// some comment
}
}
}
", options: Options.AddConfigOption(ConfigOptionKeys.BodyStyle, ConfigOptionValues.BodyStyle_Block));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseBlockBodyOrExpressionBody)]
public async Task Test_PropertyWithGetter_MultilineExpression()
{
Expand Down

0 comments on commit 0e6a5e8

Please sign in to comment.