Skip to content
Open
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
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<diagnostics id="WTG3013" message="Don't use Concat when appending a single element to an enumerable." severity="Info">
<diagnostic>
<location>Test0.cs: (8, 32-57)</location>
</diagnostic>
</diagnostics>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Linq;

public class Bob
{
public int[] Method(Bob other)
{
var items = other.GetItems()?.Where(x => x > 0).Append(42).ToArray();
return items;
}

public IEnumerable<int> GetItems() => new[] { 1, 2, 3 };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Linq;

public class Bob
{
public int[] Method(Bob other)
{
var items = other.GetItems()?.Where(x => x > 0).Concat(new[] { 42 }).ToArray();
return items;
}

public IEnumerable<int> GetItems() => new[] { 1, 2, 3 };
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ public static SyntaxNode FixConcatWithAppendMethod(MemberAccessExpressionSyntax
return InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParenthesizedExpression(m.Expression.WithoutTrivia())
.WithTriviaFrom(m.Expression)
.WithAdditionalAnnotations(Simplifier.Annotation),
WrapExpressionIfNeeded(m.Expression),
m.OperatorToken,
IdentifierName(nameof(Enumerable.Append))
.WithTriviaFrom(m.Name)))
Expand All @@ -146,9 +144,7 @@ public static SyntaxNode FixConcatWithAppendMethod(MemberAccessExpressionSyntax
{
case 1:
listOfArgumentsAndSeparators.Add(Argument(LinqEnumerableUtils.GetFirstValue(m.Expression.TryGetExpressionFromParenthesizedExpression())!));
member = ParenthesizedExpression(invocation.ArgumentList.Arguments[0].Expression.WithoutTrivia())
.WithTriviaFrom(m.Expression)
.WithAdditionalAnnotations(Simplifier.Annotation);
member = WrapExpressionIfNeeded(invocation.ArgumentList.Arguments[0].Expression.WithTriviaFrom(m.Expression));
break;
case 2:
listOfArgumentsAndSeparators.Add(invocation.ArgumentList.Arguments[1]);
Expand Down Expand Up @@ -213,5 +209,22 @@ public static SyntaxNode FixConcatWithNewCollection(MemberAccessExpressionSyntax
.WithTriviaFrom(invocation)
.WithAdditionalAnnotations(Simplifier.Annotation);
}

static ExpressionSyntax WrapExpressionIfNeeded(ExpressionSyntax expression)
{
// Invocation expressions never need parenthesization as they already have
// clear binding. Wrapping them can cause issues when the expression is part
// of a conditional access chain (?.), as it disconnects MemberBindingExpressions
// from their ConditionalAccessExpression, causing Roslyn's speculative semantic
// model to crash with a NullReferenceException.
if (expression.IsKind(SyntaxKind.InvocationExpression))
{
return expression;
}

return ParenthesizedExpression(expression.WithoutTrivia())
.WithTriviaFrom(expression)
.WithAdditionalAnnotations(Simplifier.Annotation);
}
}
}
Loading