Skip to content

Ternary expression producing faulty generated code #116

@space-alien

Description

@space-alien

I'm using v4.0.0-preview4

The following method returns x != null && x != 4:

[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
public static bool Test(this object? x)
    => x?.Equals(4) == false;

Unfortunately, the generated code has a nasty bug!
It now returns x != null && x == 4 - inverting the result when x is not null.

static global::System.Linq.Expressions.Expression<global::System.Func<object, bool>> Expression()
{
    return (object x) => x != null ? (x.Equals(4)) : (bool?)null == false;
}

The ternary expression should be wrapped in parentheses before == false.
This seems somewhat similar to #115.
The corrected code should read:

static global::System.Linq.Expressions.Expression<global::System.Func<object, bool>> Expression()
{
    return (object x) => (x != null ? (x.Equals(4)) : (bool?)null) == false;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions