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

Warning reported for assignment to out parameter with [MaybeNull] or [MaybeNullWhen(...)] #39922

Closed
cston opened this issue Nov 21, 2019 · 1 comment · Fixed by #40789
Closed
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types
Milestone

Comments

@cston
Copy link
Member

cston commented Nov 21, 2019

No warning should be reported for the assignments:

#nullable enable

using System.Diagnostics.CodeAnalysis;

class Program
{
    static void GetValue<T>([AllowNull]T x, [MaybeNull]out T y)
    {
        y = x; // warning CS8601: Possible null reference assignment
    }

    static bool TryGetValue<T>([AllowNull]T x, [MaybeNullWhen(false)]out T y)
    {
        y = x; // warning CS8601: Possible null reference assignment
        return y != null;
    }
}

For [MaybeNullWhen(...)], perhaps the compiler should allow assignment without warning to the variable regardless of the return value.

cc @stephentoub

@cston cston changed the title Warning reported for assignment to out parameter with [MaybeNullWhen(...)] Warning reported for assignment to out parameter with [MaybeNull] or [MaybeNullWhen(...)] Nov 21, 2019
@cston cston added this to the 16.5 milestone Nov 21, 2019
@cston cston self-assigned this Nov 21, 2019
@jcouv
Copy link
Member

jcouv commented Dec 11, 2019

Relates to #36039 (Should nullability attributes affect method bodies and OHI?)

@jaredpar jaredpar added the Bug label Dec 14, 2019
@jcouv jcouv self-assigned this Jan 8, 2020
@jcouv jcouv added the 4 - In Review A fix for the issue is submitted for review. label Jan 8, 2020
@cston cston removed their assignment Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants