I stumbled across a specific 'is always false' warning from ReSharper (in Rider) that I first thought was incorrect, but from what I gathered from others usually if that happens ReSharper is smarter than the programmer, so I'm trying to understand and learn, maybe someone can help me.
I've recreated the situation in an isolated empty class:
private void Method(object obj)
{
if (obj == null)
return;
var action = new Action(() =>
{
if (obj == null) // this condition is marked as always false
return;
// do stuff
});
// save reference to action somewhere
}
My thought process here was that I don't know when action
might be called and while obj
cannot be null when action
is created, it very well could be when it is invoked. ReSharper seems to think differently.
Language version is C# 9.0, Nullable reference types are disabled.