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

CS8604 warning when using MaybeNullWhen and return value is assigned #45587

Closed
benjamin-hodgson opened this issue Jul 1, 2020 · 1 comment
Closed
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue

Comments

@benjamin-hodgson
Copy link

benjamin-hodgson commented Jul 1, 2020

Version Used: 8.0

Steps to Reproduce:

When calling a method with an out parameter annotated with MaybeNullWhen, the flow analyser seems to fail when the return value is assigned to a variable before being scrutinised.

Compile this code with <Nullable>enable</Nullable>:

using System.Diagnostics.CodeAnalysis;

class Example
{
    void OK()
    {
        if (TryParse("foo", out var result))
        {
            DoSomething(result);
        }
    }
    void Warning()
    {
        var success = TryParse("foo", out var result);
        if (success)
        {
            DoSomething(result);
        }
    }
    bool TryParse(string s, [MaybeNullWhen(false)] out object result)
    {
        // dummy implementation
        result = default;
        return false;
    }
    void DoSomething(object item)
    {
    }
}

Expected Behavior:

No warning in either the OK or the Warning methods. (They self-evidently behave identically.)

Actual Behavior:

CS8604 warning ("Possible null reference argument for parameter 'item' in 'void Example.DoSomething(object item)'") inside the Warning method.

@benjamin-hodgson benjamin-hodgson changed the title CS8604 warning when control variable is assigned CS8604 warning when using MaybeNullWhen and return value is assigned Jul 1, 2020
@RikkiGibson RikkiGibson added Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue labels Jul 1, 2020
@RikkiGibson
Copy link
Contributor

Duplicate of #27011

@RikkiGibson RikkiGibson marked this as a duplicate of #27011 Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

2 participants