Nullable access in lambda warns about System.InvalidOperationException
Completed
this is exactly the same problem that was reported back in 2013. I think it was fixed, because I didn't see it until I upgraded to 2021.2
var x = tasks.Where(i => i.CompleteById.HasValue).Select(i => i.CompleteById.Value)
shows the error. And just like in an old post that I linked to, if I use foreach instead, ReSharper is smart enough to *not* throw an error:
foreach (TaskOverview task in tasks) {
if (task.CompleteById.HasValue) {
x.Add(task.CompleteById.Value);
}
}
Obviously, I can explicitly put CompletedBy!.Value - but I would prefer not to!
Thank you
Please sign in to leave a comment.
As a workaround, you can probably replace the .Where(...).Select(...) with a .Select(...).OfType<T>() call.
The OfType call will ignore any values which are null.
Thanks, Richard. As is said before, as a workaround I can just use !... but I don't want to use workaround - I want JetBrains team to fix it. Incidentally, it happens on some machines; and doesn't happen on others. Same version of Visual studio, same version of Re#
Hello Felix, thanks for the report. There is a pretty similar (and old :( ) issue on YouTrack https://youtrack.jetbrains.com/issue/RSRP-303988. I'm sorry the problem is still not fixed. Please vote for the issue in order to demonstrate additional interest. Thank you!
Thank you, Maria. We just confirmed on another machine - the problem appeared after upgrading from Re# 2021.1 to 2021.2. So, it *was* fixed for awhile - if you could see where the regression happened, that would be great! It looks like we have close to a hundred places with code like this - and now we are faced with a dilemma: stick a `!' to just satisfy Re# or have many false positives and run the risk of missing a *real* InvalidOperationException
Hello Felix, thanks for the reply. I may have reproduced another problem, as I see this problem in the previous 2021.1 and 2020.3.4 versions as well... Do you have a chance to share a small sample solution so that I can reproduce the problem on my side (you can upload the solution to our server via https://uploads.jetbrains.com/ form)? Thanks in advance!
Just did... interestingly, in the trivial example that I uploaded the warning is slightly different (Nullable value may be null)
UPDATE: Interesting... I created a trivial example *with* Entity Framework and I am *not* getting a warning
UPDATE2: I created a small example where I am able to reproduce InvalidOperationException. Upload id: 2021_08_24_CxfuyTRv54ixpDMq (file: Resharperef.zip)
Felix, thank you so much for your help. I've submitted a new ticket to our bug tracker: https://youtrack.jetbrains.com/issue/RSRP-485747. Please comment or vote for it to get the notifications about status changes. Thank you!