Skip to content

Commit

Permalink
Merge pull request #3317 from sharwell/with-init
Browse files Browse the repository at this point in the history
Update SA1118 to allow 'with' expressions
  • Loading branch information
sharwell authored Mar 10, 2021
2 parents 28795c0 + 4239b71 commit 24755d2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<StyleCop.Analyzers.ReadabilityRules.SA1118ParameterMustNotSpanMultipleLines>;

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// A parameter to a C# method or indexer, other than the first parameter, spans across multiple lines.
Expand Down Expand Up @@ -82,6 +83,7 @@ internal class SA1118ParameterMustNotSpanMultipleLines : DiagnosticAnalyzer
SyntaxKind.AnonymousObjectCreationExpression,
SyntaxKind.ArrayCreationExpression,
SyntaxKind.ImplicitArrayCreationExpression,
SyntaxKindEx.WithExpression,
};

/// <inheritdoc/>
Expand Down
3 changes: 2 additions & 1 deletion documentation/SA1118.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 24755d2

Please sign in to comment.