Re: What does JetBrains.Annotations.LinqTunnelAttribute do ?

The original discussion got deleted somehow so I'm continuing it here.

LinqTunnelAttribute" affects analisys of lambda/anonymous function passed to the function as argument. So, if the method returning IEnumerable<T> does not have function parameter then there is no sense to mark the method with LinqTunnelAttribute.
This attribute is used to understand if the lambda/anonymous argument is executed instantly (during the method execution) or can be executed later. Here is an example ("Where" method is marked with LinqTunnelAttribute):

  public void Test(int[] ints)
  {
    var min = 0;
    var max = 100;
    var en1 = ints.Where(i => i > min); // "Access to modified closure" warning on "min" because ReSharper is not sure that the lambda is executed instantly
    var en2 = ints.Where(i => i > min).Where(i => i < max).ToList(); // No warning because ReSharper know that the lambdas is executed instantly
    min = 50;
    max = 200;
  }


And, as you pointed, not every method returning IEnumeable<T> is deferred, sometimes developer just want to return non-modifiable collection, so we cannot automatically apply this attribute to every method returning IEnumerable<T>


Hi Ivan,

Thanks for the explanation. However, using your code, switching Where() with MyWehere() defined as:

        public static IEnumerable<T> MyWhere<T>(this IEnumerable<T> collection, Func<T,bool> predicate)
        {
            return collection.Where(predicate);
        }

I get the same result ("Access to modified closure"), even though MyWhere is not annotated with LinqTunnelAttribute. So if that's the default assumption R# makes I'm not sure what is there to gain with this attribute.

Thanks again

Please sign in to leave a comment.