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 reference types: Analysis of null checks on fields #9978

Closed
bkoelman opened this issue Mar 22, 2016 · 3 comments
Closed

Nullable reference types: Analysis of null checks on fields #9978

bkoelman opened this issue Mar 22, 2016 · 3 comments

Comments

@bkoelman
Copy link
Contributor

I have been playing with nullable reference types, which is already working remarkably great! To try it out, I have patched up my analyzer to convert Resharper's (Item)NotNull/CanBeNullAttribute annotations to nullable reference types. Then ran that on an actual annotated codebase.

During that process I discovered some oddities, which I'll create issues for here.

Version Used: Built from source, branch NullableReferenceTypes (commit 8553cda)

Steps to Reproduce:
The next block of code raises warning 8202: Possible dereference of a null reference:

public class Test
{
    private string? value;

    public void M()
    {
        if (value != null)
        {
            int i = value.Length; // CS8202
        }
    }
}

This can be worked around using:

    public void M()
    {
        string? valueSnapshot = value;
        if (valueSnapshot != null)
        {
            int i = valueSnapshot.Length; // no warning
        }
    }

Expected Behavior: The behavior is technically correct. Another thread could change the field value in-between. I cannot think of a better way, just wanted to mention this scenario, which gets somewhat annoying, as it occurs very often (in non-multithreaded code).

Actual Behavior:
CS8202 warning on the line with value.Length inside the if statement.

@AlekseyTs

@AlekseyTs
Copy link
Contributor

At this point the behavior is expected, state of fields of reference types is not tracked.

@AlekseyTs
Copy link
Contributor

CC @MadsTorgersen

@jaredpar jaredpar added the Bug label Apr 4, 2016
@VSadov VSadov modified the milestone: 2.0 (Preview 1) Jun 11, 2016
@gafter gafter modified the milestones: 2.0 (Preview 1), 2.0 (RC) Jun 20, 2016
@gafter gafter modified the milestones: 2.0 (RC), 3.0 Jul 19, 2016
@cston
Copy link
Member

cston commented Apr 1, 2018

Fixed in #23063.

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