Skip to content

Commit c6fb55b

Browse files
committed
Merge pull request #1819 from sharwell/fix-1597
Allow invocation expressions to span multiple lines
2 parents 784cfff + 3741760 commit c6fb55b

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ public void Bar()
9090
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
9191
}
9292

93+
[Fact]
94+
public async Task TestMethodCallWithThreeParametersSecondSpansMultipleLinesThirdSpansMultipleLinesButIsInvocationExpressionAsync()
95+
{
96+
var testCode = @"
97+
class Foo
98+
{
99+
public void Fun(int i, int j, int k)
100+
{
101+
}
102+
103+
public void Bar()
104+
{
105+
Fun(1,
106+
System.Linq.Enumerable.Count(
107+
new int[0]) + 4,
108+
System.Linq.Enumerable.Count(
109+
new int[0]));
110+
}
111+
}";
112+
113+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(11, 13);
114+
115+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
116+
}
117+
93118
[Fact]
94119
public async Task TestMethodCallWithTwoParametersFirstIsMultilineSecondIsOneLineAsync()
95120
{
@@ -156,6 +181,31 @@ public void Bar()
156181
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
157182
}
158183

184+
[Fact]
185+
public async Task TestAnonymousMethodCallSecondParameterSpansMultipleLinesThirdParameterIsInvocationAsync()
186+
{
187+
var testCode = @"
188+
class Foo
189+
{
190+
public void Bar()
191+
{
192+
System.Action<int, int, int> d = delegate(int i, int j, int k)
193+
{
194+
195+
};
196+
d(1,
197+
System.Linq.Enumerable.Count(
198+
new int[0]) + 1,
199+
System.Linq.Enumerable.Count(
200+
new int[0]));
201+
}
202+
}";
203+
204+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(11, 11);
205+
206+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
207+
}
208+
159209
[Fact]
160210
public async Task TestLambdaCallSecondParameterSpansTwoLinesAsync()
161211
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ internal class SA1118ParameterMustNotSpanMultipleLines : DiagnosticAnalyzer
8080
{
8181
SyntaxKind.AnonymousMethodExpression,
8282
SyntaxKind.ParenthesizedLambdaExpression,
83-
SyntaxKind.SimpleLambdaExpression
83+
SyntaxKind.SimpleLambdaExpression,
84+
SyntaxKind.InvocationExpression
8485
};
8586

8687
/// <inheritdoc/>

documentation/SA1118.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ A parameter to a C# method or indexer, other than the first parameter, spans acr
2121

2222
## Rule description
2323

24-
To prevent method calls from becoming excessively complicated and unreadable, only the first parameter to a method or indexer call is allowed to span across multiple lines. The exception is an anonymous method passed as a parameter, which is always allowed to span multiple lines. A violation of this rule occurs whenever a parameter other than the first parameter spans across multiple lines, and the parameter does not contain an anonymous method.
24+
To prevent method calls from becoming excessively complicated and unreadable, individual parameters and arguments should
25+
be placed on a single line. When parameters other than the first parameter span across multiple lines, it can be
26+
difficult to tell how many parameters are passed to the method. In general, the code becomes difficult to read. A
27+
violation of this rule is reported when a parameter or argument spans multiple lines, except in the following specific
28+
cases:
29+
30+
* The first parameter may span multiple lines
31+
* Anonymous methods (including lambda expressions) may span multiple lines
32+
* Invocation expressions may span multiple lines
2533

2634
For example, the following code would violate this rule, since the second parameter spans across multiple lines:
2735

@@ -32,9 +40,9 @@ return JoinStrings(
3240
" Doe");
3341
```
3442

35-
When parameters other than the first parameter span across multiple lines, it can be difficult to tell how many parameters are passed to the method. In general, the code becomes difficult to read.
36-
37-
To fix the example above, ensure that the parameters after the first parameter do not span across multiple lines. If this will cause a parameter to be excessively long, store the value of the parameter within a temporary variable. For example:
43+
To fix the example above, ensure that the parameters after the first parameter do not span across multiple lines. If
44+
this will cause a parameter to be excessively long, store the value of the parameter within a temporary variable. For
45+
example:
3846

3947
```csharp
4048
string last = "Smith" +
@@ -53,7 +61,8 @@ return JoinStrings("John", last);
5361

5462
## How to fix violations
5563

56-
To fix a violation of this rule, ensure that the parameters after the first parameter do not span multiple lines, unless the parameter contains an anonymous method.
64+
To fix a violation of this rule, ensure that the parameters and arguments do not span multiple lines, except in the
65+
specific cases listed above.
5766

5867
## How to suppress violations
5968

0 commit comments

Comments
 (0)