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

Moq1100 incorrectly firing on nullable parameters #172

Closed
marcovr opened this issue Aug 5, 2024 · 5 comments · Fixed by #187
Closed

Moq1100 incorrectly firing on nullable parameters #172

marcovr opened this issue Aug 5, 2024 · 5 comments · Fixed by #187
Assignees
Labels
analyzers Change that impacts an analyzer behavior bug
Milestone

Comments

@marcovr
Copy link

marcovr commented Aug 5, 2024

Analyzer Moq1100 is incorrectly firing in the following case:

public interface IFoo
{
    bool DoSomething(object? bar);
}

var mock = new Mock<IFoo>();
mock.Setup(m => m.DoSomething(It.IsAny<object?>())).Returns((object? bar) => true);

The analyzer reports:

Moq1100: Callback signature must match the signature of the mocked method

Note: this not only happens with object but also other types

@rjmurillo rjmurillo added bug analyzers Change that impacts an analyzer behavior labels Aug 26, 2024
@rjmurillo rjmurillo self-assigned this Aug 26, 2024
@rjmurillo rjmurillo added this to the v0.2.0 milestone Aug 26, 2024
rjmurillo added a commit that referenced this issue Aug 26, 2024
@rjmurillo rjmurillo linked a pull request Aug 26, 2024 that will close this issue
@rjmurillo
Copy link
Owner

@marcovr Thank you for reporting this issue. Would you be able to try a private to ensure this issue is corrected? Moq.Analyzers.0.2.0-g7dd22517f7.nupkg

@rjmurillo rjmurillo removed the triage label Aug 26, 2024
@marcovr
Copy link
Author

marcovr commented Aug 27, 2024

Thanks @rjmurillo!
I can confirm that the preview package resolves the issue 👍🏻.

However, I noticed that it introduced another (possibly unintended?) change in behaviour. Consider the following example:

var mock = new Mock<IFoo>();
mock.Setup(m => m.DoSomething(42)).Returns((long bar) => true);

public interface IFoo
{
    bool DoSomething(long bar);
}

In this case, the analyzer now reports Moq1100 unless I either perform a cast:

mock.Setup(m => m.DoSomething((long)42)).Returns((long bar) => true);

or change the signature of the return lambda:

mock.Setup(m => m.DoSomething(42)).Returns((int bar) => true);

I don't really like the variant with a cast as it causes other analyzers to report an unnecessary cast. And the other variant works fine, but seems kind of backwards to me. Because I now need a "wrong" signature to make the analyzer happy, which checks for correct signatures 😁

Edit: The second variant causes an Exception to be thrown:

Unhandled exception. System.ArgumentException: Object of type 'System.Int64' cannot be converted to type 'System.Int32'.

@rjmurillo
Copy link
Owner

rjmurillo commented Aug 27, 2024

Thanks @marcovr for your patience and the additional test cases; I've added them to the suite.

You're right, the updated type conversion detection method caused a regression. In the case there was an Int32 value being passed to Int64, which was failing the now stricter type check. I've updated the implementation since there is a conversion that can happen in a few scenarios:

var mock = new Mock<IFoo>();
mock.Setup(m => m.DoSomething(42)).Returns((long bar) => true);
mock.Setup(m => m.DoSomethingElse(42)).Returns((long var) => true);

public interface IFoo
{
    bool DoSomething(long bar);
    bool DoSomethingElse(object bar);
}

In the DoSomethingElse case, the integers can be boxed to an object, and can be done so on your behalf without an explicit cast. The updated implementation should now permit this without having to write extra code nor conflict with your other analyzers.

I have another private for you to try: Moq.Analyzers.0.2.0-gf735a830fe.nupkg

rjmurillo added a commit that referenced this issue Aug 27, 2024
@marcovr
Copy link
Author

marcovr commented Aug 28, 2024

Thanks for the quick fix @rjmurillo!
I can confirm that it now works as expected and I don't get any false positives anymore 🙂

@rjmurillo
Copy link
Owner

@marcovr Thank you for your help and patience. I've published a new version of the package with this bug fix: https://www.nuget.org/packages/Moq.Analyzers/0.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzers Change that impacts an analyzer behavior bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants