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

Incorrect nullability inference in ternary operator with use of local variable #54973

Closed
kindermannhubert opened this issue Jul 20, 2021 · 1 comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@kindermannhubert
Copy link

Version Used: VS 17.0.0 Preview 2.0

Steps to Reproduce:

Methods M1/2/3 are equivalent, but in the last one warning is reported.

#nullable enable
using System.Diagnostics.CodeAnalysis;
public class C 
{
    static int M1() 
    {
        if (TryGet(out var obj))
        {
            return obj.GetHashCode(); //ok
        }
        else
        {
            return -1;
        }
    }
    
    static int M2() 
    {
        return TryGet(out var obj) ? obj.GetHashCode() : -1; //ok
    }
    
    static int M3() 
    {
        var ok = TryGet(out var obj);
        return ok ? obj.GetHashCode() : -1; //warning CS8602: Dereference of a possibly null reference.
    }
    
    static bool TryGet([NotNullWhen(true)]out object? obj)
    {
        obj = new();
        return true;
    }   
}
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 20, 2021
@RikkiGibson RikkiGibson added Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design labels Jul 20, 2021
@RikkiGibson
Copy link
Contributor

Thank you for taking the time to file this issue. This behavior is by-design. See #27011.

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-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants