Skip to content

Commit 34151d6

Browse files
committed
Update SA1113 for target-typed new
1 parent 3a75847 commit 34151d6

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

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

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

44
namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;
10+
using StyleCop.Analyzers.Test.Helpers;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.ReadabilityRules.SA1113CommaMustBeOnSameLineAsPreviousParameter,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public partial class SA1113CSharp9UnitTests : SA1113CSharp8UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3972, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3972")]
20+
public async Task TestTargetTypedNewCommaPlacementAsync()
21+
{
22+
var testCode = @"
23+
class TestClass
24+
{
25+
public TestClass(int first, int second)
26+
{
27+
}
28+
}
29+
30+
class Test
31+
{
32+
void M()
33+
{
34+
TestClass value = new(1
35+
{|#0:,|} 2);
36+
}
37+
}";
38+
39+
var fixedCode = @"
40+
class TestClass
41+
{
42+
public TestClass(int first, int second)
43+
{
44+
}
45+
}
46+
47+
class Test
48+
{
49+
void M()
50+
{
51+
TestClass value = new(1,
52+
2);
53+
}
54+
}";
55+
56+
var expected = Diagnostic().WithLocation(0);
57+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
58+
}
1059
}
1160
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1113CommaMustBeOnSameLineAsPreviousParameter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ internal class SA1113CommaMustBeOnSameLineAsPreviousParameter : DiagnosticAnalyz
6363
private static readonly Action<SyntaxNodeAnalysisContext> LocalFunctionStatementAction = HandleLocalFunctionStatement;
6464
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;
6565
private static readonly Action<SyntaxNodeAnalysisContext> ObjectCreationExpressionAction = HandleObjectCreationExpression;
66+
private static readonly Action<SyntaxNodeAnalysisContext> ImplicitObjectCreationExpressionAction = HandleImplicitObjectCreationExpression;
6667
private static readonly Action<SyntaxNodeAnalysisContext> IndexerDeclarationAction = HandleIndexerDeclaration;
6768
private static readonly Action<SyntaxNodeAnalysisContext> ElementAccessExpressionAction = HandleElementAccessExpression;
6869
private static readonly Action<SyntaxNodeAnalysisContext> AnonymousMethodExpressionAction = HandleAnonymousMethodExpression;
@@ -87,6 +88,7 @@ public override void Initialize(AnalysisContext context)
8788
context.RegisterSyntaxNodeAction(LocalFunctionStatementAction, SyntaxKindEx.LocalFunctionStatement);
8889
context.RegisterSyntaxNodeAction(InvocationExpressionAction, SyntaxKind.InvocationExpression);
8990
context.RegisterSyntaxNodeAction(ObjectCreationExpressionAction, SyntaxKind.ObjectCreationExpression);
91+
context.RegisterSyntaxNodeAction(ImplicitObjectCreationExpressionAction, SyntaxKindEx.ImplicitObjectCreationExpression);
9092
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
9193
context.RegisterSyntaxNodeAction(ElementAccessExpressionAction, SyntaxKind.ElementAccessExpression);
9294
context.RegisterSyntaxNodeAction(AnonymousMethodExpressionAction, SyntaxKind.AnonymousMethodExpression);
@@ -187,6 +189,12 @@ private static void HandleObjectCreationExpression(SyntaxNodeAnalysisContext con
187189
HandleBaseArgumentListSyntax(context, objectCreationExpression.ArgumentList);
188190
}
189191

192+
private static void HandleImplicitObjectCreationExpression(SyntaxNodeAnalysisContext context)
193+
{
194+
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntaxWrapper)context.Node;
195+
HandleBaseArgumentListSyntax(context, implicitObjectCreation.ArgumentList);
196+
}
197+
190198
private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context)
191199
{
192200
var invocationEpression = (InvocationExpressionSyntax)context.Node;

documentation/SA1113.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ A comma between two parameters in a call to a C# method or indexer, or in the de
2323

2424
A violation of this rule occurs when a comma between two parameters to a method or indexer is not placed on the same line as the previous parameter. The following examples show correct placement of the comma:
2525

26+
The same guidance applies to constructor invocations and object creation expressions, including target-typed `new` introduced in C# 9. Commas separating arguments to `new(...)` must appear on the same line as the preceding argument.
27+
2628
```csharp
2729
public string JoinName(string first, string last)
2830
{

0 commit comments

Comments
 (0)