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

NS2002 falsely triggered by optional ctor args #176

Open
lonix1 opened this issue Nov 17, 2021 · 5 comments
Open

NS2002 falsely triggered by optional ctor args #176

lonix1 opened this issue Nov 17, 2021 · 5 comments

Comments

@lonix1
Copy link

lonix1 commented Nov 17, 2021

NS2002 / Constructor parameters count mismatch

Here's a type:

public class Something {
  public Something(string? foo = "foo", int? bar = 10) {
    // etc.
  }
}

This triggers the analyzer:

Substitute.ForPartsOf<Foo>()

Those ctor args are optional.

@lonix1 lonix1 changed the title NS2004 falsely triggered by optional ctor args NS2002 falsely triggered by optional ctor args Nov 17, 2021
@lonix1
Copy link
Author

lonix1 commented Nov 17, 2021

Actually, I'm unsure of this.

Forgetting about the analyser for now (I just disabled it), I nonetheless get this exception;

Exception thrown: 'Castle.DynamicProxy.InvalidProxyConstructorArgumentsException' in Castle.Core.dll

Weird, those args are optional, I'd have thought this would work?

@tpodolak
Copy link
Member

tpodolak commented Nov 17, 2021

Hi @lonix1. As you've seen Castle.Core throws exception even though parameters are options, that is why NSubstitute.Analyzers reports warning. Perhaps we should change warning message to sth more self explanatory. @dtchepak any thoughts on that?

Weird, those args are optional, I'd have thought this would work?

I assume this is limitation of Catle.Core rather than issue with NSubstitute itself, however I dont know that much about internals of NSubstitute. @dtchepak do you maybe know if this is this known problem of Castle.Core?

@lonix1
Copy link
Author

lonix1 commented Nov 17, 2021

Your idea of a more descriptive warning is good. It will let the user know that it is "normal" and there is nothing he can do about it.

@dtchepak
Copy link
Member

IIRC optional arguments are injected at compile-time? In that case Castle.Core will have no way of knowing what to pass to the constructor. (see also this thread).

Does Roslyn provide access to optional param information? If so maybe that should be a new error explaining optional parameters must be passed explicitly (possibly quick fix to pass the defaults?).

@tpodolak
Copy link
Member

tpodolak commented May 4, 2022

Does Roslyn provide access to optional param information? If so maybe that should be a new error explaining optional parameters must be passed explicitly (possibly quick fix to pass the defaults?).

Yes, we can extract default values with Roslyn
image

I will try to figure out how to use it in ctor matching logic

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

3 participants