CA2213 false positive. Local declared by not-null pattern not considered as derived from disposable field.
- Dominant language
- C#
- Stars
- 3.2k
- Forks
- 1.3k
- PR merge metrics
- PR metrics pending
Description
### Describe the bug
When new local variable declared by not-null pattern on disposable field, then that local is not considered derived from that disposable field. So disposing of that local did not considered as disposing field. In following example diagnostic raised for `d1` and `d4`, although it should only be raised for `d1`.
### To Reproduce
Class.cs:
```csharp
using System;
namespace Test;
public sealed class Class : IDisposable {
private IDisposable? d1;
private IDisposable? d2;
private IDisposable? d3;
private IDisposable? d4;
public Class() {
d1 = string.Empty.GetEnumerator();
d2 = string.Empty.GetEnumerator();
d3 = string.Empty.GetEnumerator();
d4 = string.Empty.GetEnumerator();
}
public void Dispose() {
d2?.Dispose();
var t3 = d3;
if (t3 is { }) {
t3.Dispose();
}
if (d4 is { } t4) {
t4.Dispose();
}
}
}
```
Test.csproj:
```xml
True
net10.0
enable
```
.editorconfig:
```
root = true
[*.cs]
dotnet_diagnostic.CA2213.severity = warning
```
### Exceptions (if any)
warning CA2213: 'Class' contains field 'd1' that is of IDisposable type 'IDisposable?', but it is never disposed. Change the Dispose method on 'Class' to call Close or Dispose on this field.
warning CA2213: 'Class' contains field 'd4' that is of IDisposable type 'IDisposable?', but it is never disposed. Change the Dispose method on 'Class' to call Close or Dispose on this field.
### Further technical details
details of dotnet --info
```
.NET SDK:
Version: 10.0.201
Commit: 4d3023de60
Workload version: 10.0.200-manifests.0793c108
MSBuild version: 18.3.0-release-26153-122+4d3023de6
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.201\
.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 10.0.5
Architecture: x64
Commit: a612c2a105
.NET SDKs installed:
10.0.201 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 10.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 10.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 10.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.