controlflow / resharper-heapview

ReSharper Heap Allocations Viewer plugin
MIT License
303 stars 28 forks source link

Potential incorrect warning when storing a delegate value of a property in a local variable #113

Closed EnCey closed 1 year ago

EnCey commented 1 year ago

Given a property of a delegate type (Func, Predicate etc.), I get a warning when storing the value of that property in a local variable. To my knowledge, and after checking the IL code (which I'm not that familiar with), I don't think an allocation is happening.

As I understand things, a reference to the delegate is returned and stored in the variable, no allocation should happen. Also I do NOT get a warning when directly invoking the property, only when first storing it in a variable, which also seems odd.

image
public class Holder
{
  public Predicate<int> Checker { get; init; }
}

public class Tester
{
  public bool Test()
  {
    var holder = new Holder {
      Checker = x => x > 1
    };
    return Use(holder, 5);
  }

  public bool Use(Holder h, int arg)
  {
    var local = h.Checker;
    return local(arg);
  }
}
controlflow commented 1 year ago

Should be fixed in 103fe4d133944c22e26b2abad536cbd24df0df50 I cannot reproduce this in latest builds