Skip to content

Commit b690c22

Browse files
authored
Merge pull request #3669 from bjornhellander/feature/sa1137-init-accessor
Update SA1137 to also consider init accessors
2 parents e2f3451 + 89174f9 commit b690c22

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,61 @@
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.SA1137ElementsShouldHaveTheSameIndentation,
13+
StyleCop.Analyzers.ReadabilityRules.IndentationCodeFixProvider>;
714

815
public class SA1137CSharp9UnitTests : SA1137CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3668, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3668")]
19+
public async Task TestInitAccessorAttributeListAsync()
20+
{
21+
string testCode = @"
22+
using System;
23+
24+
class TestClass
25+
{
26+
int Property
27+
{
28+
[My]
29+
[| |][My]
30+
init { }
31+
}
32+
}
33+
34+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
35+
class MyAttribute : Attribute { }
36+
";
37+
38+
string fixedCode = @"
39+
using System;
40+
41+
class TestClass
42+
{
43+
int Property
44+
{
45+
[My]
46+
[My]
47+
init { }
48+
}
49+
}
50+
51+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
52+
class MyAttribute : Attribute { }
53+
";
54+
55+
await new CSharpTest
56+
{
57+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
58+
TestCode = testCode,
59+
FixedCode = fixedCode,
60+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
61+
}
1062
}
1163
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ internal static class SyntaxKindEx
6262
public const SyntaxKind NullableDirectiveTrivia = (SyntaxKind)9055;
6363
public const SyntaxKind FunctionPointerType = (SyntaxKind)9056;
6464
public const SyntaxKind FunctionPointerParameter = (SyntaxKind)9057;
65+
public const SyntaxKind InitAccessorDeclaration = (SyntaxKind)9060;
6566
public const SyntaxKind WithExpression = (SyntaxKind)9061;
6667
public const SyntaxKind WithInitializerExpression = (SyntaxKind)9062;
6768
public const SyntaxKind RecordDeclaration = (SyntaxKind)9063;

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ private static void AddMemberAndAttributes(ImmutableList<SyntaxNode>.Builder ele
336336

337337
case SyntaxKind.GetAccessorDeclaration:
338338
case SyntaxKind.SetAccessorDeclaration:
339+
case SyntaxKindEx.InitAccessorDeclaration:
339340
case SyntaxKind.AddAccessorDeclaration:
340341
case SyntaxKind.RemoveAccessorDeclaration:
341342
case SyntaxKind.UnknownAccessorDeclaration:

0 commit comments

Comments
 (0)