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

Incorrect NS3004 for derived types #57

Closed
samcragg opened this issue Dec 18, 2018 · 3 comments · Fixed by #63
Closed

Incorrect NS3004 for derived types #57

samcragg opened this issue Dec 18, 2018 · 3 comments · Fixed by #63
Assignees
Labels
bug Something isn't working
Milestone

Comments

@samcragg
Copy link

First of all, the analyzer is really useful - thanks for creating the project!

I think I'm incorrectly getting the Could not set value of type … to argument ... because the types are incompatible error when trying to assign a Dictionary to a IReadOnlyDictionary argument and was able to reproduce it with this code:

using NSubstitute;
using Xunit;

public class ExampleTest
{
    public interface IExampleInterface
    {
        bool Method(out object value);
    }

    [Fact]
    public void DictionaryTest()
    {
        IExampleInterface substitute = Substitute.For<IExampleInterface>();
        substitute.Method(out Arg.Any<object>())
            .Returns(ci =>
            {
                ci[0] = "string";
                return true;
            });

        bool result = substitute.Method(out object value);

        Assert.True(result);
        Assert.Equal("string", value);
    }
}

The line with ci[0] = "string"; triggers the analyser error, however, the test succeeds in passing (i.e. NSubstitute does correctly set the parameter).

I think the problem is we're checking if the type is equal instead of if the assignment will work in AbstractCallInfoAnalyzer.cs - maybe Equals should be replaced with a call to CanCast?

@tpodolak
Copy link
Member

@samcragg thanks for reporting. I will take a look at this once I am back from holidays. When it comes to Equals and CanCast usages I remember than Nsubstitute does different type checks depending on the context. Can you tell me which version of Nsubstitute you use?

@samcragg
Copy link
Author

Cool, thanks for that. I've tried it in both 4.0.0-rc1 and 3.1.0 (the latest stable) against version 1.0.3 of the analyser - both times the test passes but the analyser reports an error.

@tpodolak tpodolak self-assigned this Dec 22, 2018
@tpodolak tpodolak added the bug Something isn't working label Dec 22, 2018
tpodolak added a commit that referenced this issue Dec 24, 2018
dtchepak added a commit that referenced this issue Jan 3, 2019
…rived-types

Gh 57 incorrect ns3004 for derived types
@tpodolak
Copy link
Member

tpodolak commented Jan 3, 2019

Released in 1.0.4

@tpodolak tpodolak added this to the 1.0.4 milestone Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants