From 86a93fb7308e1415eca13e3fdb02d8ffa2b9c006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 17 Jun 2023 11:20:15 +0200 Subject: [PATCH 1/2] Update SA1500 to also consider init accessors #3667 --- .../LayoutRules/SA1500CodeFixProvider.cs | 2 + .../LayoutRules/SA1500CSharp9UnitTests.cs | 45 +++++++++++++++++++ ...sForMultiLineStatementsMustNotShareLine.cs | 1 + .../Lightup/SyntaxKindEx.cs | 2 + 4 files changed, 50 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs index 2ab1bc062..b16c739be 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs @@ -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; /// @@ -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; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs index 28c07a6d9..547e22e33 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs @@ -93,5 +93,50 @@ 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 + { + } + } +}"; + + var test = new CSharpTest + { + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, + TestCode = testCode, + FixedCode = fixedCode, + }; + await test.RunAsync().ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs index 67b3b671a..dee2b8846 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs @@ -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: diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs index 677453d6f..ecb9f398b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs @@ -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; @@ -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; From ba96832d579fd4aced1fbf45485e7a4deabb2fde Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 21 Jun 2023 08:28:08 -0500 Subject: [PATCH 2/2] Update StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs --- .../LayoutRules/SA1500CSharp9UnitTests.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs index 547e22e33..019ec036e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs @@ -130,13 +130,12 @@ int Property2 } }"; - var test = new CSharpTest + await new CSharpTest { ReferenceAssemblies = ReferenceAssemblies.Net.Net50, TestCode = testCode, FixedCode = fixedCode, - }; - await test.RunAsync().ConfigureAwait(false); + }.RunAsync(CancellationToken.None).ConfigureAwait(false); } } }