From fd1de09489ae92c596e0b5d90f574871b6a1f043 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 20 Jul 2024 15:20:53 +0200 Subject: [PATCH] Avoid using operator + for string concatenation when ref-like types are involved. --- .../CSharp/Transforms/ReplaceMethodCallsWithOperators.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs b/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs index cf9970232f..36624535b5 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs @@ -274,7 +274,8 @@ 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 @@ -282,6 +283,11 @@ bool CheckArgumentsForStringConcat(Expression[] arguments) // 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