dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.01k stars 4.03k forks source link

Nullability no longer honors null check when it is also an assignment #44461

Open jozkee opened 4 years ago

jozkee commented 4 years ago

Version Used: master (19 May 2020)

Steps to Reproduce:

  1. Do assignment of the null check expression inside an if statement.

Repro code: https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+BiAdgVwBtCJhC4ACOXU8gWACgABAJgEZGmBmC1igYQoBvRhTG8eTACwUAsgAoAlMNHi1qtWIBiAex0UAZhQC8FXHADuFXTqUBuDZsdrgewhQCWUAHJFCDhk1xDyN5L19iE0MAOgAFBB0AB0QYAE8KAEJTAmJFZ3ERQKDNJjYATnkDOITkhDTogBUdABkdC0QlRQDiigBfR36GQc5JFms9RkK1bl42AAYAfgp4pJT0oQoAczgYOwooHb3B3qA===

Expected Behavior: Object was already checked for null, nullability should no longer warn "Possible null reference".

Actual Behavior: Nullability still warn "Possible null reference" regardless of the previous null check.

cc @jaredpar @cston @jcouv

jcouv commented 4 years ago

I'm not sure I would rank this high in priority, but I agree we could handle that. I think it's a matter of keeping the value on the right in a split state when analyzing x = someSplitBoolValue.

Copied the repro below:

#nullable enable
using System;
public class C {
    public void M() {

        Foo f = new Foo();

        bool isNull;
        if (isNull = f.Property != null)
        {
            Console.WriteLine(f.Property.ToLower()); // warning CS8602: Dereference of a possibly null reference.
        }
    }
}

public class Foo
{
    public string? Property { get; set; }
}