Skip to content

Commit 7501f04

Browse files
authored
Merge pull request #3666 from bjornhellander/feature/sa1513-init-accessor
Update SA1513 to not trigger before an init accessor
2 parents c991261 + 929986b commit 7501f04

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,33 @@ public void Baz(string arg)
3737

3838
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
3939
}
40+
41+
[Fact]
42+
[WorkItem(3658, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3658")]
43+
public async Task TestInitAccessorAsync()
44+
{
45+
var testCode = @"using System;
46+
47+
public class Foo
48+
{
49+
public int X
50+
{
51+
get
52+
{
53+
return 0;
54+
}
55+
init
56+
{
57+
}
58+
}
59+
}
60+
";
61+
62+
await new CSharpTest
63+
{
64+
TestCode = testCode,
65+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
66+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
67+
}
4068
}
4169
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1516CSharp9UnitTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,53 @@ record A();
104104
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
105105
}
106106

107+
[Fact]
108+
[WorkItem(3658, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3658")]
109+
public async Task TestInitAccessorAsync()
110+
{
111+
var testCode = @"using System;
112+
113+
public class Foo
114+
{
115+
public int X
116+
{
117+
get
118+
{
119+
return 0;
120+
}
121+
[| |]init
122+
{
123+
}
124+
}
125+
}
126+
";
127+
128+
var fixedCode = @"using System;
129+
130+
public class Foo
131+
{
132+
public int X
133+
{
134+
get
135+
{
136+
return 0;
137+
}
138+
139+
init
140+
{
141+
}
142+
}
143+
}
144+
";
145+
146+
await new CSharpTest
147+
{
148+
TestCode = testCode,
149+
FixedCode = fixedCode,
150+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
151+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
152+
}
153+
107154
protected virtual DiagnosticResult[] GetExpectedResultTestUsingAndGlobalStatementSpacingInTopLevelProgram()
108155
{
109156
// NOTE: Seems like a Roslyn bug made diagnostics be reported twice. Fixed in a later version.

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace StyleCop.Analyzers.LayoutRules
1515
using Microsoft.CodeAnalysis.Diagnostics;
1616
using Microsoft.CodeAnalysis.Text;
1717
using StyleCop.Analyzers.Helpers;
18+
using StyleCop.Analyzers.Lightup;
1819

1920
/// <summary>
2021
/// A closing brace within a C# element, statement, or expression is not followed by a blank line.
@@ -274,7 +275,8 @@ private void AnalyzeCloseBrace(SyntaxToken token)
274275
if (nextToken.IsKind(SyntaxKind.AddKeyword)
275276
|| nextToken.IsKind(SyntaxKind.RemoveKeyword)
276277
|| nextToken.IsKind(SyntaxKind.GetKeyword)
277-
|| nextToken.IsKind(SyntaxKind.SetKeyword))
278+
|| nextToken.IsKind(SyntaxKind.SetKeyword)
279+
|| nextToken.IsKind(SyntaxKindEx.InitKeyword))
278280
{
279281
// the close brace is followed by an accessor (SA1516 will handle that)
280282
return;

0 commit comments

Comments
 (0)