Skip to content

Commit 6327501

Browse files
authored
Merge pull request #3791 from bjornhellander/feature/sa1111-primary-ctor-3785
Update SA1111 to also check the parameter list in primary constructors
2 parents f66d17d + 5f00381 commit 6327501

File tree

5 files changed

+212
-175
lines changed

5 files changed

+212
-175
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/ReadabilityRules/SA1111CSharp11UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp11.ReadabilityRules
55
{
6+
using Microsoft.CodeAnalysis.Testing;
67
using StyleCop.Analyzers.Test.CSharp10.ReadabilityRules;
8+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
9+
StyleCop.Analyzers.ReadabilityRules.SA1111ClosingParenthesisMustBeOnLineOfLastParameter,
10+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
711

812
public partial class SA1111CSharp11UnitTests : SA1111CSharp10UnitTests
913
{
14+
protected override DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter()
15+
{
16+
return new[]
17+
{
18+
Diagnostic().WithLocation(0),
19+
};
20+
}
1021
}
1122
}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,116 @@
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.SA1111ClosingParenthesisMustBeOnLineOfLastParameter,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public partial class SA1111CSharp9UnitTests : SA1111CSharp8UnitTests
917
{
18+
[Theory]
19+
[MemberData(nameof(CommonMemberData.TypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))]
20+
[WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")]
21+
public async Task TestPrimaryConstructorWithParameterAsync(string typeKeyword)
22+
{
23+
var testCode = $@"
24+
{typeKeyword} Foo(int x
25+
{{|#0:)|}}
26+
{{
27+
}}";
28+
29+
var fixedCode = $@"
30+
{typeKeyword} Foo(int x)
31+
{{
32+
}}";
33+
34+
var expected = this.GetExpectedResultTestPrimaryConstructorWithParameter();
35+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
36+
}
37+
38+
[Theory]
39+
[MemberData(nameof(CommonMemberData.TypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))]
40+
[WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")]
41+
public async Task TestPrimaryConstructorWithoutParameterAsync(string typeKeyword)
42+
{
43+
var testCode = $@"
44+
{typeKeyword} Foo(
45+
)
46+
{{
47+
}}";
48+
49+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
50+
}
51+
52+
[Theory]
53+
[MemberData(nameof(CommonMemberData.ReferenceTypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))]
54+
[WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")]
55+
public async Task TestPrimaryConstructorBaseListWithArgumentsAsync(string typeKeyword)
56+
{
57+
var testCode = $@"
58+
{typeKeyword} Foo(int x)
59+
{{
60+
}}
61+
62+
{typeKeyword} Bar(int x) : Foo(x
63+
{{|#0:)|}}
64+
{{
65+
}}";
66+
67+
var fixedCode = $@"
68+
{typeKeyword} Foo(int x)
69+
{{
70+
}}
71+
72+
{typeKeyword} Bar(int x) : Foo(x)
73+
{{
74+
}}";
75+
76+
var expected = this.GetExpectedResultTestPrimaryConstructorBaseList();
77+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
78+
}
79+
80+
[Theory]
81+
[MemberData(nameof(CommonMemberData.ReferenceTypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))]
82+
[WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")]
83+
public async Task TestPrimaryConstructorBaseListWithoutArgumentsAsync(string typeKeyword)
84+
{
85+
var testCode = $@"
86+
{typeKeyword} Foo()
87+
{{
88+
}}
89+
90+
{typeKeyword} Bar(int x) : Foo(
91+
)
92+
{{
93+
}}";
94+
95+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
96+
}
97+
98+
protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter()
99+
{
100+
return new[]
101+
{
102+
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136
103+
Diagnostic().WithLocation(0),
104+
Diagnostic().WithLocation(0),
105+
};
106+
}
107+
108+
protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorBaseList()
109+
{
110+
return new[]
111+
{
112+
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/70488
113+
Diagnostic().WithLocation(0),
114+
Diagnostic().WithLocation(0),
115+
};
116+
}
10117
}
11118
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1612UnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,13 @@ public class ClassName
536536
}
537537

538538
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
539-
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, new[] { expected }, false, cancellationToken);
539+
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, new[] { expected }, ignoreCompilerDiagnostics: false, cancellationToken);
540540

541541
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
542-
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, false, cancellationToken);
542+
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, ignoreCompilerDiagnostics: false, cancellationToken);
543543

544544
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
545-
=> VerifyCSharpDiagnosticAsync(source, testSettings, expected, false, cancellationToken);
545+
=> VerifyCSharpDiagnosticAsync(source, testSettings, expected, ignoreCompilerDiagnostics: false, cancellationToken);
546546

547547
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
548548
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/CommonMemberData.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.Helpers
75
{
86
using System.Collections.Generic;
@@ -115,7 +113,7 @@ public static IEnumerable<object[]> GenericTypeDeclarationKeywords
115113
}
116114
}
117115

118-
public static IEnumerable<object[]> TypeKeywordsWhichSupportPrimaryConstructors
116+
public static IEnumerable<object[]> ReferenceTypeKeywordsWhichSupportPrimaryConstructors
119117
{
120118
get
121119
{
@@ -127,22 +125,33 @@ public static IEnumerable<object[]> TypeKeywordsWhichSupportPrimaryConstructors
127125
if (LightupHelpers.SupportsCSharp10)
128126
{
129127
yield return new[] { "record class" };
130-
yield return new[] { "record struct" };
131128
}
132129

133130
if (LightupHelpers.SupportsCSharp12)
134131
{
135132
yield return new[] { "class" };
136-
yield return new[] { "struct" };
137133
}
138134
}
139135
}
140136

141-
public static IEnumerable<object[]> ReferenceTypeKeywordsWhichSupportPrimaryConstructors
137+
public static IEnumerable<object[]> TypeKeywordsWhichSupportPrimaryConstructors
142138
{
143139
get
144140
{
145-
return TypeKeywordsWhichSupportPrimaryConstructors.Where(x => !((string)x[0]).Contains("struct"));
141+
foreach (var keyword in ReferenceTypeKeywordsWhichSupportPrimaryConstructors)
142+
{
143+
yield return keyword;
144+
}
145+
146+
if (LightupHelpers.SupportsCSharp10)
147+
{
148+
yield return new[] { "record struct" };
149+
}
150+
151+
if (LightupHelpers.SupportsCSharp12)
152+
{
153+
yield return new[] { "struct" };
154+
}
146155
}
147156
}
148157
}

0 commit comments

Comments
 (0)