Skip to content

Commit 1b49c76

Browse files
committed
Fix SA1413 handling of single-line enum declarations
Fixes #2467
1 parent f5b2fff commit 1b49c76

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1413UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,24 @@ public async Task VerifyEnumWithoutTrailingCommaAsync()
488488
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
489489
}
490490

491+
[Fact]
492+
[WorkItem(2467, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2467")]
493+
public async Task VerifySingleLineEnumWithoutCommaAsync()
494+
{
495+
// SA1413 is not reported whether or not the comma is included for single-line enums
496+
var testCode = @"enum TestEnum { One, Two }";
497+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
498+
}
499+
500+
[Fact]
501+
[WorkItem(2467, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2467")]
502+
public async Task VerifySingleLineEnumWithCommaAsync()
503+
{
504+
// SA1413 is not reported whether or not the comma is included for single-line enums
505+
var testCode = @"enum TestEnum { One, Two, }";
506+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
507+
}
508+
491509
/// <summary>
492510
/// Verifies that the last value of an enum without a trailing comma produces a diagnostic.
493511
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static void HandleEnumDeclaration(SyntaxNodeAnalysisContext context)
8282
{
8383
var initializer = (EnumDeclarationSyntax)context.Node;
8484
var lastMember = initializer.Members.LastOrDefault();
85-
if (lastMember == null)
85+
if (lastMember == null || !initializer.SpansMultipleLines())
8686
{
8787
return;
8888
}

0 commit comments

Comments
 (0)