Browse Source

[nullables] Fix unnecessary casts in nullable compound assignments.

pull/897/head
Daniel Grunwald 8 years ago
parent
commit
8e634bc23c
  1. 23
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

23
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -819,17 +819,27 @@ namespace ICSharpCode.Decompiler.CSharp
} else { } else {
switch (op) { switch (op) {
case AssignmentOperatorType.Add: case AssignmentOperatorType.Add:
case AssignmentOperatorType.Subtract: case AssignmentOperatorType.Subtract: {
value = value.ConvertTo(target.Type.GetEnumUnderlyingType(), this, inst.CheckForOverflow); IType targetType = NullableType.GetUnderlyingType(target.Type).GetEnumUnderlyingType();
break; if (NullableType.IsNullable(value.Type)) {
targetType = NullableType.Create(compilation, targetType);
}
value = value.ConvertTo(targetType, this, inst.CheckForOverflow);
break;
}
case AssignmentOperatorType.Multiply: case AssignmentOperatorType.Multiply:
case AssignmentOperatorType.Divide: case AssignmentOperatorType.Divide:
case AssignmentOperatorType.Modulus: case AssignmentOperatorType.Modulus:
case AssignmentOperatorType.BitwiseAnd: case AssignmentOperatorType.BitwiseAnd:
case AssignmentOperatorType.BitwiseOr: case AssignmentOperatorType.BitwiseOr:
case AssignmentOperatorType.ExclusiveOr: case AssignmentOperatorType.ExclusiveOr: {
value = value.ConvertTo(target.Type, this, inst.CheckForOverflow); IType targetType = NullableType.GetUnderlyingType(target.Type);
break; if (NullableType.IsNullable(value.Type)) {
targetType = NullableType.Create(compilation, targetType);
}
value = value.ConvertTo(targetType, this, inst.CheckForOverflow);
break;
}
} }
resultExpr = new AssignmentExpression(target.Expression, op, value.Expression) resultExpr = new AssignmentExpression(target.Expression, op, value.Expression)
.WithILInstruction(inst) .WithILInstruction(inst)
@ -842,6 +852,7 @@ namespace ICSharpCode.Decompiler.CSharp
TranslatedExpression HandleCompoundShift(CompoundAssignmentInstruction inst, AssignmentOperatorType op) TranslatedExpression HandleCompoundShift(CompoundAssignmentInstruction inst, AssignmentOperatorType op)
{ {
Debug.Assert(inst.CompoundAssignmentType == CompoundAssignmentType.EvaluatesToNewValue);
var target = Translate(inst.Target); var target = Translate(inst.Target);
var value = Translate(inst.Value); var value = Translate(inst.Value);

Loading…
Cancel
Save