"Expression is always false" in pattern only when #nullable enable is off

I have a .NET Standard 2.1 project (thus it uses C# 8) but have the project-level setting for reference type nullability as off. I turn it on for select files in that project with #nullable enable.

If I have code with try/catch that uses pattern matching, matching a more derived type after checking for a broader type underlines the former with "Expression is always false" but only in the files that I haven't turned on nullability.

This block has the "e is TaskCanceledException" underlined with the "expression is always false" message (file has nullability off), but if all I do is turn on #nullable enable for the file, the underline goes away.

try
{
  // some code that could throw an OperationCanceledException or a TaskCanceledException
}
catch (Exception e) when (e is OperationCanceledException || e is TaskCanceledException)
{
 Logger.Trace($"In {nameof(ShowMenu)}: A task was canceled.");
}
Is this expected? If so, I don't understand why nullability being enabled or not affects things in this case. "e" is never null.
0
4 comments

Hello Brian Runck, thank you for your question.

The warning is only displayed when "#nullable disable" is used because the ReSharper control flow analysis examines data types, whereas ReSharper NRT analysis does not. Please let me know if you have any questions.

1

Hello Brian Runck, thanks for the reply and sorry for using the abbreviation. NRT = Nullable reference types.

1

Brian Runck, I've created a new issue in our bug tracker to improve this analysis in future: https://youtrack.jetbrains.com/issue/RSRP-491007/Expression-is-always-false-in-pattern-only-when-nullable-enable-is-off

Please leave a comment or vote on it to receive updates on its status. Have a nice day!

0

Please sign in to leave a comment.