Skip to content

Commit e523335

Browse files
authored
Merge pull request #2659 from morsiu/#2527
SA1137: Each switch case with a block is checked separately
2 parents de3d96c + c9f2604 commit e523335

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,18 +1460,37 @@ void MethodName()
14601460
14611461
break;
14621462
1463-
case 1:
1464-
case 2:
1465-
label2:
1463+
case 1:
1464+
{
1465+
label2:
1466+
if (true)
1467+
{
1468+
}
1469+
1470+
break;
1471+
}
1472+
case 2:
1473+
case 3:
1474+
label3:
14661475
while (true)
14671476
{
14681477
}
14691478
14701479
break;
14711480
1481+
case 4:
1482+
case 5:
1483+
{
1484+
label4:
1485+
while (true)
1486+
{
1487+
}
1488+
1489+
break;
1490+
}
14721491
default:
1473-
label3a:
1474-
label3b:
1492+
label5a:
1493+
label5b:
14751494
break;
14761495
}
14771496
}
@@ -1493,17 +1512,36 @@ void MethodName()
14931512
break;
14941513
14951514
case 1:
1515+
{
1516+
label2:
1517+
if (true)
1518+
{
1519+
}
1520+
1521+
break;
1522+
}
14961523
case 2:
1497-
label2:
1524+
case 3:
1525+
label3:
14981526
while (true)
14991527
{
15001528
}
15011529
15021530
break;
15031531
1532+
case 4:
1533+
case 5:
1534+
{
1535+
label4:
1536+
while (true)
1537+
{
1538+
}
1539+
1540+
break;
1541+
}
15041542
default:
1505-
label3a:
1506-
label3b:
1543+
label5a:
1544+
label5b:
15071545
break;
15081546
}
15091547
}
@@ -1513,15 +1551,19 @@ void MethodName()
15131551
DiagnosticResult[] expected =
15141552
{
15151553
this.CSharpDiagnostic().WithLocation(14, 1),
1516-
this.CSharpDiagnostic().WithLocation(16, 1),
1517-
this.CSharpDiagnostic().WithLocation(17, 1),
1518-
this.CSharpDiagnostic().WithLocation(18, 1),
1519-
this.CSharpDiagnostic().WithLocation(19, 1),
15201554
this.CSharpDiagnostic().WithLocation(23, 1),
15211555
this.CSharpDiagnostic().WithLocation(25, 1),
15221556
this.CSharpDiagnostic().WithLocation(26, 1),
15231557
this.CSharpDiagnostic().WithLocation(27, 1),
15241558
this.CSharpDiagnostic().WithLocation(28, 1),
1559+
this.CSharpDiagnostic().WithLocation(32, 1),
1560+
this.CSharpDiagnostic().WithLocation(34, 1),
1561+
this.CSharpDiagnostic().WithLocation(35, 1),
1562+
this.CSharpDiagnostic().WithLocation(36, 1),
1563+
this.CSharpDiagnostic().WithLocation(44, 1),
1564+
this.CSharpDiagnostic().WithLocation(45, 1),
1565+
this.CSharpDiagnostic().WithLocation(46, 1),
1566+
this.CSharpDiagnostic().WithLocation(47, 1),
15251567
};
15261568

15271569
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,16 @@ private static void HandleSwitchStatement(SyntaxNodeAnalysisContext context)
217217
var labels = ImmutableList.CreateBuilder<SwitchLabelSyntax>();
218218
var statements = ImmutableList.CreateBuilder<StatementSyntax>();
219219
var labeledStatements = ImmutableList.CreateBuilder<StatementSyntax>();
220+
var blockStatements = ImmutableList.CreateBuilder<StatementSyntax>();
220221
foreach (SwitchSectionSyntax switchSection in switchStatement.Sections)
221222
{
222223
labels.AddRange(switchSection.Labels);
224+
if (switchSection.Statements.Count == 1 && switchSection.Statements[0].IsKind(SyntaxKind.Block))
225+
{
226+
blockStatements.Add(switchSection.Statements[0]);
227+
continue;
228+
}
229+
223230
foreach (var statement in switchSection.Statements)
224231
{
225232
StatementSyntax statementToAlign = statement;
@@ -236,6 +243,7 @@ private static void HandleSwitchStatement(SyntaxNodeAnalysisContext context)
236243
CheckElements(context, labels.ToImmutable());
237244
CheckElements(context, statements.ToImmutable());
238245
CheckElements(context, labeledStatements.ToImmutable());
246+
CheckElements(context, blockStatements.ToImmutable());
239247
}
240248

241249
private static void HandleInitializerExpression(SyntaxNodeAnalysisContext context)

0 commit comments

Comments
 (0)