CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

Add an analyzer for [field: ObservableProperty] uses #734

Closed Sergio0694 closed 1 year ago

Sergio0694 commented 1 year ago

The [ObservableProperty] attribute is only valid on field declarations. Trying to use it on a backing field for a property will not cause errors, as the source generator will simply skip that syntax node, but it will produce no diagnostics to tell the users what's wrong. There should be a new diagnostic analyzer to produce the appropriate diagnostic in these cases.

Example

Consider this snippet:

public partial class MyViewModel : ObservableObject
{
    [field: ObservableProperty]
    public string? Name { get; private set; }
}

This should emit a diagnostic on that attribute use.