Skip to content

Commit

Permalink
Avoid using operator + for string concatenation when ref-like types a…
Browse files Browse the repository at this point in the history
…re involved.
  • Loading branch information
siegfriedpammer committed Jul 20, 2024
1 parent e6004e5 commit fd1de09
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,20 @@ bool CheckArgumentsForStringConcat(Expression[] arguments)
}
foreach (var arg in arguments)
{
if (arg.GetResolveResult() is InvocationResolveResult rr && IsStringConcat(rr.Member))
var rr = arg.GetResolveResult();
if (rr is InvocationResolveResult irr && IsStringConcat(irr.Member))
{
// Roslyn + mcs also flatten nested string.Concat() invocations within a operator+ use,
// which causes it to use the incorrect evaluation order despite the code using an
// explicit string.Concat() call.
// This problem is avoided if the outer call remains string.Concat() as well.
return false;
}
if (rr.Type.IsByRefLike)
{
// ref structs cannot be converted to object for use with +
return false;
}
}

// One of the first two arguments must be string, otherwise the + operator
Expand Down

0 comments on commit fd1de09

Please sign in to comment.