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

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

Open
jnm2 opened this issue Aug 30, 2021 · 1 comment
Open

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

jnm2 opened this issue Aug 30, 2021 · 1 comment

Comments

@jnm2
Copy link
Collaborator

jnm2 commented Aug 30, 2021

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));
    }
}
@jnm2
Copy link
Collaborator Author

jnm2 commented Aug 30, 2021

Local functions are also not understood. Replacing the Test property in the full repro above:

    public int Test
    {
        get
        {
            LocalFunction();
            return 42;

            void LocalFunction() => Console.WriteLine(SomeProperty);
        }
    }

Although, here's a case where the warning would be desirable:

    public string? Test
    {
        get
        {
            return LocalFunction();
            string? LocalFunction() => SomeProperty;
        }
    }

So maybe heuristically ignore lambdas (less likely for a lambda created in a getter to also be invoked by the getter to obtain the return value) and leave local functions as-is until there is a motivation to do something more?

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

No branches or pull requests

1 participant