diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
index 0584438..e51d946 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
@@ -280,6 +280,32 @@ private static Expr SkipParentheses(Expr expr)
return expr;
}
+ ///
+ /// Converts the null literal opposite an operand of a nullable value type to that type.
+ /// For the + operator with an operand of a nullable enum type, the literal is converted
+ /// to the nullable form of the underlying type of the enum.
+ ///
+ /// The binary expression.
+ /// The expression of the left operand.
+ /// The expression of the right operand.
+ private static void ApplyNullLiteralConversion(Expr.Binary expr, ref Expression lhs, ref Expression rhs)
+ {
+ //
+ // The null literal converts to a nullable value type, so opposite an operand of such a type
+ // it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
+ // are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
+ //
+ if (lhs.Type.IsNullable() && IsNullLiteral(expr.Right))
+ rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhs.Type));
+ else if (rhs.Type.IsNullable() && IsNullLiteral(expr.Left))
+ lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhs.Type));
+
+ static Type GetNullLiteralType(Identifier op, Type operandType) =>
+ op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
+ ? enumType.GetEnumUnderlyingType().MakeNullableType()
+ : operandType;
+ }
+
///
/// Converts the integer literal 0 opposite an operand of an enum type to that enum type
/// if the operator is defined for two operands of the enum type. As in the C# compiler, a literal
diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
index 1361319..c825a28 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
@@ -64,16 +64,7 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
var lhsType = lhs.Type;
var rhsType = rhs.Type;
- //
- // The null literal converts to a nullable value type, so opposite an operand of such a type
- // it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
- // are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
- //
- if (lhsType.IsNullable() && IsNullLiteral(expr.Right))
- rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhsType));
- else if (rhsType.IsNullable() && IsNullLiteral(expr.Left))
- lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhsType));
-
+ ApplyNullLiteralConversion(expr, ref lhs, ref rhs);
ApplyEnumZeroConversion(expr, ref lhs, ref rhs);
var factory = ResolveBinaryOperatorFactory(expr.Operator.Name);
@@ -173,11 +164,6 @@ void Flatten(Expression e)
}
return null;
-
- static Type GetNullLiteralType(Identifier op, Type operandType) =>
- op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
- ? enumType.GetEnumUnderlyingType().MakeNullableType()
- : operandType;
}
///