Using FluentAssertions NotBeNull isn't taken into account by nullable analysers
08:04 22 Sep 2021

When I use Fluent assertions to test if a property is not null, the analysers still complain about subsequent lines which dereference that line as possibly null. Is there any way to get the compiler to recognise the property as not being null after testing with FluentAssertions? e.g.

Foo? foo = Bar();

foo.Should().NotBeNull();
foo.Value.Should().Be(5); // Warning about dereference of a possibly null reference on this line

I know I can use a ! on the foo in the second line but is there any way to get the analysers to work this out for itself?

I did find this but I can't see how it can be used in this case.

c# .net nullable-reference-types fluent-assertions