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

Unexpected Dereference of a possibly null reference warning #38084

Closed
tndata opened this issue Aug 18, 2019 · 8 comments
Closed

Unexpected Dereference of a possibly null reference warning #38084

tndata opened this issue Aug 18, 2019 · 8 comments

Comments

@tndata
Copy link

tndata commented Aug 18, 2019

Version Used:
Visual Studio 2019 16.2.2
.NET Core 3 , Preview 8

Steps to Reproduce:
When I compile this code, I get a Dereference of a possibly null reference warning in the Console.WriteLine statement.

using System;

#nullable enable
public class Test
{

    public void DoStuff(Customer? customer)
    {
        if (customer != null)
        {
            if (customer?.Name?.Contains("hello") ?? false)
            {
                Console.WriteLine(customer.Name);  //warning here
            }
        }
    }
}

public class Customer
{
    public string Name = "Hello";
}

See the code here:
https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABATARgFgAoEgYgDsBXAGxomBrgAI4KGmTsBmZvZgCpxYJAN4kSzKX17YALMwAiAewDKMKgDNNACgDCVWMrCIA/MwDGhmMcQBKSdPHFpr5gEtNzHVaMmEzACEALzM1HQOLm5SztHRnt6+Nv6mAHQAchAmaXrKFDAQ7hRQOgBEABZwdMqldsym5poQNFBwkXFusR1x2PgAnD7WtggZWW0A3I7dzAC+U25zUVKLi1yyuMwGfohi8zx8+AAMzJkmzKGlABJVNDWTxDNAA=

Expected Behavior:
I would expect no warnings from the code, because the customer can never be null when the Console WriteLine is called.

Actual Behavior:
I get a warning "warning CS8602: Dereference of a possibly null reference."

@0xd4d
Copy link

0xd4d commented Aug 18, 2019

Change customer?. to customer. and you won't get that warning. You're telling the compiler that it can still be null.

@tndata
Copy link
Author

tndata commented Aug 19, 2019

Thanks, I assumed the compiler would remember that the customer can't bee null anymore due to the initial if-statement and that the use of ? wouldn't affect that.

@RikkiGibson
Copy link
Contributor

I believe this is really a manifestation of #37394, which is fixed in 16.4. I tried it out just now and it's handling this scenario as expected (no diagnostics even when using customer?.Name). It sounds like you have found a workaround, which is good too.

Thanks for reporting this issue.

@RikkiGibson
Copy link
Contributor

Basically, it's true that conditional-accessing something with ?. hints to the compiler that the LHS might be null, but your ?? false check should have given a subsequent hint that everything being conditionally-accessed was not-null 😄

@RikkiGibson RikkiGibson added this to the 16.4.P1 milestone Aug 20, 2019
@longwolf
Copy link

Shouldn't be a warning, just an info in the line
            if (customer?.Name?.Contains("hello") ?? false)
at the question mark after "customer"
with contents like "superfluous null check"

@CyrusNajmabadi
Copy link
Member

at the question mark after "customer", with contents like "superfluous null check"

You could always write an analyzer to check for those sorts of redundancies :)

@toni1elias
Copy link

Having the same warning when using XMLNodeList.Item(0).InnerText.

@longwolf
Copy link

Actually, an XmlNodeList can be empty; and unlike other lists, XmlNodeList.Item(int) does not throw an IndexOutOfRangeException but simply returns null when the index is too big.

(See https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnodelist.item?view=net-5.0)

Or did you mean, XmlNodeList.Item(0)?.InnerText ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants