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 about null value while value is set as not null #65406

Closed
NN--- opened this issue Nov 14, 2022 · 4 comments · Fixed by #65987
Closed

Warning about null value while value is set as not null #65406

NN--- opened this issue Nov 14, 2022 · 4 comments · Fixed by #65987
Assignees
Milestone

Comments

@NN---
Copy link

NN--- commented Nov 14, 2022

Version Used:
Visual Studio 17.4.0

Steps to Reproduce:

using System;
using System.Diagnostics.CodeAnalysis;

#nullable enable

public class A
{
    private readonly string _s;

    public required string S
    {
        get
        {
            return _s;
        }
        [MemberNotNull(nameof(_s))]
        init
        {
            if (_s is null) throw new ArgumentException("Must not be null or empty");
            _s = value;
        }
    }
}

public class C
{
    public static void Main()
    {
        new A { S = "" };
    }
}

Diagnostic Id:

warning CS8618: Non-nullable field '_s' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Expected Behavior:
No warning.
The field _s is not null after init block which specifies it using MemberNotNullAttribute.

Actual Behavior:
Warning about making _s to be nullable.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 14, 2022
@NN---
Copy link
Author

NN--- commented Nov 14, 2022

Probably the same as #43587

@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 15, 2022
@jaredpar jaredpar added this to the 17.5 milestone Nov 15, 2022
@jaredpar
Copy link
Member

@333fred to confirm dupe

@333fred
Copy link
Member

333fred commented Nov 15, 2022

Unrelated. I'm not sure whether we really support this scenario, however. We generally don't process property bodies when considering nullability of fields, but we might make an exception for MemberNotNull of required properties. We'll need to run it by LDM.

@333fred
Copy link
Member

333fred commented Nov 28, 2022

dotnet/csharplang#6754 will be discussed by LDM on Wednesday. There are some subtleties to this request: chained constructors, in particular, are more difficult than they appear on the surface, as we would need to use MemberNotNull attributes to infer a starting state for the mentioned members, not just an ending state.

333fred added a commit that referenced this issue Dec 16, 2022
* Support MemberNotNull on required members in nullable analysis

Fixes dotnet/csharplang#6754, #65406.

* Handle and add test for IDE-discovered scenario.

* PR feedback.
333fred added a commit to 333fred/roslyn that referenced this issue Dec 21, 2022
…t#65987)

* Support MemberNotNull on required members in nullable analysis

Fixes dotnet/csharplang#6754, dotnet#65406.

* Handle and add test for IDE-discovered scenario.

* PR feedback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants