Skip to content

Commit 0e192df

Browse files
authored
Merge pull request #3661 from bjornhellander/feature/sa1212-init-accessor
Update SA1212 to also trigger for an init accessor before a getter
2 parents 7501f04 + 280b11a commit 0e192df

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/OrderingRules/SA1212CSharp9UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.OrderingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.OrderingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.OrderingRules.SA1212PropertyAccessorsMustFollowOrder,
13+
StyleCop.Analyzers.OrderingRules.SA1212SA1213CodeFixProvider>;
714

815
public class SA1212CSharp9UnitTests : SA1212CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3652, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3652")]
19+
public async Task TestAutoPropertyDeclarationInitBeforeGetterAsync()
20+
{
21+
var testCode = @"
22+
public class Foo
23+
{
24+
public int Prop { [|init;|] get; }
25+
}";
26+
27+
var fixedCode = @"
28+
public class Foo
29+
{
30+
public int Prop { get; init; }
31+
}";
32+
33+
await new CSharpTest
34+
{
35+
TestCode = testCode,
36+
FixedCode = fixedCode,
37+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
38+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
39+
}
40+
41+
[Fact]
42+
[WorkItem(3652, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3652")]
43+
public async Task TestPropertyWithBackingFieldDeclarationInitBeforeGetterAsync()
44+
{
45+
var testCode = @"
46+
public class Foo
47+
{
48+
private int i = 0;
49+
50+
public int Prop
51+
{
52+
[|init
53+
{
54+
i = value;
55+
}|]
56+
57+
get
58+
{
59+
return i;
60+
}
61+
}
62+
}";
63+
64+
var fixedCode = @"
65+
public class Foo
66+
{
67+
private int i = 0;
68+
69+
public int Prop
70+
{
71+
get
72+
{
73+
return i;
74+
}
75+
76+
init
77+
{
78+
i = value;
79+
}
80+
}
81+
}";
82+
83+
await new CSharpTest
84+
{
85+
TestCode = testCode,
86+
FixedCode = fixedCode,
87+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
88+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
89+
}
1090
}
1191
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1212UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public int Prop
339339
}
340340

341341
[Fact]
342-
public async Task TestAutoPropertydDeclarationSetterBeforeGetterAsync()
342+
public async Task TestAutoPropertyDeclarationSetterBeforeGetterAsync()
343343
{
344344
var testCode = @"
345345
public class Foo

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1212PropertyAccessorsMustFollowOrder.cs

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

1516
/// <summary>
1617
/// A get accessor appears after a set accessor within a property or indexer.
@@ -99,7 +100,7 @@ private static void AnalyzeProperty(SyntaxNodeAnalysisContext context, BasePrope
99100
return;
100101
}
101102

102-
if (accessors[0].Kind() == SyntaxKind.SetAccessorDeclaration &&
103+
if ((accessors[0].Kind() is SyntaxKind.SetAccessorDeclaration or SyntaxKindEx.InitAccessorDeclaration) &&
103104
accessors[1].Kind() == SyntaxKind.GetAccessorDeclaration)
104105
{
105106
context.ReportDiagnostic(Diagnostic.Create(Descriptor, accessors[0].GetLocation()));

documentation/SA1212.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
## Cause
1919

20-
A get accessor appears after a set accessor within a property or indexer.
20+
A get accessor appears after a set or init accessor within a property or indexer.
2121

2222
## Rule description
2323

24-
A violation of this rule occurs when a get accessor is placed after a set accessor within a property or indexer. To comply with this rule, the get accessor should appear before the set accessor.
24+
A violation of this rule occurs when a get accessor is placed after a set or init accessor within a property or indexer. To comply with this rule, the get accessor should appear first.
2525

2626
For example, the following code would raise an instance of this violation:
2727

0 commit comments

Comments
 (0)