Skip to content

Commit c991261

Browse files
authored
Merge pull request #3670 from bjornhellander/feature/sa1500-init-accessor
Update SA1500 to also consider init accessors
2 parents b690c22 + ba96832 commit c991261

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace StyleCop.Analyzers.LayoutRules
1818
using Microsoft.CodeAnalysis.CSharp;
1919
using Microsoft.CodeAnalysis.Text;
2020
using StyleCop.Analyzers.Helpers;
21+
using StyleCop.Analyzers.Lightup;
2122
using StyleCop.Analyzers.Settings.ObjectModel;
2223

2324
/// <summary>
@@ -165,6 +166,7 @@ private static bool IsAccessorWithSingleLineBlock(SyntaxToken previousToken, Syn
165166
{
166167
case SyntaxKind.GetKeyword:
167168
case SyntaxKind.SetKeyword:
169+
case SyntaxKindEx.InitKeyword:
168170
case SyntaxKind.AddKeyword:
169171
case SyntaxKind.RemoveKeyword:
170172
break;

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,49 @@ public async Task TestMultiLineRecordWithParameterAsync(string keyword)
9393
FixedCode = testCode,
9494
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
9595
}
96+
97+
[Fact]
98+
[WorkItem(3667, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3667")]
99+
public async Task TestInitAccessorAsync()
100+
{
101+
var testCode = @"
102+
class TestClass
103+
{
104+
int Property1
105+
{
106+
init
107+
[|{|] }
108+
}
109+
110+
int Property2
111+
{
112+
init [|{|]
113+
}
114+
}
115+
}";
116+
117+
var fixedCode = @"
118+
class TestClass
119+
{
120+
int Property1
121+
{
122+
init { }
123+
}
124+
125+
int Property2
126+
{
127+
init
128+
{
129+
}
130+
}
131+
}";
132+
133+
await new CSharpTest
134+
{
135+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
136+
TestCode = testCode,
137+
FixedCode = fixedCode,
138+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
139+
}
96140
}
97141
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ private static void CheckBraces(SyntaxNodeAnalysisContext context, StyleCopSetti
219219
{
220220
case SyntaxKind.GetAccessorDeclaration:
221221
case SyntaxKind.SetAccessorDeclaration:
222+
case SyntaxKindEx.InitAccessorDeclaration:
222223
case SyntaxKind.AddAccessorDeclaration:
223224
case SyntaxKind.RemoveAccessorDeclaration:
224225
case SyntaxKind.UnknownAccessorDeclaration:

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal static class SyntaxKindEx
1212
public const SyntaxKind OrKeyword = (SyntaxKind)8438;
1313
public const SyntaxKind AndKeyword = (SyntaxKind)8439;
1414
public const SyntaxKind NotKeyword = (SyntaxKind)8440;
15+
public const SyntaxKind InitKeyword = (SyntaxKind)8443;
1516
public const SyntaxKind ManagedKeyword = (SyntaxKind)8445;
1617
public const SyntaxKind UnmanagedKeyword = (SyntaxKind)8446;
1718
public const SyntaxKind RequiredKeyword = (SyntaxKind)8447;

0 commit comments

Comments
 (0)