Skip to content

INPC003 warns incorrectly when a getter contains a lambda expression #221

Open
@jnm2

Description

This hits a common pattern (also when not using ??=):

private ICommand someCommand;
private ICommand SomeCommand => someCommand ??= new RelayCommand(_ => DoSomething(OtherProperty));

Full repro (using v3.2.3):

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

class C : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    private string? someProperty;
    public string? SomeProperty
    {
        get => this.someProperty;
        set
        {
            if (value == this.someProperty)
            {
                return;
            }

            // ⚠ INPC003 Notify that property 'Test' changes.
            this.someProperty = value;
            OnPropertyChanged();
        }
    }

    public Action Test
    {
        get
        {
            return () => Console.WriteLine(SomeProperty);
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions