Skip to content

Binary expression node with non existed enum value throw System.NullReferenceException #97

Description

@letarak

Describe the bug
ToString throw exception when render binary expression with non existed enum value

To Reproduce

using System.Linq.Expressions;
using ExpressionTreeToString;

//OK
_ = ((Expression<Func<TestEnum, bool>>)(it => it > TestEnum.A)).ToString("c#");

//OK
_ = ((Expression<Func<TestEnum, bool>>)(it => it > (TestEnum)1)).ToString("c#");

//OK
_ = ((Expression<Func<TestEnum, bool>>)(it => (int)it > 1)).ToString("c#");

//EXCEPTION
_ = ((Expression<Func<TestEnum, bool>>)(it => (int)it > 0)).ToString("c#");

//EXCEPTION
_ = ((Expression<Func<TestEnum, bool>>)(it => it > (TestEnum)0)).ToString("c#");

public enum TestEnum { A = 1 }

Expected behavior
Output without exception, something like
it => (Int32)it > 1
or
it => it > (TestEnum)1

Version info
net8.0
ExpressionTreeToString 3.4.71

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at ExpressionTreeToString.Util.ExpressionExtensions.Deconstruct(Expression expr, ExpressionType& nodeType, Type& type)
   at ExpressionTreeToString.CSharpWriterVisitor.getPrecedence(Expression node, Type parentType)
   at ExpressionTreeToString.CSharpWriterVisitor.Parens(OneOf`2 arg, String path, Expression childNode)
   at ExpressionTreeToString.CSharpWriterVisitor.WriteBinary(ExpressionType nodeType, String leftPath, Expression left, String rightPath, Expression right)
   at ExpressionTreeToString.CodeWriterVisitor.WriteBinary(BinaryExpression expr)
   at ExpressionTreeToString.BuiltinsWriterVisitor.writeExpression(Expression expr)
   at ExpressionTreeToString.BuiltinsWriterVisitor.WriteNodeImpl(Object o, Boolean parameterDeclaration, Object metadata)
   at ExpressionTreeToString.WriterVisitorBase.WriteNode(String pathSegment, Object o, Boolean parameterDeclaration, Object metadata)
   at ExpressionTreeToString.CSharpWriterVisitor.WriteLambda(LambdaExpression expr)
   at ExpressionTreeToString.BuiltinsWriterVisitor.writeExpression(Expression expr)
   at ExpressionTreeToString.BuiltinsWriterVisitor.WriteNodeImpl(Object o, Boolean parameterDeclaration, Object metadata)
   at ExpressionTreeToString.WriterVisitorBase.WriteNode(String pathSegment, Object o, Boolean parameterDeclaration, Object metadata)
   at ExpressionTreeToString.WriterVisitorBase..ctor(Object o, OneOf`2 languageArg, IEnumerable`1 insertionPointKeys, Boolean hasPathSpans)
   at ExpressionTreeToString.BuiltinsWriterVisitor..ctor(Object o, OneOf`2 languageArg, IEnumerable`1 insertionPointKeys, Boolean hasPathSpans)
   at ExpressionTreeToString.CodeWriterVisitor..ctor(Object o, OneOf`2 languageArg, Boolean hasPathSpans)
   at ExpressionTreeToString.CSharpWriterVisitor..ctor(Object o, Boolean hasPathSpans)
   at ExpressionTreeToString.Renderers.<>c.<.cctor>b__6_0(Object o, OneOf`2 languageArg, Boolean usePathSpans)
   at ExpressionTreeToString.Renderers.Invoke(OneOf`2 rendererArg, Object o, OneOf`2 language)
   at ExpressionTreeToString.ExpressionExtension.ToString(Expression expr, OneOf`2 rendererArg, OneOf`2 language)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions