diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1118CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1118CSharp9UnitTests.cs index 3c6ccb559..bcbec918d 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1118CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1118CSharp9UnitTests.cs @@ -3,9 +3,77 @@ namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules { + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules; + using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier; public class SA1118CSharp9UnitTests : SA1118CSharp8UnitTests { + [Fact] + [WorkItem(3314, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3314")] + public async Task TestWithExpressionAsync() + { + var testCode = @" +class Foo +{ + public record R(int X, int Y); + + public void FunA(params object[] j) + { + } + + public void FunB(R r) + { + FunA( + 1, + r with + { + X = 1, + }); + } +}"; + + await new CSharpTest(LanguageVersion.CSharp9) + { + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, + TestCode = testCode, + }.RunAsync(CancellationToken.None).ConfigureAwait(false); + } + + [Fact] + [WorkItem(3314, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3314")] + public async Task TestWithExpression2Async() + { + var testCode = @" +class Foo +{ + public record R(int X, int Y); + + public void FunA(params object[] j) + { + } + + public void FunB(R r) + { + FunA( + 1, + r with + { + X = 1, + }, + 2); + } +}"; + + await new CSharpTest(LanguageVersion.CSharp9) + { + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, + TestCode = testCode, + }.RunAsync(CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1118ParameterMustNotSpanMultipleLines.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1118ParameterMustNotSpanMultipleLines.cs index 0e59f8470..5deaafa00 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1118ParameterMustNotSpanMultipleLines.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1118ParameterMustNotSpanMultipleLines.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.ReadabilityRules using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; /// /// A parameter to a C# method or indexer, other than the first parameter, spans across multiple lines. @@ -82,6 +83,7 @@ internal class SA1118ParameterMustNotSpanMultipleLines : DiagnosticAnalyzer SyntaxKind.AnonymousObjectCreationExpression, SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression, + SyntaxKindEx.WithExpression, }; /// diff --git a/documentation/SA1118.md b/documentation/SA1118.md index 0f7f5971d..a4f7c53a2 100644 --- a/documentation/SA1118.md +++ b/documentation/SA1118.md @@ -30,7 +30,8 @@ cases: * The first parameter may span multiple lines * Anonymous methods (including lambda expressions) may span multiple lines * Invocation expressions may span multiple lines -* Object creation expressions may span multiple lines +* Object and array creation expressions may span multiple lines +* `with` expressions (C# 9) may span multiple lines For example, the following code would violate this rule, since the second parameter spans across multiple lines: