From 8e9ecf6c36d42c985be5b192961028e75ff94edb Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 22 Sep 2020 12:11:07 +0200 Subject: [PATCH] Fix #2162: handle VariableInitializers like AssignmentExpressions in InsertParenthesesVisitor --- .../TestCases/ILPretty/Issue1389.cs | 2 +- .../TestCases/ILPretty/Issue1454.cs | 2 +- .../TestCases/ILPretty/Issue684.cs | 6 +++--- .../TestCases/Pretty/DelegateConstruction.cs | 16 +++++++------- .../TestCases/Pretty/ExpressionTrees.cs | 4 ++-- .../TestCases/Pretty/FixProxyCalls.cs | 2 +- .../TestCases/Pretty/InlineAssignmentTest.cs | 2 +- .../TestCases/Pretty/QueryExpressions.cs | 8 +++---- .../TestCases/Pretty/ThrowExpressions.cs | 4 ++-- .../TestCases/Pretty/TypeAnalysisTests.cs | 4 ++-- .../OutputVisitor/InsertParenthesesVisitor.cs | 21 ++++++++++++++----- 11 files changed, 41 insertions(+), 30 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs index e2ee2eb3d..9af1d17ff 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs @@ -12,7 +12,7 @@ namespace Issue1389 private static void UnusedResultOfIsinst() { - _ = (GetObject() is TypeCode); + _ = GetObject() is TypeCode; } private static bool BoolResultOfIsinst() diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1454.cs b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1454.cs index b7aea6421..0a436759b 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1454.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1454.cs @@ -13,7 +13,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty for (int i = 0; i < array.Length; i++) { int num2 = array[i]; - num2 -= ((num2 >> 1) & 0x55555555); + num2 -= (num2 >> 1) & 0x55555555; num2 = (num2 & 0x33333333) + ((num2 >> 2) & 0x33333333); num2 = ((num2 + (num2 >> 4)) & 0xF0F0F0F) * 16843009 >> 24; num += num2; diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue684.cs b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue684.cs index e10af2db7..e33bde26f 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue684.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue684.cs @@ -11,7 +11,7 @@ public static class Issue684 bool num2 = num >= 1000; if (!num2) { - num2 = (num < 2); + num2 = num < 2; } if (num2) { @@ -25,7 +25,7 @@ public static class Issue684 Console.WriteLine(num3); for (; i <= num; i += num3) { - int num4 = array[i] = 1; + int num4 = (array[i] = 1); } i = num3; while (true) @@ -33,7 +33,7 @@ public static class Issue684 bool num5 = i <= num; if (num5) { - num5 = (array[i] != 0); + num5 = array[i] != 0; } if (!num5) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs index f03a062cc..fdb31a779 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs @@ -247,14 +247,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } - public static Func test0 = (string a, string b) => string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b); - public static Func test1 = (string a, string b) => string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b); - public static Func test2 = (string a, string b) => !string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b); - public static Func test3 = (string a, string b) => !string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b); - public static Func test4 = (string a, string b) => string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b); - public static Func test5 = (string a, string b) => string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b); - public static Func test6 = (string a, string b) => !string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b); - public static Func test7 = (string a, string b) => !string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b); + public static Func test0 = ((string a, string b) => string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b)); + public static Func test1 = ((string a, string b) => string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b)); + public static Func test2 = ((string a, string b) => !string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b)); + public static Func test3 = ((string a, string b) => !string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b)); + public static Func test4 = ((string a, string b) => string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b)); + public static Func test5 = ((string a, string b) => string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b)); + public static Func test6 = ((string a, string b) => !string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b)); + public static Func test7 = ((string a, string b) => !string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b)); public static void Test(this string a) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs index f59a17e83..0ecf7cb8c 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs @@ -607,7 +607,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty 2012 }.All(set.Add)); - Func, bool> sink = (Func f) => f(null, null); + Func, bool> sink = ((Func f) => f(null, null)); ToCode(X(), () => sink(object.Equals)); } @@ -623,7 +623,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public void NestedLambda() { - Func, int> call = (Func f) => f(); + Func, int> call = ((Func f) => f()); //no params ToCode(X(), () => call(() => 42)); //one param diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/FixProxyCalls.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/FixProxyCalls.cs index 58cc8cbd0..b71edc02d 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/FixProxyCalls.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/FixProxyCalls.cs @@ -57,7 +57,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty { protected internal override string Test(string test) { - Func func = (string a) => base.Test(a); + Func func = ((string a) => base.Test(a)); test = string.Join(test, "aa"); return func(test); } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InlineAssignmentTest.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InlineAssignmentTest.cs index 595dc2b82..e3727af84 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InlineAssignmentTest.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InlineAssignmentTest.cs @@ -146,7 +146,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public bool BoolPropertyTest(object x) { - return BoolProperty = (x != null); + return BoolProperty = x != null; } } } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs index f1c957790..9bb0e5da2 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs @@ -235,18 +235,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public static IEnumerable Issue1310a(bool test) { #if ROSLYN && OPT - IEnumerable obj = test ? (from c in Enumerable.Range(0, 255) + IEnumerable obj = (test ? (from c in Enumerable.Range(0, 255) where char.IsLetter((char)c) select (char)c) : (from c in Enumerable.Range(0, 255) where char.IsDigit((char)c) - select (char)c); + select (char)c)); return obj.Concat(obj); #else - IEnumerable enumerable = test ? (from c in Enumerable.Range(0, 255) + IEnumerable enumerable = (test ? (from c in Enumerable.Range(0, 255) where char.IsLetter((char)c) select (char)c) : (from c in Enumerable.Range(0, 255) where char.IsDigit((char)c) - select (char)c); + select (char)c)); return enumerable.Concat(enumerable); #endif } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ThrowExpressions.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ThrowExpressions.cs index fc0269c2d..7be31a5f6 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ThrowExpressions.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ThrowExpressions.cs @@ -14,8 +14,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public ArgumentCheckingCtor(object simpleObj, int? nullableInt) { - this.simpleObj = (simpleObj ?? throw new ArgumentNullException("simpleObj")); - this.nullableInt = (nullableInt ?? throw new ArgumentNullException("nullableInt")); + this.simpleObj = simpleObj ?? throw new ArgumentNullException("simpleObj"); + this.nullableInt = nullableInt ?? throw new ArgumentNullException("nullableInt"); } public ArgumentCheckingCtor(string input) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs index cd164998e..ab01251d6 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs @@ -35,7 +35,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty return (int)(uint64Field & 0x7FFFFFFF); } set { - uint64Field = ((uint64Field & 0xFFFFFFFF80000000uL) | (ulong)value); + uint64Field = (uint64Field & 0xFFFFFFFF80000000uL) | (ulong)value; } } @@ -44,7 +44,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty return (ushort)((uint64Field & 0xFFFF000000000000uL) >> 48); } set { - uint64Field = ((uint64Field & 0xFFFFFFFFFFFFuL) | ((ulong)value << 48)); + uint64Field = (uint64Field & 0xFFFFFFFFFFFFuL) | ((ulong)value << 48); } } diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs index c266d677a..45ff80826 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs +++ b/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs @@ -425,19 +425,30 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor { // assignment is right-associative ParenthesizeIfRequired(assignmentExpression.Left, PrecedenceLevel.Assignment + 1); - if (InsertParenthesesForReadability && !(assignmentExpression.Right is DirectionExpression)) + HandleAssignmentRHS(assignmentExpression.Right); + base.VisitAssignmentExpression(assignmentExpression); + } + + private void HandleAssignmentRHS(Expression right) + { + if (InsertParenthesesForReadability && !(right is DirectionExpression)) { - ParenthesizeIfRequired(assignmentExpression.Right, PrecedenceLevel.RelationalAndTypeTesting + 1); + ParenthesizeIfRequired(right, PrecedenceLevel.Conditional + 1); } else { - ParenthesizeIfRequired(assignmentExpression.Right, PrecedenceLevel.Assignment); + ParenthesizeIfRequired(right, PrecedenceLevel.Assignment); } - base.VisitAssignmentExpression(assignmentExpression); } - // don't need to handle lambdas, they have lowest precedence and unambiguous associativity + public override void VisitVariableInitializer(VariableInitializer variableInitializer) + { + if (!variableInitializer.Initializer.IsNull) + HandleAssignmentRHS(variableInitializer.Initializer); + base.VisitVariableInitializer(variableInitializer); + } + // don't need to handle lambdas, they have lowest precedence and unambiguous associativity public override void VisitQueryExpression(QueryExpression queryExpression) { // Query expressions are strange beasts: