From 17b8bd6aaaff1b590da90bc7510e132ff18b0929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Thu, 8 Jun 2023 20:34:08 +0200 Subject: [PATCH] Update SA1513 to not trigger before an init accessor. Also add SA1516 test to verify that it already handles init accessors correctly. #3658 --- .../LayoutRules/SA1513CSharp9UnitTests.cs | 30 ++++++++++++ .../LayoutRules/SA1516CSharp9UnitTests.cs | 49 +++++++++++++++++++ ...13ClosingBraceMustBeFollowedByBlankLine.cs | 4 +- .../Lightup/SyntaxKindEx.cs | 1 + 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs index a44ac6bca..1221245f4 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules using System.Threading.Tasks; using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp8.LayoutRules; + using StyleCop.Analyzers.Test.Verifiers; using Xunit; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1513ClosingBraceMustBeFollowedByBlankLine, @@ -37,5 +38,34 @@ public void Baz(string arg) await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + [WorkItem(3658, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3658")] + public async Task TestInitAccessorAsync() + { + var testCode = @"using System; + +public class Foo +{ + public int X + { + get + { + return 0; + } + init + { + } + } +} +"; + + var test = new CSharpTest + { + TestCode = testCode, + ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50, + }; + await test.RunAsync(CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1516CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1516CSharp9UnitTests.cs index e88bd35f4..3b9f55a24 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1516CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1516CSharp9UnitTests.cs @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp8.LayoutRules; + using StyleCop.Analyzers.Test.Verifiers; using Xunit; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1516ElementsMustBeSeparatedByBlankLine, @@ -104,6 +105,54 @@ record A(); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); } + [Fact] + [WorkItem(3658, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3658")] + public async Task TestInitAccessorAsync() + { + var testCode = @"using System; + +public class Foo +{ + public int X + { + get + { + return 0; + } +[| |]init + { + } + } +} +"; + + var fixedCode = @"using System; + +public class Foo +{ + public int X + { + get + { + return 0; + } + + init + { + } + } +} +"; + + var test = new CSharpTest + { + TestCode = testCode, + FixedCode = fixedCode, + ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50, + }; + await test.RunAsync(CancellationToken.None).ConfigureAwait(false); + } + protected virtual DiagnosticResult[] GetExpectedResultTestUsingAndGlobalStatementSpacingInTopLevelProgram() { // NOTE: Seems like a Roslyn bug made diagnostics be reported twice. Fixed in a later version. diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs index b38232471..7b4dcf641 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs @@ -15,6 +15,7 @@ namespace StyleCop.Analyzers.LayoutRules using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Text; using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; /// /// A closing brace within a C# element, statement, or expression is not followed by a blank line. @@ -274,7 +275,8 @@ private void AnalyzeCloseBrace(SyntaxToken token) if (nextToken.IsKind(SyntaxKind.AddKeyword) || nextToken.IsKind(SyntaxKind.RemoveKeyword) || nextToken.IsKind(SyntaxKind.GetKeyword) - || nextToken.IsKind(SyntaxKind.SetKeyword)) + || nextToken.IsKind(SyntaxKind.SetKeyword) + || nextToken.IsKind(SyntaxKindEx.InitKeyword)) { // the close brace is followed by an accessor (SA1516 will handle that) return; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs index 677453d6f..bd0a376fd 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;