Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable false positives when boolean conditional is first assigned to a named variable #52717

Closed
j2jensen opened this issue Apr 18, 2021 · 3 comments
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@j2jensen
Copy link

j2jensen commented Apr 18, 2021

Version Used: Microsoft.Net.Compilers.Toolset v3.9.0, or VS/MSBuild 16.9.4

Steps to Reproduce:

  1. Write code like this. Note the lack of compiler warnings.
#nullable enable

if(!(new[] { "" }.ToDictionary(i => i.ToString())
    .TryGetValue("", out var value) && value.Any()))
{
    Console.WriteLine("No value");
}
else
{
    Console.WriteLine(value.ToString());
}
  1. Now extract a named variable:
#nullable enable

var hasNonEmptyValue = new[] { "" }.ToDictionary(i => i.ToString())
    .TryGetValue("", out var value) && value.Any();
if(!hasNonEmptyValue)
{
    Console.WriteLine("No value");
}
else
{
    Console.WriteLine(value.ToString());
}

Expected Behavior:
The compiler should know that value cannot be null in the else block, just as it knows that when not using a variable.

Actual Behavior:
CS8602 warning upon calling value.ToString()
Similar behavior with other nullable-related errors if I try to pass the value as an argument, assign it to a field, etc.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 18, 2021
@j2jensen j2jensen changed the title Nullable false positives when Nullable false positives when boolean conditional is first assigned to a named variable Apr 18, 2021
@j2jensen
Copy link
Author

I just realized this is described in this SO post, where Mads Torgensen mentions it might be revisited in C# 9. I'm guessing it wasn't?

@Youssef1313
Copy link
Member

The current behavior is by design. This may need a detailed proposal on how things should be improved. #27011

@RikkiGibson
Copy link
Contributor

It would be great if someone decided to explore this issue in https://github.com/dotnet/csharplang/discussions. In particular, how associating conditional state with boolean variables should work, and how "invalidating" such state should work when changes to the underlying variables occur, i.e.

void M(string? x)
{
    bool b = x != null;
    x = null; // perhaps conditional state associated with 'b' is now "out of date"
    if (b)
    {
        x.ToString(); // warning
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

4 participants