dotnet / dotnet/roslyn

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

Open
#73,259 6 comments 3 reactions 0 assignees View on GitHub
Area-IDE Bug Feature - IDE0200 help wanted
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**:

**Steps to Reproduce**:

```csharp
public class Test {
private List _list = [1];

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

private Func 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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.