Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ternary expression producing faulty generated code #116

Closed
space-alien opened this issue Sep 2, 2024 · 0 comments
Closed

Ternary expression producing faulty generated code #116

space-alien opened this issue Sep 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@space-alien
Copy link

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;
}
@koenbeuk koenbeuk added the bug Something isn't working label Sep 2, 2024
koenbeuk added a commit that referenced this issue Oct 22, 2024
Fix missing parenthesis around ternary expressions (Fixes #116)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants