Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,88 @@ await VerifyCSharpFixAsync(
fixedCode,
CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
public async Task TestParenthesizedLambdaInCollectionExpressionAsync()
{
var testCode = @"
class TestClass
{
private System.Action[] actions = [ [|(|]) => {}];
}
";

var fixedCode = @"
class TestClass
{
private System.Action[] actions = [() => {}];
}
";

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
public async Task TestLambdaAfterCommaInCollectionExpressionAsync()
{
var testCode = @"
class TestClass
{
private System.Func<int, int, int>[] functions = [(x, y) => x + y,{|#0:(|}x, y) => x - y];
}
";

var fixedCode = @"
class TestClass
{
private System.Func<int, int, int>[] functions = [(x, y) => x + y, (x, y) => x - y];
}
";

var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
public async Task TestLambdaAfterSpreadInCollectionExpressionAsync()
{
var testCode = @"
class TestClass
{
private static System.Func<int, int, int>[] existing = [(x, y) => x + y];
private System.Func<int, int, int>[] functions = [..existing,{|#0:(|}x, y) => x - y];
}
";

var fixedCode = @"
class TestClass
{
private static System.Func<int, int, int>[] existing = [(x, y) => x + y];
private System.Func<int, int, int>[] functions = [..existing, (x, y) => x - y];
}
";

var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
public async Task TestLambdaWithBracketOnPreviousLineAsync()
{
var testCode = @"
class TestClass
{
private System.Func<int, int, int>[] functions = [
(x, y) => x + y
];
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,34 @@ public void TestMethod1()
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
public async Task TestLambdaInCollectionInitializerAsync()
{
var testCode = @"
using System;
using System.Collections.Generic;

class TestClass
{
private List<Action<int, int>> actions = new List<Action<int, int>>() {{|#0:(|}x, y) => { } };
}
";

var fixedCode = @"
using System;
using System.Collections.Generic;

class TestClass
{
private List<Action<int, int>> actions = new List<Action<int, int>>() { (x, y) => { } };
}
";

var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that multi-line comments are handled properly.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt

case SyntaxKind.ParameterList:
var partOfLambdaExpression = token.Parent.Parent.IsKind(SyntaxKind.ParenthesizedLambdaExpression);
haveLeadingSpace = partOfLambdaExpression;
var startOfCollectionExpression = prevToken.IsKind(SyntaxKind.OpenBracketToken) && prevToken.Parent.IsKind(SyntaxKindEx.CollectionExpression);
haveLeadingSpace = partOfLambdaExpression && !startOfCollectionExpression;
break;

case SyntaxKindEx.TupleType:
Expand Down