Browse Source

Fix #1628: Preserve order of child nodes in PatternStatementTransform.VisitBinaryOperatorExpression.

pull/1641/head
Siegfried Pammer 6 years ago
parent
commit
8d2e8cc267
  1. 24
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

24
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -1002,24 +1002,26 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
/// <summary> /// <summary>
/// Use associativity of logic operators to avoid parentheses. /// Use associativity of logic operators to avoid parentheses.
/// </summary> /// </summary>
public override AstNode VisitBinaryOperatorExpression(BinaryOperatorExpression boe1) public override AstNode VisitBinaryOperatorExpression(BinaryOperatorExpression expr)
{ {
switch (boe1.Operator) { switch (expr.Operator) {
case BinaryOperatorType.ConditionalAnd: case BinaryOperatorType.ConditionalAnd:
case BinaryOperatorType.ConditionalOr: case BinaryOperatorType.ConditionalOr:
// a && (b && c) ==> (a && b) && c // a && (b && c) ==> (a && b) && c
var boe2 = boe1.Right as BinaryOperatorExpression; var bAndC = expr.Right as BinaryOperatorExpression;
if (boe2 != null && boe2.Operator == boe1.Operator) { if (bAndC != null && bAndC.Operator == expr.Operator) {
// make boe2 the parent and boe1 the child // make bAndC the parent and expr the child
var b = boe2.Left.Detach(); var b = bAndC.Left.Detach();
boe1.ReplaceWith(boe2.Detach()); var c = bAndC.Right.Detach();
boe2.Left = boe1; expr.ReplaceWith(bAndC.Detach());
boe1.Right = b; bAndC.Left = expr;
return base.VisitBinaryOperatorExpression(boe2); bAndC.Right = c;
expr.Right = b;
return base.VisitBinaryOperatorExpression(bAndC);
} }
break; break;
} }
return base.VisitBinaryOperatorExpression(boe1); return base.VisitBinaryOperatorExpression(expr);
} }
#endregion #endregion
} }

Loading…
Cancel
Save