Skip to content

Commit 7092b7f

Browse files
authored
Fix analyzer RCS1203 (#1683)
1 parent 820feaa commit 7092b7f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

ChangeLog.md

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

1818
- Fix analyzer [RCS1246](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1246) ([PR](https://github.com/dotnet/roslynator/pull/1676))
1919
- Fix analyzer [RCS1248](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1248) ([PR](https://github.com/dotnet/roslynator/pull/1677))
20+
- Fix analyzer [RCS1203](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1203) ([PR](https://github.com/dotnet/roslynator/pull/1683))
2021
- Fix refactoring [Check expression for null](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0024) ([PR](https://github.com/dotnet/roslynator/pull/1682))
2122

2223
## [4.14.0] - 2025-07-26

src/Analyzers.CodeFixes/CSharp/CodeFixes/ClassDeclarationCodeFixProvider.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,17 @@ private static Task<Document> UseAttributeUsageAttributeAsync(
128128
NameEquals(IdentifierName("AllowMultiple")),
129129
FalseLiteralExpression())));
130130

131-
ClassDeclarationSyntax newNode = classDeclaration.AddAttributeLists(AttributeList(attribute));
131+
ClassDeclarationSyntax newClassDeclaration = classDeclaration;
132+
AttributeListSyntax attributeList = AttributeList(attribute);
133+
SyntaxTrivia comment = classDeclaration.GetDocumentationCommentTrivia();
134+
135+
if (!comment.IsKind(SyntaxKind.None))
136+
{
137+
attributeList = attributeList.WithLeadingTrivia(classDeclaration.GetLeadingTrivia());
138+
newClassDeclaration = newClassDeclaration.WithoutLeadingTrivia();
139+
}
140+
141+
ClassDeclarationSyntax newNode = newClassDeclaration.AddAttributeLists(attributeList);
132142

133143
return document.ReplaceNodeAsync(classDeclaration, newNode, cancellationToken);
134144
}

src/Tests/Analyzers.Tests/RCS1203UseAttributeUsageAttributeTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ class [|FooAttribute|] : Attribute
2424
", @"
2525
using System;
2626
27+
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
28+
class FooAttribute : Attribute
29+
{
30+
}
31+
");
32+
}
33+
34+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseAttributeUsageAttribute)]
35+
public async Task Test_WithComment()
36+
{
37+
await VerifyDiagnosticAndFixAsync(@"
38+
using System;
39+
40+
/// <summary>
41+
/// x
42+
/// <summary>
43+
class [|FooAttribute|] : Attribute
44+
{
45+
}
46+
", @"
47+
using System;
48+
49+
/// <summary>
50+
/// x
51+
/// <summary>
2752
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
2853
class FooAttribute : Attribute
2954
{

0 commit comments

Comments
 (0)