diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 4afc0c9..49eb9c0 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -413,34 +413,36 @@ protected internal override Expression VisitUnary(Expr.Unary expr) // A conversion takes the enum itself, so that converting it to object boxes the enum // rather than its underlying value. // - if (nonNullableType.IsEnum && expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus) - Error.NonApplicableUnaryOperator(expr, operandType); - if (nonNullableType.IsEnum && expr.UnaryType != UnaryType.Convert) + { + if (expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus) + Error.NonApplicableUnaryOperator(expr, operandType); + operand = ConvertOperand(operand, nonNullableType.GetEnumUnderlyingType(), lifted); + } - switch (expr.UnaryType, operand) + switch (expr.UnaryType) { // // As in C#, the literals 2147483648 and 9223372036854775808 directly after a unary minus are the constants // int.MinValue and long.MinValue. Parentheses or the U suffix keep their uint and ulong types. // Other suffixes need no check: 2147483648 with L or UL is not a uint, and C# allows L for 9223372036854775808. // - case (UnaryType.Negate, _) when expr.Operand is Expr.Literal { Value: 0x80000000u, HasUnsignedSuffix: false }: + case UnaryType.Negate when expr.Operand is Expr.Literal { Value: 0x80000000u, HasUnsignedSuffix: false }: return Expression.Constant(int.MinValue); - case (UnaryType.Negate, _) when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }: + case UnaryType.Negate when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }: return Expression.Constant(long.MinValue); // // C# has no negation operator for uint, so the operand is converted to long // and then negated, which keeps the sign of the result. // - case (UnaryType.Negate, _) when operand.Type.GetNonNullableType() == typeof(uint): + case UnaryType.Negate when operand.Type.GetNonNullableType() == typeof(uint): operand = ConvertOperand(operand, typeof(long), lifted); break; - case (not UnaryType.Convert, _): + case not UnaryType.Convert: operand = ApplyUnaryNumericPromotion(operand); break; }