Skip to content

Commit 19aefb8

Browse files
authored
Merge pull request #3216 from pantosha/fix-2916
Update SA1118 to support multiline array creation
2 parents fe00f94 + ff19b90 commit 19aefb8

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1118UnitTests.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.Test.ReadabilityRules
55
{
6+
using System.Collections.Generic;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.CodeAnalysis.Testing;
@@ -12,6 +13,26 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
1213

1314
public class SA1118UnitTests
1415
{
16+
public static IEnumerable<object[]> ArrayCreationExpressions { get; } = new List<object[]>
17+
{
18+
new object[]
19+
{
20+
@"new[]
21+
{
22+
0,
23+
1
24+
}",
25+
},
26+
new object[]
27+
{
28+
@"new int[]
29+
{
30+
0,
31+
1
32+
}",
33+
},
34+
};
35+
1536
[Fact]
1637
public async Task TestMethodCallWithTwoParametersSecondSpansMoreThanOneLineAsync()
1738
{
@@ -276,7 +297,7 @@ public void Bar()
276297
}
277298

278299
[Fact]
279-
public async Task TestLambdaCallSecondParameterIsAnonynousMethodAsync()
300+
public async Task TestLambdaCallSecondParameterIsAnonymousMethodAsync()
280301
{
281302
var testCode = @"
282303
class Foo
@@ -341,7 +362,7 @@ public void Bar()
341362
}
342363

343364
[Fact]
344-
public async Task TestAttributeSecondParameterSpandsMultipleLinesAsync()
365+
public async Task TestAttributeSecondParameterSpansMultipleLinesAsync()
345366
{
346367
var testCode = @"
347368
[System.AttributeUsage(System.AttributeTargets.Class,AllowMultiple = true)]
@@ -363,5 +384,27 @@ public class Foo
363384

364385
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
365386
}
387+
388+
[Theory]
389+
[MemberData(nameof(ArrayCreationExpressions))]
390+
public async Task TestArrayCreationSpansMultipleLinesAsync(string arrayCreationExpression)
391+
{
392+
var testCode = $@"
393+
class Foo
394+
{{
395+
public void Fun(int i, int[] j)
396+
{{
397+
}}
398+
399+
public void Bar()
400+
{{
401+
Fun(
402+
1,
403+
{arrayCreationExpression});
404+
}}
405+
}}";
406+
407+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
408+
}
366409
}
367410
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1118ParameterMustNotSpanMultipleLines.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ internal class SA1118ParameterMustNotSpanMultipleLines : DiagnosticAnalyzer
8080
SyntaxKind.InvocationExpression,
8181
SyntaxKind.ObjectCreationExpression,
8282
SyntaxKind.AnonymousObjectCreationExpression,
83+
SyntaxKind.ArrayCreationExpression,
84+
SyntaxKind.ImplicitArrayCreationExpression,
8385
};
8486

8587
/// <inheritdoc/>
@@ -131,10 +133,10 @@ private static bool CheckIfArgumentIsMultiline(CSharpSyntaxNode argument)
131133
return lineSpan.EndLinePosition.Line > lineSpan.StartLinePosition.Line;
132134
}
133135

134-
private static bool IsArgumentOnExceptionList(ExpressionSyntax argumentExpresson)
136+
private static bool IsArgumentOnExceptionList(ExpressionSyntax argumentExpression)
135137
{
136-
return argumentExpresson != null
137-
&& ArgumentExceptionSyntaxKinds.Any(argumentExpresson.IsKind);
138+
return argumentExpression != null
139+
&& ArgumentExceptionSyntaxKinds.Any(argumentExpression.IsKind);
138140
}
139141
}
140142
}

0 commit comments

Comments
 (0)