From c6f028938869d6eaf886f7ea2d3237da72b4ac3c Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 19 Nov 2026 22:49:12 +0500 Subject: [PATCH] parser: reject void branches of the conditional operator --- .../ExpressionBuilder.Helpers.cs | 9 ++++++++- tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs index eaac1d9..01035ae 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs @@ -105,7 +105,8 @@ private static MemberExpression CreateMemberAccess(Expression? expression, Membe /// /// As in C#, the common type is the type of one of the branches, so it is only /// when a branch is of that type, for example after an explicit conversion. The null literal takes the type - /// of the other branch if that type accepts . + /// of the other branch if that type accepts . A branch of type + /// is rejected. /// private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expression ifTrue, ref Expression ifFalse) { @@ -131,6 +132,12 @@ private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expre isFalseNull ? null : ifFalse.Type); } + // + // A branch of type void has no value, so the conditional expression would have none either. + // + if (ifTrue.Type == typeof(void) || ifFalse.Type == typeof(void)) + Error.NonApplicableConditionalOperator(ifTrue.Type, ifFalse.Type); + if (ifTrue.Type == ifFalse.Type) return ifTrue.Type; diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index df597bd..fd43971 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -269,5 +269,8 @@ Operator '?:' cannot be applied to operands of type 'System.Int32' and ''. true ? null : null Operator '?:' cannot be applied to operands of type '' and ''. +true ? TimeZoneInfo.ClearCachedData() : TimeZoneInfo.ClearCachedData() +Operator '?:' cannot be applied to operands of type 'System.Void' and 'System.Void'. + (true ? "abc" : null :> object).Length Cannot resolve symbol 'Length'.