diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Correctness/StringConcat.cs b/ICSharpCode.Decompiler.Tests/TestCases/Correctness/StringConcat.cs index ebe8de7ce..01d15eccd 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Correctness/StringConcat.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Correctness/StringConcat.cs @@ -107,11 +107,19 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness Console.WriteLine(s); } + static void TestCharPlusChar(string a) + { + Console.WriteLine("TestCharPlusChar:"); + Console.WriteLine(a[0] + a[1]); + Console.WriteLine(a[0].ToString() + a[1].ToString()); + } + static void Main() { TestClass(); TestStruct(); TestStructMutation(); + TestCharPlusChar("ab"); } } } diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs b/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs index 194c4f603..f031778fd 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs @@ -61,11 +61,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms if (IsStringConcat(method) && CheckArgumentsForStringConcat(arguments)) { bool isInExpressionTree = invocationExpression.Ancestors.OfType().Any( lambda => lambda.Annotation()?.Kind == IL.ILFunctionKind.ExpressionTree); - Expression expr = arguments[0].Detach(); + Expression arg0 = arguments[0].Detach(); + Expression arg1 = arguments[1].Detach(); if (!isInExpressionTree) { - expr = RemoveRedundantToStringInConcat(expr, method, isLastArgument: false).Detach(); + arg1 = RemoveRedundantToStringInConcat(arg1, method, isLastArgument: arguments.Length == 2).Detach(); + if (arg1.GetResolveResult().Type.IsKnownType(KnownTypeCode.String)) { + arg0 = RemoveRedundantToStringInConcat(arg0, method, isLastArgument: false).Detach(); + } } - for (int i = 1; i < arguments.Length; i++) { + var expr = new BinaryOperatorExpression(arg0, BinaryOperatorType.Add, arg1); + for (int i = 2; i < arguments.Length; i++) { var arg = arguments[i].Detach(); if (!isInExpressionTree) { arg = RemoveRedundantToStringInConcat(arg, method, isLastArgument: i == arguments.Length - 1).Detach();