diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs index e05e5bd91..0704d886b 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs @@ -1026,6 +1026,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty }); } + public static void StringConcat() + { + Test>(null, (string a, object b) => a + b); + Test>(null, (string a, object b) => a + b.ToString()); + Test>(null, (string a, int b) => a + b); + Test>(null, (string a, int b) => a + b.ToString()); + } + public async Task Issue1524(string str) { await Task.Delay(100); diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs b/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs index eb95d66f9..7444cd957 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs @@ -59,10 +59,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms // Reduce "String.Concat(a, b)" to "a + b" if (IsStringConcat(method) && CheckArgumentsForStringConcat(arguments)) { - invocationExpression.Arguments.Clear(); // detach arguments from invocationExpression - Expression expr = RemoveRedundantToStringInConcat(arguments[0], method, isLastArgument: false).Detach(); + bool isInExpressionTree = invocationExpression.Ancestors.OfType().Any( + lambda => lambda.Annotation()?.Kind == IL.ILFunctionKind.ExpressionTree); + Expression expr = arguments[0].Detach(); + if (!isInExpressionTree) { + expr = RemoveRedundantToStringInConcat(expr, method, isLastArgument: false).Detach(); + } for (int i = 1; i < arguments.Length; i++) { - var arg = RemoveRedundantToStringInConcat(arguments[i], method, isLastArgument: i == arguments.Length - 1).Detach(); + var arg = arguments[i].Detach(); + if (!isInExpressionTree) { + arg = RemoveRedundantToStringInConcat(arg, method, isLastArgument: i == arguments.Length - 1).Detach(); + } expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Add, arg); } expr.CopyAnnotationsFrom(invocationExpression);