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

Support null checking with property in Nullable Reference Type analysis #36149

Closed
MihaZupan opened this issue Jun 4, 2019 · 4 comments
Closed

Comments

@MihaZupan
Copy link
Member

class Foo
{
    public TaskCompletionSource<bool>? _completionSource;

    public bool IsStopped => !(_completionSource is null);
    // or
    public bool IsStopped => _completionSource != null;

    public void Bar()
    {
        if (IsStopped)
        {
            // Possible dereference
            _completionSource.SetResult(true);
        }

        if (!(_completionSource is null))
        {
            // This is fine
            _completionSource.SetResult(true);
        }
    }
}

I know I can stick a dammit operator in there, was just wondering if there are plans to support such null checks in analysis.

@jcouv
Copy link
Member

jcouv commented Jun 5, 2019

There is no plan to support such an analysis at this point.
This issue is similar to #34800

@MihaZupan
Copy link
Member Author

Okay. Thanks for the response.

@jcouv
Copy link
Member

jcouv commented Jul 1, 2020

This will be supported in C# 9 after all, but requires an attribute to be placed saying that the return value of IsStopped affects our knowledge of the state of _completionSource.

class Foo
{
    public TaskCompletionSource<bool>? _completionSource;

    [MemberNotNullWhen(true, "_completionSource")]
    public bool IsStopped => !(_completionSource is null);
    
    public void Bar()
    {
        if (IsStopped)
        {
            // Possible dereference
            _completionSource.SetResult(true);
        }

        if (!(_completionSource is null))
        {
            // This is fine
            _completionSource.SetResult(true);
        }
    }
}

namespace System.Diagnostics.CodeAnalysis
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
    public sealed class MemberNotNullAttribute : Attribute
    {
        public MemberNotNullAttribute(params string[] members) { }
        public MemberNotNullAttribute(string member) { }
    }
}

namespace System.Diagnostics.CodeAnalysis
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
    public sealed class MemberNotNullWhenAttribute : Attribute
    {
        public MemberNotNullWhenAttribute(bool when, params string[] members) { }
        public MemberNotNullWhenAttribute(bool when, string member) { }
    }
}

sharplab

@MihaZupan
Copy link
Member Author

Thanks! I didn't realize the attribute will be supported on properties as well.

Thanks for reaching out even on such an old and closed issue

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

2 participants