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

IENumerable<T>.All() and List<T>.All() methods incorrectly assumed to be pure #184

Open
AngieEl opened this issue Dec 6, 2019 · 0 comments

Comments

@AngieEl
Copy link

AngieEl commented Dec 6, 2019

The sample program below generates a pair of incorrect warnings on the All() method calls. In both cases it is assuming the invocation is a pure method and the line of code is actually a no-op; but the collection is modified.

using System.Linq;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            BlameStackoverflowForThis();
        }
 
        static void BlameStackoverflowForThis()
        {
            //https://stackoverflow.com/a/6233655/85661
            //the abuse of All() seen below is from the above answer
 
            List<TestObject> testList = new List<TestObject> { new TestObject(), new TestObject(), new TestObject() };
            IEnumerable<TestObject> testEnum = testList;
 
            //ReSharper warning:  Return value of pure method is not used
            //but text is changed from null to "foo"
            testEnum.All(c => { c.text = "foo"; return true; });
 
            //ReSharper warning:  Return value of pure method is not used
            //but text is changed from "foo" to "bar"            
            //in this case testList.ForEach(c => { c.text = "bar"; }); is a 
            //clean way to do it,  but IEnumerable doesn't have a Foreach() method
            testList.All(c => { c.text = "bar"; return true; });
 
        }
    }
 
    public class TestObject
    {
        public  string text { get; set; }
    }
}
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

1 participant