dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

ObserveableProperty and Nullable Can Generate Warnings

Open david-maw opened this issue 2 years ago • 1 comments

Describe the bug

An observable property in a nullable class like the one shown here can cause somewhat intractable warnings.

using CommunityToolkit.Mvvm.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace ObservableTest;

internal partial class TestViewModel:ObservableObject
{
    TestViewModel() 
    {
        Helper();
    }

    [MemberNotNull(nameof(testString))]
    private void Helper()
    {
        TestString = string.Empty;
    }

    [ObservableProperty]
    private string testString;
}

Regression

No response

Steps to reproduce

Create a default MAUI app and add a class like the one shown above.

Observe that the line:
    [MemberNotNull(nameof(testString))]

Gets a warning for referring to testString, not TestString, but referring to TestString generates a different warning (CS8618: Non-nullable field 'testString' must contain a non-null value when exiting constructor)

Expected behavior

Either don't warn on this use of testString or allow TestString to be used here.

Screenshots

No response

IDE and version

VS 2022 Preview

IDE version

17.10.0 Preview 1

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

No, just wanted to report this

david-maw avatar Feb 29 '24 05:02 david-maw

I just filed #939 as well which seems maybe related or at least an additional case of a more complex scenario involving Observable Properties and nullabilty analysis.

hawkerm avatar Sep 06 '24 08:09 hawkerm