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

Missing nullable warnings in member initializers #31440

Closed
TessenR opened this issue Nov 29, 2018 · 2 comments
Closed

Missing nullable warnings in member initializers #31440

TessenR opened this issue Nov 29, 2018 · 2 comments

Comments

@TessenR
Copy link

TessenR commented Nov 29, 2018

Version Used:

Branch C# 8.0: Nullable reference type (29 Oct 2018)
https://github.com/dotnet/csharplang/blob/master/proposals/nullable-reference-types.md
Latest commit 2f8fef by AlekseyTs:
Compare Nullable modifiers while comparing type symbols by default. (#30770)

Steps to Reproduce:

Compile the following code

#nullable enable
public class C {
    public static string? field;
    public string s = field.ToString(); // no warning
    public void M() => field.ToString(); // warning CS8602: Possible dereference of a null reference.
}

https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQDk4BstWFiAJQkiQDsBjCAFQE8AHFAHwGIztd8IACCMvAgFgAUAAEAzN1EAmbgGFuAbxHdVUyaICMANimaADAH5uYAJYQsAEwDcKtRL37uybgF4T5qwDpqAewDKMAimZADmABQAlNbcAPSx3GS+3ADuUAhkIaF2qg6iACzcALJRbgB8HhaWPgFBWVEx8anpmWEiAL5AA

Expected Behavior:

Consistent warnings for both dereferencing of field.

Actual Behavior:

Field initializer does not have the warning

@DarthVella
Copy link

This is also a problem when directly assigning null to a non-nullable member. However, it only arises if there is no explicit constructor (or static constructor, for static variables). For instance:

using System;
#nullable enable

class MyObject 
{
    public static string myStaticString = null; //no warning

    public string myString = null; //no warning
}

class MyOtherObject 
{
    public static string myStaticString = null; //correct warning
    static MyOtherObject() { }

    public string myString = null; //correct warning
    public MyOtherObject() { }
}

@cston
Copy link
Member

cston commented Feb 5, 2019

Fixed in #33093.

@cston cston closed this as completed Feb 5, 2019
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

5 participants