From f8a73b6622fb8497461a9a6828d3950345f80e8d Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 17:39:21 +0500 Subject: [PATCH] parser: reject unary '-' and '+' on enum operands The unary '-' and '+' operators accepted an enum operand and applied to its underlying value, returning a number: for example, -DayOfWeek.Friday returned -5 and +DayOfWeek.Friday returned 5. C# does not define these operators for an enum. Report that the operator cannot be applied to the enum type, as for '!'. The value can still be negated after an explicit conversion, such as -(DayOfWeek.Friday :> int). --- src/Ramstack.ExpressionParser/ExpressionBuilder.cs | 5 ++++- tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt | 9 +++++++++ .../ExpressionParserTest.cs | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 634790b..58df7a5 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -385,10 +385,13 @@ protected internal override Expression VisitUnary(Expr.Unary expr) : operandType; // - // Arithmetic and bitwise operators apply to the underlying type of an enum. + // C# does not define the unary '-' and '+' operators for an enum, and '~' applies to its underlying type. // A conversion takes the enum itself, so that converting it to object boxes the enum // rather than its underlying value. // + if (operandType.IsEnum && expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus) + Error.NonApplicableUnaryOperator(expr, operandType); + if (operandType.IsEnum && expr.UnaryType != UnaryType.Convert) operand = Expression.Convert(operand, underlyingType); diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index 6cde52d..f340493 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -137,6 +137,15 @@ Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.I -1ul Unary operator '-' cannot be applied to operand of type 'System.UInt64'. +-DayOfWeek.Friday +Unary operator '-' cannot be applied to operand of type 'System.DayOfWeek'. + ++DayOfWeek.Friday +Unary operator '+' cannot be applied to operand of type 'System.DayOfWeek'. + +-StringSplitOptions.RemoveEmptyEntries +Unary operator '-' cannot be applied to operand of type 'System.StringSplitOptions'. + ####################################### # ! operator diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 0247661..2f8f087 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -36,6 +36,7 @@ private static List EvaluatesSource list.Add(["""Guid.Empty""", Guid.Empty]); list.Add(["""1 :> DayOfWeek :> object""", DayOfWeek.Monday]); list.Add(["""(DayOfWeek.Friday :> object).ToString()""", "Friday"]); + list.Add(["""-(DayOfWeek.Friday :> int)""", -5]); return list; } }