Skip to content

Commit 400d34a

Browse files
committed
Update SA1111 for target-typed new
1 parent b6e2036 commit 400d34a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,47 @@ public async Task TestPrimaryConstructorBaseListWithoutArgumentsAsync(string typ
9595
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
9696
}
9797

98+
[Fact]
99+
[WorkItem(3972, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3972")]
100+
public async Task TestTargetTypedNewClosingParenthesisOnNextLineAsync()
101+
{
102+
var testCode = @"
103+
class TestClass
104+
{
105+
public TestClass(int value)
106+
{
107+
}
108+
}
109+
110+
class Test
111+
{
112+
void M()
113+
{
114+
TestClass value = new(1
115+
{|#0:)|};
116+
}
117+
}";
118+
119+
var fixedCode = @"
120+
class TestClass
121+
{
122+
public TestClass(int value)
123+
{
124+
}
125+
}
126+
127+
class Test
128+
{
129+
void M()
130+
{
131+
TestClass value = new(1);
132+
}
133+
}";
134+
135+
var expected = Diagnostic().WithLocation(0);
136+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
137+
}
138+
98139
protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter()
99140
{
100141
return new[]

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1111ClosingParenthesisMustBeOnLineOfLastParameter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ internal class SA1111ClosingParenthesisMustBeOnLineOfLastParameter : DiagnosticA
6666
private static readonly Action<SyntaxNodeAnalysisContext> LocalFunctionStatementAction = HandleLocalFunctionStatement;
6767
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;
6868
private static readonly Action<SyntaxNodeAnalysisContext> ObjectCreationExpressionAction = HandleObjectCreationExpression;
69+
private static readonly Action<SyntaxNodeAnalysisContext> ImplicitObjectCreationExpressionAction = HandleImplicitObjectCreationExpression;
6970
private static readonly Action<SyntaxNodeAnalysisContext> IndexerDeclarationAction = HandleIndexerDeclaration;
7071
private static readonly Action<SyntaxNodeAnalysisContext> ElementAccessExpressionAction = HandleElementAccessExpression;
7172
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
@@ -90,6 +91,7 @@ public override void Initialize(AnalysisContext context)
9091
context.RegisterSyntaxNodeAction(LocalFunctionStatementAction, SyntaxKindEx.LocalFunctionStatement);
9192
context.RegisterSyntaxNodeAction(InvocationExpressionAction, SyntaxKind.InvocationExpression);
9293
context.RegisterSyntaxNodeAction(ObjectCreationExpressionAction, SyntaxKind.ObjectCreationExpression);
94+
context.RegisterSyntaxNodeAction(ImplicitObjectCreationExpressionAction, SyntaxKindEx.ImplicitObjectCreationExpression);
9395
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
9496
context.RegisterSyntaxNodeAction(ElementAccessExpressionAction, SyntaxKind.ElementAccessExpression);
9597
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
@@ -190,6 +192,12 @@ private static void HandleObjectCreationExpression(SyntaxNodeAnalysisContext con
190192
CheckArgumentList(context, objectCreation.ArgumentList);
191193
}
192194

195+
private static void HandleImplicitObjectCreationExpression(SyntaxNodeAnalysisContext context)
196+
{
197+
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntaxWrapper)context.Node;
198+
CheckArgumentList(context, implicitObjectCreation.ArgumentList);
199+
}
200+
193201
private static void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context)
194202
{
195203
var indexerDeclaration = (IndexerDeclarationSyntax)context.Node;

documentation/SA1111.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ The closing parenthesis or bracket in a call to a C# method or indexer, or the d
2323

2424
A violation of this rule occurs when the closing bracket of a method or indexer call or declaration is not placed on the same line as the last parameter. The following examples show correct placement of the bracket:
2525

26+
Constructor invocations and object creation expressions, including target-typed `new`, are covered by this rule as well. The closing `)` for these expressions should appear on the same line as the final argument when one exists.
27+
2628
```csharp
2729
public string JoinName(string first, string last)
2830
{

0 commit comments

Comments
 (0)