Skip to content

Commit

Permalink
Fix analyzer RCS1223 - handle type declaration with no braces (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Oct 7, 2024
1 parent 1ea4149 commit 0a0d094
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix analyzer [RCS0053](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0053) ([PR](https://github.com/dotnet/roslynator/pull/1547))
- Fix analyzer [RCS1223](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1223) ([PR](https://github.com/dotnet/roslynator/pull/1552))

## [4.12.7] - 2024-10-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,31 @@ private string DebuggerDisplay
}
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MarkTypeWithDebuggerDisplayAttribute)]
public async Task Test_PublicReadOnlyRefStruct()
{
await VerifyDiagnosticAndFixAsync("""
using System.Diagnostics;
public readonly ref struct [|Dummy|];
""", """
using System.Diagnostics;
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public readonly ref struct Dummy
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay
{
get
{
return "";
}
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Roslynator.CSharp.CSharpFactory;
Expand All @@ -16,7 +17,16 @@ public static async Task<Document> RefactorAsync(
TypeDeclarationSyntax typeDeclaration,
CancellationToken cancellationToken)
{
int position = typeDeclaration.OpenBraceToken.Span.End;
TypeDeclarationSyntax newTypeDeclaration = typeDeclaration;

if (typeDeclaration.OpenBraceToken.IsKind(SyntaxKind.None))
{
newTypeDeclaration = typeDeclaration.WithSemicolonToken(default)
.WithOpenBraceToken(OpenBraceToken())
.WithCloseBraceToken(CloseBraceToken());
}

int position = newTypeDeclaration.Identifier.Span.End;

SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

Expand All @@ -27,9 +37,15 @@ public static async Task<Document> RefactorAsync(
ParseName("System.Diagnostics.DebuggerDisplayAttribute").WithSimplifierAnnotation(),
AttributeArgument(LiteralExpression($"{{{propertyName},nq}}"))));

PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration(propertyName, InvocationExpression(IdentifierName("ToString")));
INamedTypeSymbol typeSymbol = semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken)!;

ExpressionSyntax returnExpression = (typeSymbol.TypeKind == TypeKind.Struct && typeSymbol.IsRefLikeType)
? StringLiteralExpression("")
: InvocationExpression(IdentifierName("ToString"));

PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration(propertyName, returnExpression);

TypeDeclarationSyntax newTypeDeclaration = SyntaxRefactorings.AddAttributeLists(typeDeclaration, keepDocumentationCommentOnTop: true, attributeList);
newTypeDeclaration = SyntaxRefactorings.AddAttributeLists(newTypeDeclaration, keepDocumentationCommentOnTop: true, attributeList);

newTypeDeclaration = MemberDeclarationInserter.Default.Insert(newTypeDeclaration, propertyDeclaration);

Expand Down

0 comments on commit 0a0d094

Please sign in to comment.