dotnet/roslyn

IDE0200 (remove unnecessary lambda expression) can change semantics when capturing writable members

Open

#73,259 opened on Apr 29, 2024

View on GitHub
 (6 comments) (3 reactions) (0 assignees)C# (4,257 forks)batch import
Area-IDEBugFeature - IDE0200help wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

Version Used:

Steps to Reproduce:

public class Test {
  private List<int> _list = [1];

  public bool N() {
    var func = M();
    _list = [2];
    return func(2);
  }

  private Func<int, bool> M() {
    return x => _list.Contains(x); // IDE0200
  }
}

Diagnostic Id: IDE0200

Expected Behavior: The code fix should not be offered. It changes the semantics.

Actual Behavior: After applying the code fix, N returns false instead of true

I saw that this was fixed when capturing locals in #69101

Contributor guide