Skip to content

Commit fc62ca0

Browse files
Issue #3369: Updated SA1116 to also handle target-typed new
1 parent aacf8a5 commit fc62ca0

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,51 @@
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 Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.ReadabilityRules.SA1116SplitParametersMustStartOnLineAfterDeclaration,
13+
StyleCop.Analyzers.ReadabilityRules.SA1116CodeFixProvider>;
714

815
public class SA1116CSharp9UnitTests : SA1116CSharp8UnitTests
916
{
17+
[Fact]
18+
public async Task TestTargetTypedNewExpressionnAsync()
19+
{
20+
var testCode = @"
21+
class Foo
22+
{
23+
public Foo(int a, int b)
24+
{
25+
}
26+
27+
public void Method()
28+
{
29+
Foo x = new(1,
30+
2);
31+
}
32+
}";
33+
34+
var fixedCode = @"
35+
class Foo
36+
{
37+
public Foo(int a, int b)
38+
{
39+
}
40+
41+
public void Method()
42+
{
43+
Foo x = new(
44+
1,
45+
2);
46+
}
47+
}";
48+
49+
DiagnosticResult expected = Diagnostic().WithLocation(10, 21);
50+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
51+
}
1052
}
1153
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1116SplitParametersMustStartOnLineAfterDeclaration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ internal class SA1116SplitParametersMustStartOnLineAfterDeclaration : Diagnostic
6161
private static readonly Action<SyntaxNodeAnalysisContext> IndexerDeclarationAction = HandleIndexerDeclaration;
6262
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;
6363
private static readonly Action<SyntaxNodeAnalysisContext> ObjectCreationExpressionAction = HandleObjectCreationExpression;
64+
private static readonly Action<SyntaxNodeAnalysisContext> ImplicitObjectCreationExpressionAction = HandleImplicitObjectCreationExpression;
6465
private static readonly Action<SyntaxNodeAnalysisContext> ElementAccessExpressionAction = HandleElementAccessExpression;
6566
private static readonly Action<SyntaxNodeAnalysisContext> ElementBindingExpressionAction = HandleElementBindingExpression;
6667
private static readonly Action<SyntaxNodeAnalysisContext> ImplicitElementAccessAction = HandleImplicitElementAccess;
@@ -86,6 +87,7 @@ public override void Initialize(AnalysisContext context)
8687
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
8788
context.RegisterSyntaxNodeAction(InvocationExpressionAction, SyntaxKind.InvocationExpression);
8889
context.RegisterSyntaxNodeAction(ObjectCreationExpressionAction, SyntaxKind.ObjectCreationExpression);
90+
context.RegisterSyntaxNodeAction(ImplicitObjectCreationExpressionAction, SyntaxKindEx.ImplicitObjectCreationExpression);
8991
context.RegisterSyntaxNodeAction(ElementAccessExpressionAction, SyntaxKind.ElementAccessExpression);
9092
context.RegisterSyntaxNodeAction(ElementBindingExpressionAction, SyntaxKind.ElementBindingExpression);
9193
context.RegisterSyntaxNodeAction(ImplicitElementAccessAction, SyntaxKind.ImplicitElementAccess);
@@ -119,6 +121,12 @@ private static void HandleObjectCreationExpression(SyntaxNodeAnalysisContext con
119121
HandleArgumentListSyntax(context, objectCreation.ArgumentList);
120122
}
121123

124+
private static void HandleImplicitObjectCreationExpression(SyntaxNodeAnalysisContext context)
125+
{
126+
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntaxWrapper)context.Node;
127+
HandleArgumentListSyntax(context, implicitObjectCreation.ArgumentList);
128+
}
129+
122130
private static void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context)
123131
{
124132
var indexerDeclaration = (IndexerDeclarationSyntax)context.Node;

0 commit comments

Comments
 (0)