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 Jul 26, 2025
commit 3982686bf11c49255a0d015e092cebb718ac7ced
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
SyntaxKind.ObjectInitializerExpression,
SyntaxKind.CollectionInitializerExpression,
SyntaxKind.EnumDeclaration,
SyntaxKind.AnonymousObjectCreationExpression)))
SyntaxKind.AnonymousObjectCreationExpression,
SyntaxKind.CollectionExpression)))
{
return;
}
Expand Down Expand Up @@ -69,6 +70,31 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
}
}
if (node is CollectionExpressionSyntax collectionExpression)
{
SeparatedSyntaxList<CollectionElementSyntax> elements = collectionExpression.Elements;

int count = elements.Count;

if (count == elements.SeparatorCount)
{
CodeAction codeAction = CodeAction.Create(
"Remove comma",
ct => RemoveTrailingComma(document, elements.GetSeparator(count - 1), ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
}
else
{
CodeAction codeAction = CodeAction.Create(
"Add comma",
ct => AddTrailingComma(document, elements.Last(), ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
}
}
else if (node is AnonymousObjectCreationExpressionSyntax objectCreation)
{
SeparatedSyntaxList<AnonymousObjectMemberDeclaratorSyntax> initializers = objectCreation.Initializers;
Expand Down
44 changes: 44 additions & 0 deletions src/Analyzers/CSharp/Analysis/AddOrRemoveTrailingCommaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override void Initialize(AnalysisContext context)

context.RegisterSyntaxNodeAction(f => AnalyzeEnumDeclaration(f), SyntaxKind.EnumDeclaration);
context.RegisterSyntaxNodeAction(f => AnalyzeAnonymousObjectCreationExpression(f), SyntaxKind.AnonymousObjectCreationExpression);
context.RegisterSyntaxNodeAction(f => AnalyzeCollectionExpression(f), SyntaxKind.CollectionExpression);

context.RegisterSyntaxNodeAction(
f => AnalyzeInitializerExpression(f),
Expand Down Expand Up @@ -169,6 +170,49 @@ private static void AnalyzeAnonymousObjectCreationExpression(SyntaxNodeAnalysisC
}
}

private static void AnalyzeCollectionExpression(SyntaxNodeAnalysisContext context)
{
TrailingCommaStyle style = context.GetTrailingCommaStyle();

if (style == TrailingCommaStyle.None)
return;

var objectCreation = (CollectionExpressionSyntax)context.Node;

SeparatedSyntaxList<CollectionElementSyntax> elements = objectCreation.Elements;

if (!elements.Any())
return;

int count = elements.Count;
int separatorCount = elements.SeparatorCount;

if (count == separatorCount)
{
if (style == TrailingCommaStyle.Omit)
{
ReportRemove(context, elements.GetSeparator(count - 1));
}
else if (style == TrailingCommaStyle.OmitWhenSingleLine
&& elements.IsSingleLine(cancellationToken: context.CancellationToken))
{
ReportRemove(context, elements.GetSeparator(count - 1));
}
}
else if (separatorCount == count - 1)
{
if (style == TrailingCommaStyle.Include)
{
ReportAdd(context, elements.Last());
}
else if (style == TrailingCommaStyle.OmitWhenSingleLine
&& !elements.IsSingleLine(cancellationToken: context.CancellationToken))
{
ReportAdd(context, elements.Last());
}
}
}

private static void ReportAdd(SyntaxNodeAnalysisContext context, SyntaxNode lastNode)
{
DiagnosticHelpers.ReportDiagnostic(
Expand Down
44 changes: 44 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1260AddOrRemoveTrailingCommaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,48 @@ void M()
}
""", options: Options.AddConfigOption(ConfigOptionKeys.TrailingCommaStyle, ConfigOptionValues.TrailingCommaStyle_OmitWhenSingleLine));
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddOrRemoveTrailingComma)]
public async Task Test_CollectionExpression_Omit()
{
await VerifyDiagnosticAndFixAsync("""
class C
{
void M()
{
int[] x = [1, 2, 3[|,|]];
}
}
""", """
class C
{
void M()
{
int[] x = [1, 2, 3];
}
}
""", options: Options.AddConfigOption(ConfigOptionKeys.TrailingCommaStyle, ConfigOptionValues.TrailingCommaStyle_Omit));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddOrRemoveTrailingComma)]
public async Task Test_CollectionExpression_OmitWhenSingleLine()
{
await VerifyDiagnosticAndFixAsync("""
class C
{
void M()
{
int[] x = [1, 2, 3[|,|]];
}
}
""", """
class C
{
void M()
{
int[] x = [1, 2, 3];
}
}
""", options: Options.AddConfigOption(ConfigOptionKeys.TrailingCommaStyle, ConfigOptionValues.TrailingCommaStyle_OmitWhenSingleLine));
}

}
Loading