Skip to content

Commit 3dbbe30

Browse files
authored
Merge pull request #2608 from sharwell/allow-single-line-arrays
Allow single-line ArrayCreationExpression syntax
2 parents 5156b7e + 621ee19 commit 3dbbe30

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

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

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public partial class SA1500UnitTests
2525
public async Task TestArrayInitializersValidAsync()
2626
{
2727
var testCode = @"
28+
using System;
29+
2830
public class TestClass
2931
{
3032
private int[] array1 = { };
@@ -33,6 +35,8 @@ public class TestClass
3335
3436
private int[] array3 = new[] { 0, 1 };
3537
38+
private int[] array3b = new int[] { 0, 1 };
39+
3640
private int[] array4 =
3741
{
3842
};
@@ -70,18 +74,22 @@ public void TestMethod()
7074
{
7175
0,
7276
};
77+
78+
Console.WriteLine(new[] { 0, 1 });
79+
Console.WriteLine(new int[] { 0, 1 });
80+
Console.WriteLine(new int[] { });
7381
}
7482
}";
7583

7684
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
7785
}
7886

7987
/// <summary>
80-
/// Verifies that diagnostics will be reported for all invalid array initializer definitions.
88+
/// Verifies that diagnostics will be reported for all invalid implicit array initializer definitions.
8189
/// </summary>
8290
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
8391
[Fact]
84-
public async Task TestArrayInitializersInvalidAsync()
92+
public async Task TestImplicitArrayInitializersInvalidAsync()
8593
{
8694
var testCode = @"
8795
public class TestClass
@@ -243,5 +251,96 @@ public void TestMethod()
243251
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
244252
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
245253
}
254+
255+
/// <summary>
256+
/// Verifies that diagnostics will be reported for all invalid array initializer definitions.
257+
/// </summary>
258+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
259+
[Fact]
260+
[WorkItem(2607, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2607")]
261+
public async Task TestArrayInitializersInvalidAsync()
262+
{
263+
var testCode = @"
264+
public class TestClass
265+
{
266+
private int[] invalidArray5 = new int[]
267+
{ 0, 1 };
268+
269+
private int[] invalidArray6 = new int[]
270+
{ 0, 1
271+
};
272+
273+
private int[] invalidArray7 = new int[]
274+
{
275+
0, 1 };
276+
277+
public void TestMethod()
278+
{
279+
var invalidArray12 = new int[]
280+
{ 0, 1 };
281+
282+
var invalidArray13 = new int[]
283+
{ 0, 1
284+
};
285+
286+
var invalidArray14 = new int[]
287+
{
288+
0, 1 };
289+
}
290+
}";
291+
292+
var fixedTestCode = @"
293+
public class TestClass
294+
{
295+
private int[] invalidArray5 = new int[]
296+
{
297+
0, 1
298+
};
299+
300+
private int[] invalidArray6 = new int[]
301+
{
302+
0, 1
303+
};
304+
305+
private int[] invalidArray7 = new int[]
306+
{
307+
0, 1
308+
};
309+
310+
public void TestMethod()
311+
{
312+
var invalidArray12 = new int[]
313+
{
314+
0, 1
315+
};
316+
317+
var invalidArray13 = new int[]
318+
{
319+
0, 1
320+
};
321+
322+
var invalidArray14 = new int[]
323+
{
324+
0, 1
325+
};
326+
}
327+
}";
328+
329+
DiagnosticResult[] expectedDiagnostics =
330+
{
331+
this.CSharpDiagnostic().WithLocation(5, 9),
332+
this.CSharpDiagnostic().WithLocation(5, 16),
333+
this.CSharpDiagnostic().WithLocation(8, 9),
334+
this.CSharpDiagnostic().WithLocation(13, 18),
335+
this.CSharpDiagnostic().WithLocation(18, 13),
336+
this.CSharpDiagnostic().WithLocation(18, 20),
337+
this.CSharpDiagnostic().WithLocation(21, 13),
338+
this.CSharpDiagnostic().WithLocation(26, 22),
339+
};
340+
341+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
342+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
343+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
344+
}
246345
}
247346
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ private static void CheckBraces(SyntaxNodeAnalysisContext context, SyntaxToken o
155155

156156
break;
157157

158+
case SyntaxKind.ArrayCreationExpression:
159+
if (GetStartLine(((ArrayCreationExpressionSyntax)context.Node.Parent).NewKeyword) == GetStartLine(openBraceToken))
160+
{
161+
return;
162+
}
163+
164+
break;
165+
158166
case SyntaxKind.ImplicitArrayCreationExpression:
159167
if (GetStartLine(((ImplicitArrayCreationExpressionSyntax)context.Node.Parent).NewKeyword) == GetStartLine(openBraceToken))
160168
{

0 commit comments

Comments
 (0)