fluentassertions.analyzers
fluentassertions.analyzers copied to clipboard
Bug for NullConditionalAssertion
// cloudEvent.Time is Nullable<DateTime>
(cloudEvent.Time?.ToUniversalTime()).Should().Be(cloudEvent2.Time?.ToUniversalTime());
This code will get a NullConditionalAssertion warning. But
var x = cloudEvent.Time?.ToUniversalTime();
x.Should().Be(cloudEvent2.Time?.ToUniversalTime());
has no warning.
Is it a bug of the analyzer? If not, how should I fix my code?
Just noticed the same thing. This is definitely a false positive.
Please fix.
In an expression like:
X?.Y.Should().Be(expected);
it is absolutely true that the entire "chain" .Y.Should().Be(expected)
will be skipped if X
is null. In particular, Should
and Be
are never called, and a warning is reasonable.
However, in the statement:
(X?.Y).Should().Be(expected);
the chain stops at )
and Should
is always called. It is an extension method that can be called in a meaningful way on a null, even a reference-type null (which can occur if the compile-time type of Y
is a reference type; in iron9light's example it was a value type which gives a Nullable<>
which is also fine).
@JeppeSN which analyzer is triggering the warning here?
resolved by #170