Skip to content

Commit 949f53f

Browse files
vweijsterssharwell
authored andcommitted
Fixes SA1500 for array initializers
1 parent 50eb92d commit 949f53f

4 files changed

Lines changed: 308 additions & 27 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,15 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Document
122122

123123
if (!braceTokens.Contains(nextToken))
124124
{
125-
int newIndentationSteps;
125+
int newIndentationSteps = indentationSteps;
126126
if (braceToken.IsKind(SyntaxKind.OpenBraceToken))
127127
{
128-
newIndentationSteps = indentationSteps + 1;
128+
newIndentationSteps++;
129129
}
130-
else if (nextToken.IsKind(SyntaxKind.CloseBraceToken))
131-
{
132-
newIndentationSteps = Math.Max(0, indentationSteps - 1);
133-
}
134-
else
130+
131+
if (nextToken.IsKind(SyntaxKind.CloseBraceToken))
135132
{
136-
newIndentationSteps = indentationSteps;
133+
newIndentationSteps = Math.Max(0, newIndentationSteps - 1);
137134
}
138135

139136
AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, newIndentationSteps)));
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.LayoutRules
5+
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using StyleCop.Analyzers.LayoutRules;
9+
using TestHelper;
10+
using Xunit;
11+
12+
/// <summary>
13+
/// Unit tests for <see cref="SA1500BracesForMultiLineStatementsMustNotShareLine"/>.
14+
/// </summary>
15+
public partial class SA1500UnitTests
16+
{
17+
/// <summary>
18+
/// Verifies that no diagnostics are reported for the valid array initializers defined in this test.
19+
/// </summary>
20+
/// <remarks>
21+
/// These are valid for SA1500 only, some will report other diagnostics.
22+
/// </remarks>
23+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
24+
[Fact]
25+
public async Task TestArrayInitializersValidAsync()
26+
{
27+
var testCode = @"
28+
public class TestClass
29+
{
30+
private int[] array1 = { };
31+
32+
private int[] array2 = { 0, 1 };
33+
34+
private int[] array3 = new[] { 0, 1 };
35+
36+
private int[] array4 =
37+
{
38+
};
39+
40+
private int[] array5 =
41+
{
42+
0,
43+
1,
44+
};
45+
46+
private int[] array6 = new[]
47+
{
48+
0,
49+
};
50+
51+
public void TestMethod()
52+
{
53+
int[] array7 = { };
54+
55+
int[] array8 = { 0, 1 };
56+
57+
var array9 = new[] { 0, 1 };
58+
59+
int[] array10 =
60+
{
61+
};
62+
63+
int[] array11 =
64+
{
65+
0,
66+
1,
67+
};
68+
69+
var array12 = new[]
70+
{
71+
0,
72+
};
73+
}
74+
}";
75+
76+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
77+
}
78+
79+
/// <summary>
80+
/// Verifies that diagnostics will be reported for all invalid array initializer definitions.
81+
/// </summary>
82+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
83+
[Fact]
84+
public async Task TestArrayInitializersInvalidAsync()
85+
{
86+
var testCode = @"
87+
public class TestClass
88+
{
89+
private int[] invalidArray1 =
90+
{ };
91+
92+
private int[] invalidArray2 =
93+
{ 0, 1 };
94+
95+
private int[] invalidArray3 =
96+
{ 0, 1
97+
};
98+
99+
private int[] invalidArray4 =
100+
{
101+
0, 1 };
102+
103+
private int[] invalidArray5 = new[]
104+
{ 0, 1 };
105+
106+
private int[] invalidArray6 = new[]
107+
{ 0, 1
108+
};
109+
110+
private int[] invalidArray7 = new[]
111+
{
112+
0, 1 };
113+
114+
public void TestMethod()
115+
{
116+
int[] invalidArray8 =
117+
{ };
118+
119+
int[] invalidArray9 =
120+
{ 0, 1 };
121+
122+
int[] invalidArray10 =
123+
{ 0, 1
124+
};
125+
126+
int[] invalidArray11 =
127+
{
128+
0, 1 };
129+
130+
var invalidArray12 = new[]
131+
{ 0, 1 };
132+
133+
var invalidArray13 = new[]
134+
{ 0, 1
135+
};
136+
137+
var invalidArray14 = new[]
138+
{
139+
0, 1 };
140+
}
141+
}";
142+
143+
var fixedTestCode = @"
144+
public class TestClass
145+
{
146+
private int[] invalidArray1 =
147+
{
148+
};
149+
150+
private int[] invalidArray2 =
151+
{
152+
0, 1
153+
};
154+
155+
private int[] invalidArray3 =
156+
{
157+
0, 1
158+
};
159+
160+
private int[] invalidArray4 =
161+
{
162+
0, 1
163+
};
164+
165+
private int[] invalidArray5 = new[]
166+
{
167+
0, 1
168+
};
169+
170+
private int[] invalidArray6 = new[]
171+
{
172+
0, 1
173+
};
174+
175+
private int[] invalidArray7 = new[]
176+
{
177+
0, 1
178+
};
179+
180+
public void TestMethod()
181+
{
182+
int[] invalidArray8 =
183+
{
184+
};
185+
186+
int[] invalidArray9 =
187+
{
188+
0, 1
189+
};
190+
191+
int[] invalidArray10 =
192+
{
193+
0, 1
194+
};
195+
196+
int[] invalidArray11 =
197+
{
198+
0, 1
199+
};
200+
201+
var invalidArray12 = new[]
202+
{
203+
0, 1
204+
};
205+
206+
var invalidArray13 = new[]
207+
{
208+
0, 1
209+
};
210+
211+
var invalidArray14 = new[]
212+
{
213+
0, 1
214+
};
215+
}
216+
}";
217+
218+
DiagnosticResult[] expectedDiagnostics =
219+
{
220+
this.CSharpDiagnostic().WithLocation(5, 9),
221+
this.CSharpDiagnostic().WithLocation(5, 11),
222+
this.CSharpDiagnostic().WithLocation(8, 9),
223+
this.CSharpDiagnostic().WithLocation(8, 16),
224+
this.CSharpDiagnostic().WithLocation(11, 9),
225+
this.CSharpDiagnostic().WithLocation(16, 18),
226+
this.CSharpDiagnostic().WithLocation(19, 9),
227+
this.CSharpDiagnostic().WithLocation(19, 16),
228+
this.CSharpDiagnostic().WithLocation(22, 9),
229+
this.CSharpDiagnostic().WithLocation(27, 18),
230+
this.CSharpDiagnostic().WithLocation(32, 13),
231+
this.CSharpDiagnostic().WithLocation(32, 15),
232+
this.CSharpDiagnostic().WithLocation(35, 13),
233+
this.CSharpDiagnostic().WithLocation(35, 20),
234+
this.CSharpDiagnostic().WithLocation(38, 13),
235+
this.CSharpDiagnostic().WithLocation(43, 22),
236+
this.CSharpDiagnostic().WithLocation(46, 13),
237+
this.CSharpDiagnostic().WithLocation(46, 20),
238+
this.CSharpDiagnostic().WithLocation(49, 13),
239+
this.CSharpDiagnostic().WithLocation(54, 22),
240+
};
241+
242+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
243+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
244+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
245+
}
246+
}
247+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1500/SA1500UnitTests.Properties.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ public bool Property6
9191
9292
// Valid property #8 (Valid for SA1500 only)
9393
public int[] Property8 { get; set; } = { 0, 1, 2 };
94-
95-
// Valid property #9 (Valid for SA1500 only)
96-
public int[] Property9 { get; set; } =
97-
{ 0, 1, 2 };
9894
}";
9995

10096
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
@@ -219,6 +215,10 @@ public bool Property11
219215
// Invalid property #14
220216
public int[] Property14 { get; set; } = { 0, 1, 2
221217
};
218+
219+
// Invalid property #15
220+
public int[] Property15 { get; set; } =
221+
{ 0, 1, 2 };
222222
}";
223223

224224
var fixedTestCode = @"using System;
@@ -356,6 +356,12 @@ public bool Property11
356356
{
357357
0, 1, 2
358358
};
359+
360+
// Invalid property #15
361+
public int[] Property15 { get; set; } =
362+
{
363+
0, 1, 2
364+
};
359365
}";
360366

361367
DiagnosticResult[] expectedDiagnostics =
@@ -410,6 +416,10 @@ public bool Property11
410416

411417
// Invalid property #14
412418
this.CSharpDiagnostic().WithLocation(111, 45),
419+
420+
// Invalid property #15
421+
this.CSharpDiagnostic().WithLocation(116, 5),
422+
this.CSharpDiagnostic().WithLocation(116, 15),
413423
};
414424

415425
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,52 @@ private static void CheckBraces(SyntaxNodeAnalysisContext context, SyntaxToken o
143143

144144
if (GetStartLine(openBraceToken) == GetStartLine(closeBraceToken))
145145
{
146-
switch (context.Node.Parent.Kind())
146+
if (context.Node.IsKind(SyntaxKind.ArrayInitializerExpression))
147147
{
148-
case SyntaxKind.GetAccessorDeclaration:
149-
case SyntaxKind.SetAccessorDeclaration:
150-
case SyntaxKind.AddAccessorDeclaration:
151-
case SyntaxKind.RemoveAccessorDeclaration:
152-
case SyntaxKind.UnknownAccessorDeclaration:
153-
if (GetStartLine(((AccessorDeclarationSyntax)context.Node.Parent).Keyword) == GetStartLine(openBraceToken))
148+
switch (context.Node.Parent.Kind())
154149
{
155-
// reported as SA1504, if at all
156-
return;
157-
}
150+
case SyntaxKind.EqualsValueClause:
151+
if (GetStartLine(((EqualsValueClauseSyntax)context.Node.Parent).EqualsToken) == GetStartLine(openBraceToken))
152+
{
153+
return;
154+
}
158155

159-
checkCloseBrace = false;
160-
break;
156+
break;
161157

162-
default:
163-
// reported by SA1501 or SA1502
164-
return;
158+
case SyntaxKind.ImplicitArrayCreationExpression:
159+
if (GetStartLine(((ImplicitArrayCreationExpressionSyntax)context.Node.Parent).NewKeyword) == GetStartLine(openBraceToken))
160+
{
161+
return;
162+
}
163+
164+
break;
165+
166+
default:
167+
break;
168+
}
169+
}
170+
else
171+
{
172+
switch (context.Node.Parent.Kind())
173+
{
174+
case SyntaxKind.GetAccessorDeclaration:
175+
case SyntaxKind.SetAccessorDeclaration:
176+
case SyntaxKind.AddAccessorDeclaration:
177+
case SyntaxKind.RemoveAccessorDeclaration:
178+
case SyntaxKind.UnknownAccessorDeclaration:
179+
if (GetStartLine(((AccessorDeclarationSyntax)context.Node.Parent).Keyword) == GetStartLine(openBraceToken))
180+
{
181+
// reported as SA1504, if at all
182+
return;
183+
}
184+
185+
checkCloseBrace = false;
186+
break;
187+
188+
default:
189+
// reported by SA1501 or SA1502
190+
return;
191+
}
165192
}
166193
}
167194

0 commit comments

Comments
 (0)