Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1500 to also consider init accessors #3670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace StyleCop.Analyzers.LayoutRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;

/// <summary>
Expand Down Expand Up @@ -165,6 +166,7 @@ private static bool IsAccessorWithSingleLineBlock(SyntaxToken previousToken, Syn
{
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
case SyntaxKindEx.InitKeyword:
case SyntaxKind.AddKeyword:
case SyntaxKind.RemoveKeyword:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,49 @@ public async Task TestMultiLineRecordWithParameterAsync(string keyword)
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3667, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3667")]
public async Task TestInitAccessorAsync()
{
var testCode = @"
class TestClass
{
int Property1
{
init
[|{|] }
}

int Property2
{
init [|{|]
}
}
}";

var fixedCode = @"
class TestClass
{
int Property1
{
init { }
}

int Property2
{
init
{
}
}
}";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ private static void CheckBraces(SyntaxNodeAnalysisContext context, StyleCopSetti
{
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKindEx.InitAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
case SyntaxKind.RemoveAccessorDeclaration:
case SyntaxKind.UnknownAccessorDeclaration:
Expand Down
2 changes: 2 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal static class SyntaxKindEx
public const SyntaxKind OrKeyword = (SyntaxKind)8438;
public const SyntaxKind AndKeyword = (SyntaxKind)8439;
public const SyntaxKind NotKeyword = (SyntaxKind)8440;
public const SyntaxKind InitKeyword = (SyntaxKind)8443;
public const SyntaxKind ManagedKeyword = (SyntaxKind)8445;
public const SyntaxKind UnmanagedKeyword = (SyntaxKind)8446;
public const SyntaxKind RequiredKeyword = (SyntaxKind)8447;
Expand Down Expand Up @@ -62,6 +63,7 @@ internal static class SyntaxKindEx
public const SyntaxKind NullableDirectiveTrivia = (SyntaxKind)9055;
public const SyntaxKind FunctionPointerType = (SyntaxKind)9056;
public const SyntaxKind FunctionPointerParameter = (SyntaxKind)9057;
public const SyntaxKind InitAccessorDeclaration = (SyntaxKind)9060;
public const SyntaxKind WithExpression = (SyntaxKind)9061;
public const SyntaxKind WithInitializerExpression = (SyntaxKind)9062;
public const SyntaxKind RecordDeclaration = (SyntaxKind)9063;
Expand Down