Skip to content

ConstantSubExpressionReductionVisitor throws on lifted operator #1712

@jnyrup

Description

@jnyrup

Description

When using an expression containing a lifted operator, i.e. > on a null, the call to ExpressionVisitor.Visit fails.
This is probably because there is static bool operator<(int?, int?), it's only syntax sugar.
So

public bool M(int? i) => i > 42;

is transformed into

public bool M(Nullable<int> i)
{
    Nullable<int> num = i;
    int num2 = 42;
    return (num.GetValueOrDefault() > num2) & num.HasValue;
}

Complete minimal example reproducing the issue

public class TestObject
{
    public int? Prop { get; set; }
}

public class UnitTest
{
    [Fact]
    public void LiftedBinaryOperator()
    {
        var subject = new TestObject { Prop = 42 };
        subject.Should().Match<TestObject>(e => e.Prop > 43);
    }
}

Expected behavior:

Assertion should throw with a proper failure description.
E.g.

Xunit.Sdk.XunitException: 'Expected subject to match (e.Prop > Convert(43)), but found FluentAssertions.Specs.TestObject
{
    Prop = 42
}.'

Actual behavior:

System.InvalidOperationException: 'The binary operator GreaterThan is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'.'

Versions

  • Which version of Fluent Assertions are you using: master
  • Which .NET runtime and version are you targeting? net 5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions