From 610aa69c5145c9b3e28575af0f8a8b53a1dc5331 Mon Sep 17 00:00:00 2001 From: Anton Paulovich Date: Tue, 17 Nov 2020 23:45:34 +0300 Subject: [PATCH] Update SA1413 to support with initializer (fix #3240) --- .../SA1413CSharp9UnitTests.cs | 57 +++++++++++++++++++ ...seTrailingCommasInMultiLineInitializers.cs | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/MaintainabilityRules/SA1413CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/MaintainabilityRules/SA1413CSharp9UnitTests.cs index 680405fb1..9ad562df1 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/MaintainabilityRules/SA1413CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/MaintainabilityRules/SA1413CSharp9UnitTests.cs @@ -3,9 +3,66 @@ namespace StyleCop.Analyzers.Test.CSharp9.MaintainabilityRules { + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp8.MaintainabilityRules; + using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.MaintainabilityRules.SA1413UseTrailingCommasInMultiLineInitializers, + StyleCop.Analyzers.MaintainabilityRules.SA1413CodeFixProvider>; public class SA1413CSharp9UnitTests : SA1413CSharp8UnitTests { + [Fact] + [WorkItem(3240, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3240")] + public async Task VerifyWithInitializerAsync() + { + var testCode = @"namespace TestNamespace +{ + public record R + { + public int A { get; set; } + + public int B { get; set; } + + void M() + { + _ = this with { A = 1, B = 2 }; + _ = this with { A = 1, B = 2, }; + _ = this with + { + A = 1, + [|B = 2|] + }; + } + } +} +"; + var fixedCode = @"namespace TestNamespace +{ + public record R + { + public int A { get; set; } + + public int B { get; set; } + + void M() + { + _ = this with { A = 1, B = 2 }; + _ = this with { A = 1, B = 2, }; + _ = this with + { + A = 1, + B = 2, + }; + } + } +} +"; + + await VerifyCSharpFixAsync(LanguageVersion.CSharp9, testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs index 0cc0e23cf..706c21503 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs @@ -63,7 +63,7 @@ internal class SA1413UseTrailingCommasInMultiLineInitializers : DiagnosticAnalyz private static readonly Action HandleSwitchExpressionAction = HandleSwitchExpression; private static readonly ImmutableArray ObjectInitializerKinds = - ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression, SyntaxKind.ArrayInitializerExpression, SyntaxKind.CollectionInitializerExpression); + ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression, SyntaxKind.ArrayInitializerExpression, SyntaxKind.CollectionInitializerExpression, SyntaxKindEx.WithInitializerExpression); /// public override ImmutableArray SupportedDiagnostics { get; } =