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

Variable should be nullable outside try block if assigned nullable value inside try block #30561

Closed
cston opened this issue Oct 17, 2018 · 2 comments
Assignees
Milestone

Comments

@cston
Copy link
Member

cston commented Oct 17, 2018

s.Length should result in a warning:

[System.Runtime.CompilerServices.NonNullTypes]
class C
{
    static void MayThrow()
    {
        throw null;
    }
    static int F()
    {
        string? s = "";
        try
        {
            s = null;
            MayThrow();
            s = "";
        }
        catch (System.Exception)
        {
        }
        return s.Length; // warning: possibly null
    }
}

Reported by @gafter.

@gafter gafter added this to the 16.0 milestone Oct 18, 2018
@gafter gafter self-assigned this Oct 23, 2018
@gafter
Copy link
Member

gafter commented Oct 24, 2018

Here is a much simpler example demonstrating the shortcomings of our analysis of try-finally:

[module: System.Runtime.CompilerServices.NonNullTypes(true)]

public static class Program {
    public static void Main() {
        string? s = "";
        try
        {
        }
        finally
        {
            s = null;
        }

        _ = s.Length; // warning missing
    }
}

@jaredpar jaredpar added the Bug label Oct 29, 2018
gafter added a commit to gafter/roslyn that referenced this issue Nov 9, 2018
gafter pushed a commit that referenced this issue Nov 13, 2018
@gafter
Copy link
Member

gafter commented Nov 13, 2018

Fixed in #31082

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

3 participants