From 70abd32aaf817f871957d453cc57779fa7a6b28b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 17 Jun 2023 09:41:42 +0200 Subject: [PATCH 1/2] Update SA1137 to also consider init accessors #3668 --- .../SA1137CSharp9UnitTests.cs | 52 +++++++++++++++++++ .../Lightup/SyntaxKindEx.cs | 1 + ...137ElementsShouldHaveTheSameIndentation.cs | 1 + 3 files changed, 54 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs index 5c725eea5..76620c42e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs @@ -3,9 +3,61 @@ namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules { + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules; + using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.ReadabilityRules.SA1137ElementsShouldHaveTheSameIndentation, + StyleCop.Analyzers.ReadabilityRules.IndentationCodeFixProvider>; public class SA1137CSharp9UnitTests : SA1137CSharp8UnitTests { + [Fact] + [WorkItem(3668, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3668")] + public async Task TestInitAccessorAttributeListAsync() + { + string testCode = @" +using System; + +class TestClass +{ + int Property + { + [My] +[| |][My] + init { } + } +} + +[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] +class MyAttribute : Attribute { } +"; + + string fixedCode = @" +using System; + +class TestClass +{ + int Property + { + [My] + [My] + init { } + } +} + +[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] +class MyAttribute : Attribute { } +"; + + var test = new CSharpTest + { + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, + TestCode = testCode, + FixedCode = fixedCode, + }; + await test.RunAsync().ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs index 677453d6f..62b06c867 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs @@ -62,6 +62,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; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs index 1f3348894..ec5700d2f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs @@ -336,6 +336,7 @@ private static void AddMemberAndAttributes(ImmutableList.Builder ele case SyntaxKind.GetAccessorDeclaration: case SyntaxKind.SetAccessorDeclaration: + case SyntaxKindEx.InitAccessorDeclaration: case SyntaxKind.AddAccessorDeclaration: case SyntaxKind.RemoveAccessorDeclaration: case SyntaxKind.UnknownAccessorDeclaration: From 89174f9ce034ed34af8f0f2bd1d5ac4267d57446 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 21 Jun 2023 08:30:52 -0500 Subject: [PATCH 2/2] Apply suggestions from code review --- .../ReadabilityRules/SA1137CSharp9UnitTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs index 76620c42e..1093df5b3 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1137CSharp9UnitTests.cs @@ -3,6 +3,7 @@ namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules { + using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules; @@ -51,13 +52,12 @@ int Property class MyAttribute : Attribute { } "; - 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); } } }