Skip to content

Commit 0a0d094

Browse files
authored
Fix analyzer RCS1223 - handle type declaration with no braces (#1552)
1 parent 1ea4149 commit 0a0d094

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

ChangeLog.md

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

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

1415
## [4.12.7] - 2024-10-01
1516

src/Tests/Analyzers.Tests/RCS1223MarkTypeWithDebuggerDisplayAttributeTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,31 @@ private string DebuggerDisplay
312312
}
313313
}
314314
}
315+
""");
316+
}
317+
318+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MarkTypeWithDebuggerDisplayAttribute)]
319+
public async Task Test_PublicReadOnlyRefStruct()
320+
{
321+
await VerifyDiagnosticAndFixAsync("""
322+
using System.Diagnostics;
323+
324+
public readonly ref struct [|Dummy|];
325+
""", """
326+
using System.Diagnostics;
327+
328+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
329+
public readonly ref struct Dummy
330+
{
331+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
332+
private string DebuggerDisplay
333+
{
334+
get
335+
{
336+
return "";
337+
}
338+
}
339+
}
315340
""");
316341
}
317342
}

src/Workspaces.Common/CSharp/Refactorings/MarkTypeWithDebuggerDisplayAttributeRefactoring.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
67
using Microsoft.CodeAnalysis.CSharp.Syntax;
78
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
89
using static Roslynator.CSharp.CSharpFactory;
@@ -16,7 +17,16 @@ public static async Task<Document> RefactorAsync(
1617
TypeDeclarationSyntax typeDeclaration,
1718
CancellationToken cancellationToken)
1819
{
19-
int position = typeDeclaration.OpenBraceToken.Span.End;
20+
TypeDeclarationSyntax newTypeDeclaration = typeDeclaration;
21+
22+
if (typeDeclaration.OpenBraceToken.IsKind(SyntaxKind.None))
23+
{
24+
newTypeDeclaration = typeDeclaration.WithSemicolonToken(default)
25+
.WithOpenBraceToken(OpenBraceToken())
26+
.WithCloseBraceToken(CloseBraceToken());
27+
}
28+
29+
int position = newTypeDeclaration.Identifier.Span.End;
2030

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

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

30-
PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration(propertyName, InvocationExpression(IdentifierName("ToString")));
40+
INamedTypeSymbol typeSymbol = semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken)!;
41+
42+
ExpressionSyntax returnExpression = (typeSymbol.TypeKind == TypeKind.Struct && typeSymbol.IsRefLikeType)
43+
? StringLiteralExpression("")
44+
: InvocationExpression(IdentifierName("ToString"));
45+
46+
PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration(propertyName, returnExpression);
3147

32-
TypeDeclarationSyntax newTypeDeclaration = SyntaxRefactorings.AddAttributeLists(typeDeclaration, keepDocumentationCommentOnTop: true, attributeList);
48+
newTypeDeclaration = SyntaxRefactorings.AddAttributeLists(newTypeDeclaration, keepDocumentationCommentOnTop: true, attributeList);
3349

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

0 commit comments

Comments
 (0)