fluentassertions.analyzers icon indicating copy to clipboard operation
fluentassertions.analyzers copied to clipboard

Bug for NullConditionalAssertion

Open iron9light opened this issue 4 years ago • 1 comments

// 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?

iron9light avatar Jan 06 '20 09:01 iron9light

Just noticed the same thing. This is definitely a false positive.

thomaslevesque avatar Jun 22 '20 14:06 thomaslevesque

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 avatar Jan 11 '23 14:01 JeppeSN

@JeppeSN which analyzer is triggering the warning here?

Meir017 avatar Jan 12 '23 09:01 Meir017

resolved by #170

Meir017 avatar May 27 '23 21:05 Meir017