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

Make NS1004 distinguish between non-virtual members and extension methods #206

Open
petterh opened this issue Jun 20, 2023 · 1 comment
Open

Comments

@petterh
Copy link

petterh commented Jun 20, 2023

Assume an interface and an extension method for that interface:

public interface IHolder
{
    string Value { get; set; }
}

public static class IHolderExtensions
{
    public static void SetValue(this IHolder holder, string value) => holder.Value = value;
}

The following triggers an NS1004:

string? value = null;
var holder = Substitute.For<IHolder>();
holder.SetValue(Arg.Do<string>(v => value = v)); // <==
holder.SetValue("hello");
value.Should().Be("hello");

With the provided definition of SetValue, I can ignore the NS1004: Things work because the call to the extension method basically boils down to this:

holder.Value = Arg.Do<string>(v => value = v);

If I change SetValue to the following, things blow up, of course, so the analyzer is right to flag the issue:

public static void SetValue(this IHolder holder, string value) => throw new InvalidOperationException("Go away");

Proposal

If possible, I'd like to see the message

Argument matcher used with a non-virtual member of a class.

changed to

Argument matcher used with an extension method.

Might require a new rule.

@tpodolak
Copy link
Member

Hi @petterh
We are planing to have separate rule for substitution of extension methods, so having separate rule for argument matchers makes sense. Thanks for pointing that out

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

No branches or pull requests

2 participants