Skip to content

Commit

Permalink
Fix handling of records in RCS1223 (dotnet#1051)
Browse files Browse the repository at this point in the history
Co-authored-by: Josef Pihrt <[email protected]>
  • Loading branch information
2 people authored and JochemHarmes committed Oct 30, 2023
1 parent 56d9df3 commit d5a2165
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix ([RCS1235](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1235.md)) ([#1047](https://github.com/JosefPihrt/Roslynator/pull/1047)).
- Fix ([RCS1206](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1206.md)) ([#1049](https://github.com/JosefPihrt/Roslynator/pull/1049)).
- Prevent possible recursion in ([RCS1235](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1235.md)) ([#1054](https://github.com/JosefPihrt/Roslynator/pull/1054)).
- Fix ([RCS1223](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1223.md)) ([#1051](https://github.com/JosefPihrt/Roslynator/pull/1051)).

## [4.2.0] - 2022-11-27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,12 @@ public static void AnalyzerNamedTypeSymbol(SymbolAnalysisContext context, INamed

if (typeSymbol.OriginalDefinition.HasAttribute(debuggerDisplayAttributeSymbol, includeBaseTypes: true))
return;

SyntaxToken identifier;

if (typeKind == TypeKind.Class)
{
var classDeclaration = (ClassDeclarationSyntax)typeSymbol.GetSyntax(context.CancellationToken);

identifier = classDeclaration.Identifier;
}
else
{
var structDeclaration = (StructDeclarationSyntax)typeSymbol.GetSyntax(context.CancellationToken);

identifier = structDeclaration.Identifier;
}


if(typeSymbol.GetSyntax(context.CancellationToken) is not TypeDeclarationSyntax typeDeclaration)
return;

var identifier = typeDeclaration.Identifier;

DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.MarkTypeWithDebuggerDisplayAttribute, identifier, identifier.ValueText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,32 @@ public abstract class C
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MarkTypeWithDebuggerDisplayAttribute)]
public async Task Test_PublicRecord()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Diagnostics;
public record [|R|]
{
}
", @"
using System.Diagnostics;
[DebuggerDisplay(""{DebuggerDisplay,nq}"")]
public record R
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay
{
get
{
return ToString();
}
}
}
");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,7 @@ public static async Task<Document> RefactorAsync(

PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration(propertyName, InvocationExpression(IdentifierName("ToString")));

TypeDeclarationSyntax newTypeDeclaration;

if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
{
newTypeDeclaration = SyntaxRefactorings.AddAttributeLists(classDeclaration, keepDocumentationCommentOnTop: true, attributeList);
}
else
{
var structDeclaration = (StructDeclarationSyntax)typeDeclaration;

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

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

Expand Down

0 comments on commit d5a2165

Please sign in to comment.