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

Possibly unexpected warning when NotNull attribute is involved #29865

Open
AlekseyTs opened this issue Sep 13, 2018 · 0 comments
Open

Possibly unexpected warning when NotNull attribute is involved #29865

AlekseyTs opened this issue Sep 13, 2018 · 0 comments

Comments

@AlekseyTs
Copy link
Contributor

        [Fact]
        public void EnsuresNotNull_BackEffect()
        {
            CSharpCompilation c = CreateCompilation(new[] { @"
using System.Runtime.CompilerServices;
public class C
{
    void M(string? s1, string? s2)
    {
        ThrowIfNull(s2 = s1, s1);
        s2.ToString(); // warn
    }
    public static void ThrowIfNull(string? x1, [EnsuresNotNull] string? x2) => throw null;
}
", EnsuresNotNullAttributeDefinition, NonNullTypesTrue, NonNullTypesAttributesDefinition });

            // PROTOTYPE(NullableReferenceTypes): Should we be able to trace that s2 was assigned a non-null value?
            c.VerifyDiagnostics(
                // (8,9): warning CS8602: Possible dereference of a null reference.
                //         s2.ToString(); // warn
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s2").WithLocation(8, 9)
                );
        }


        [Fact]
        public void EnsuresNotNull_ForwardEffect()
        {
            CSharpCompilation c = CreateCompilation(new[] { @"
using System.Runtime.CompilerServices;
public class C
{
    void M(string? s1, string? s2)
    {
        ThrowIfNull(s1, s2 = s1);
        s2.ToString(); // ok
    }
    public static void ThrowIfNull([EnsuresNotNull] string? x1, string? x2) => throw null;
}
", EnsuresNotNullAttributeDefinition, NonNullTypesTrue, NonNullTypesAttributesDefinition });

            c.VerifyDiagnostics();
        }
@jcouv jcouv added this to the 16.0 milestone Sep 17, 2018
@jaredpar jaredpar added this to Attribute in Nullable Board Jan 28, 2019
@gafter gafter modified the milestones: 16.0, 16.1 Feb 26, 2019
@jcouv jcouv modified the milestones: 16.1, 16.2 Apr 23, 2019
@jcouv jcouv modified the milestones: 16.2, 16.3 Jun 25, 2019
@jcouv jcouv modified the milestones: 16.3, Compiler.Next Jul 8, 2019
@jcouv jcouv changed the title Possibly unexpected warning when EnsuresNotNull attribute is involved Possibly unexpected warning when NotNull attribute is involved Jul 10, 2019
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants