Skip to content

Commit 24755d2

Browse files
authored
Merge pull request #3317 from sharwell/with-init
Update SA1118 to allow 'with' expressions
2 parents 28795c0 + 4239b71 commit 24755d2

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1118CSharp9UnitTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,77 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<StyleCop.Analyzers.ReadabilityRules.SA1118ParameterMustNotSpanMultipleLines>;
713

814
public class SA1118CSharp9UnitTests : SA1118CSharp8UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3314, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3314")]
18+
public async Task TestWithExpressionAsync()
19+
{
20+
var testCode = @"
21+
class Foo
22+
{
23+
public record R(int X, int Y);
24+
25+
public void FunA(params object[] j)
26+
{
27+
}
28+
29+
public void FunB(R r)
30+
{
31+
FunA(
32+
1,
33+
r with
34+
{
35+
X = 1,
36+
});
37+
}
38+
}";
39+
40+
await new CSharpTest(LanguageVersion.CSharp9)
41+
{
42+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
43+
TestCode = testCode,
44+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
45+
}
46+
47+
[Fact]
48+
[WorkItem(3314, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3314")]
49+
public async Task TestWithExpression2Async()
50+
{
51+
var testCode = @"
52+
class Foo
53+
{
54+
public record R(int X, int Y);
55+
56+
public void FunA(params object[] j)
57+
{
58+
}
59+
60+
public void FunB(R r)
61+
{
62+
FunA(
63+
1,
64+
r with
65+
{
66+
X = 1,
67+
},
68+
2);
69+
}
70+
}";
71+
72+
await new CSharpTest(LanguageVersion.CSharp9)
73+
{
74+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
75+
TestCode = testCode,
76+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
77+
}
1078
}
1179
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
1212
using Microsoft.CodeAnalysis.Diagnostics;
1313
using StyleCop.Analyzers.Helpers;
14+
using StyleCop.Analyzers.Lightup;
1415

1516
/// <summary>
1617
/// A parameter to a C# method or indexer, other than the first parameter, spans across multiple lines.
@@ -82,6 +83,7 @@ internal class SA1118ParameterMustNotSpanMultipleLines : DiagnosticAnalyzer
8283
SyntaxKind.AnonymousObjectCreationExpression,
8384
SyntaxKind.ArrayCreationExpression,
8485
SyntaxKind.ImplicitArrayCreationExpression,
86+
SyntaxKindEx.WithExpression,
8587
};
8688

8789
/// <inheritdoc/>

documentation/SA1118.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ cases:
3030
* The first parameter may span multiple lines
3131
* Anonymous methods (including lambda expressions) may span multiple lines
3232
* Invocation expressions may span multiple lines
33-
* Object creation expressions may span multiple lines
33+
* Object and array creation expressions may span multiple lines
34+
* `with` expressions (C# 9) may span multiple lines
3435

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

0 commit comments

Comments
 (0)