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

Report WRN_NullabilityMismatchInAssignment for a loop variable in ```foreach``` based on the type of the Current property and conversions involved #29971

Closed
AlekseyTs opened this Issue Sep 17, 2018 · 1 comment

Comments

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Sep 17, 2018

        [Fact]
        public void ForEach_10()
        {
            var source =
@"#pragma warning disable 0649
#pragma warning disable 8618
class A<T>
{
    internal T F;
}
class B : A<object> { }
class C
{
    static void F(A<object?>[] c)
    {
        foreach (var a1 in c)
            a1.F.ToString();
        foreach (A<object?> a2 in c)
            a2.F.ToString();
        foreach (A<object> a3 in c)
            a3.F.ToString();
        foreach (B b1 in c)
            b1.ToString();
    }
}";
            // PROTOTYPE(NullableReferenceTypes): Should report WRN_NullabilityMismatchInAssignment
            // for `A<object> a3 in c` and `B b1 in c`.
            var comp = CreateCompilation(new[] { source, NonNullTypesTrue, NonNullTypesAttributesDefinition });
            comp.VerifyDiagnostics(
                // (13,13): warning CS8602: Possible dereference of a null reference.
                //             a1.F.ToString();
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a1.F").WithLocation(13, 13),
                // (15,13): warning CS8602: Possible dereference of a null reference.
                //             a2.F.ToString();
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a2.F").WithLocation(15, 13));
        }
@AlekseyTs

This comment has been minimized.

Copy link
Contributor Author

AlekseyTs commented Sep 17, 2018

The warning is reported for this scenario, but the location isn't great.


        [Fact]
        public void ForEach_11()
        {
            var source =
@"using System.Collections.Generic;
class A
{
    public static implicit operator B?(A a) => null;
}
class B
{
}
class C
{
    static void F(IEnumerable<A> e)
    {
        foreach (var x in e)
            x.ToString();
        foreach (B y in e)
            y.ToString();
        foreach (B? z in e)
        {
            z.ToString();
            if (z != null) z.ToString();
        }
    }
}";
            var comp = CreateCompilation(new[] { source, NonNullTypesTrue, NonNullTypesAttributesDefinition });
            // PROTOTYPE(NullableReferenceTypes): Location of WRN_NullabilityMismatchInAssignment should be `y` rather than `B`.
            // PROTOTYPE(NullableReferenceTypes): Reword WRN_NullabilityMismatchInAssignment since there is not an explicit assignment.
            comp.VerifyDiagnostics(
                // (15,18): warning CS8601: Possible null reference assignment.
                //         foreach (B y in e)
                Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "B").WithLocation(15, 18),
                // (16,13): warning CS8602: Possible dereference of a null reference.
                //             y.ToString();
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(16, 13),
                // (19,13): warning CS8602: Possible dereference of a null reference.
                //             z.ToString();
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(19, 13));
        }

@AlekseyTs AlekseyTs changed the title Report WRN_NullabilityMismatchInAssignment for a loop variable in ```foreach``` based on the type of the Current property Report WRN_NullabilityMismatchInAssignment for a loop variable in ```foreach``` based on the type of the Current property and conversions involved Sep 17, 2018

@jaredpar jaredpar added the Bug label Sep 18, 2018

@jcouv jcouv added this to the 16.0 milestone Sep 20, 2018

@jaredpar jaredpar added this to Misc in Nullable Board Jan 28, 2019

@gafter gafter modified the milestones: 16.0, 16.1 Feb 26, 2019

@jaredpar jaredpar moved this from Misc to foreach in Nullable Board Apr 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.