Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix
  • Loading branch information
josefpihrt committed Aug 30, 2025
commit dd5d2e5caed9747ac39cf55959ec2f20c55edb16
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@ private static Task<Document> UseAttributeUsageAttributeAsync(
NameEquals(IdentifierName("AllowMultiple")),
FalseLiteralExpression())));

ClassDeclarationSyntax newNode = classDeclaration.AddAttributeLists(AttributeList(attribute));
ClassDeclarationSyntax newClassDeclaration = classDeclaration;
AttributeListSyntax attributeList = AttributeList(attribute);
SyntaxTrivia comment = classDeclaration.GetDocumentationCommentTrivia();

if (!comment.IsKind(SyntaxKind.None))
{
attributeList = attributeList.WithLeadingTrivia(classDeclaration.GetLeadingTrivia());
newClassDeclaration = newClassDeclaration.WithoutLeadingTrivia();
}

ClassDeclarationSyntax newNode = newClassDeclaration.AddAttributeLists(attributeList);

return document.ReplaceNodeAsync(classDeclaration, newNode, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ class [|FooAttribute|] : Attribute
", @"
using System;

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
class FooAttribute : Attribute
{
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseAttributeUsageAttribute)]
public async Task Test_WithComment()
{
await VerifyDiagnosticAndFixAsync(@"
using System;

/// <summary>
/// x
/// <summary>
class [|FooAttribute|] : Attribute
{
}
", @"
using System;

/// <summary>
/// x
/// <summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
class FooAttribute : Attribute
{
Expand Down
Loading