Skip to content

Commit 74390e9

Browse files
committed
Each switch case containing a single block child is checked independently for consistent indentation
1 parent 1c3c14d commit 74390e9

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

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

Lines changed: 53 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,18 @@ 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(44, 1),
1563+
this.CSharpDiagnostic().WithLocation(45, 1),
1564+
this.CSharpDiagnostic().WithLocation(46, 1),
1565+
this.CSharpDiagnostic().WithLocation(47, 1),
15251566
};
15261567

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ private static void HandleSwitchStatement(SyntaxNodeAnalysisContext context)
220220
foreach (SwitchSectionSyntax switchSection in switchStatement.Sections)
221221
{
222222
labels.AddRange(switchSection.Labels);
223+
if (switchSection.Statements.Count == 1 && switchSection.Statements[0].IsKind(SyntaxKind.Block))
224+
{
225+
continue;
226+
}
227+
223228
foreach (var statement in switchSection.Statements)
224229
{
225230
StatementSyntax statementToAlign = statement;

0 commit comments

Comments
 (0)