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

NS4000 false positive in foreach loop when element used twice #106

Closed
stukselbax opened this issue Jun 5, 2019 · 1 comment · Fixed by #107
Closed

NS4000 false positive in foreach loop when element used twice #106

stukselbax opened this issue Jun 5, 2019 · 1 comment · Fixed by #107
Assignees
Labels
bug Something isn't working
Milestone

Comments

@stukselbax
Copy link

NSubstitute 4.2.0.0
NSubstitute.Analyzers.CSharp 1.0.10

When using POCO properties in foreach loop and trying to setup two mocks the NS4000 warning occur. When foreach loop is removed there are no warnings. Looks like wrong behavior.

using NSubstitute;
using NUnit.Framework;
using System;
using System.Collections.Generic;

namespace Tests
{
    public class Tests
    {
        [Test]
        public void Test1()
        {
            var list = new List<DateTime>(1) { DateTime.Now };
            var enumerator1 = Substitute.For<IEnumerator<DateTime>>();
            var enumerator2 = Substitute.For<IEnumerator<DateTime>>();
            foreach (var date in list)
            {
                // case 1: use date
                enumerator1.Current.Returns(date); // No warnings
                
                // case 2: use date properties
                //enumerator1.Current.Returns(new DateTime(date.Ticks)); // NS4000 here

                enumerator2.Current.Returns(new DateTime(date.Ticks)); // NS4000 here in both cases
            }
        }
    }
}
@tpodolak tpodolak added the bug Something isn't working label Jun 5, 2019
@tpodolak
Copy link
Member

tpodolak commented Jun 5, 2019

Hi @stukselbax thanks for reporting. Looks like a bug to me as well. I will try to take a look over the weekend

@tpodolak tpodolak added this to the 1.0.11 milestone Jun 5, 2019
@tpodolak tpodolak self-assigned this Jun 5, 2019
tpodolak added a commit that referenced this issue Jun 6, 2019
tpodolak added a commit that referenced this issue Jun 8, 2019
…-in-foreach

[GH-106] - preventing false positives for NS400 in foreach loops when element is used twice
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
2 participants