Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUnit1032 (missing Dispose), if dispose is wrapped in "(… as IDisposable)?.Dispose()" #743

Closed
DrPepperBianco opened this issue May 27, 2024 · 2 comments · Fixed by #744
Assignees
Milestone

Comments

@DrPepperBianco
Copy link

OK, basically I had a test class. In the Test-Class was a member "IServiceProvider _serviceProvider". In the [TearDown]-method the service provider was disposed with the following code:

(_serviceProvider as IDisposable)?.Dispose();
_serviceProvider = null;

When compiled I got the above mentioned error: Error NUnit1032: The field _serviceProvider should be Disposed in a method annotated with [TearDownAttribute] (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit1032.md)

  1. IServiceProvider doesn’t inherit the IDisposable interface, hence the as operator in the TearDown method.
  2. I explicitly call Dispose() in the TearDown method.

So why do I get this error message!? This is probably a bug.

So for reference, this is the NuGet Config:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@OsirisTerje
Copy link
Member

Looks like this belongs to the analyzer. Moving it there.

@OsirisTerje OsirisTerje transferred this issue from nunit/nunit May 27, 2024
@manfred-brands manfred-brands self-assigned this May 28, 2024
@manfred-brands
Copy link
Member

The analyzer already recognized:

if (field is IDisposable disposable)
    dispoable.Dispose()

and

((IDisposable)field.Dispose())

I did create a PR to add support for (field as IDisposable)?.Dispose()

@mikkelbu mikkelbu added this to the Release 4.3 milestone Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants