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

CA2213:Disposable fields should be disposed and support for DisposeCoreAsync #6976

Merged
merged 6 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,95 @@ End Function
}.RunAsync();
}

[Fact]
public async Task DisposeCoreAsync_NoDiagnosticAsync()
{
await new VerifyCS.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
using System;
using System.Threading.Tasks;

class A : IAsyncDisposable
{
public ValueTask DisposeAsync()
{
return default(ValueTask);
}
}

// No diagnostic for DisposeCoreAsync.
class B : IAsyncDisposable
{
private readonly object disposedValueLock = new object();
private bool disposedValue;
private readonly A a;

public B()
{
a = new A();
}

protected virtual async ValueTask DisposeCoreAsync()
{
lock (disposedValueLock)
{
if (disposedValue)
{
return;
}

disposedValue = true;
}

await a.DisposeAsync().ConfigureAwait(false);
}

public async ValueTask DisposeAsync()
{
await DisposeCoreAsync().ConfigureAwait(false);
GC.SuppressFinalize(this);
}
}

// No diagnostic for DisposeAsyncCore.
class C : IAsyncDisposable
{
private readonly object disposedValueLock = new object();
private bool disposedValue;
private readonly A a;

public C()
{
a = new A();
}

protected virtual async ValueTask DisposeAsyncCore()
{
lock (disposedValueLock)
{
if (disposedValue)
{
return;
}

disposedValue = true;
}

await a.DisposeAsync().ConfigureAwait(false);
}

public async ValueTask DisposeAsync()
{
await DisposeAsyncCore().ConfigureAwait(false);
GC.SuppressFinalize(this);
}
}
"
}.RunAsync();
}

[Fact, WorkItem(5099, "https://github.com/dotnet/roslyn-analyzers/issues/5099")]
public async Task OwnDisposableButDoesNotOverrideDisposableMember_Dispose()
{
Expand Down
20 changes: 18 additions & 2 deletions src/Utilities/Compiler/Extensions/IMethodSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,30 @@ private static bool HasDisposeCloseAsyncMethodSignature(this IMethodSymbol metho
}

/// <summary>
/// Checks if the given method has the signature "override Task DisposeCoreAsync(bool)".
/// Checks if the given method has the signature "override Task DisposeCoreAsync(bool)" or "override Task DisposeAsyncCore(bool)".
/// </summary>
private static bool HasOverriddenDisposeCoreAsyncMethodSignature(this IMethodSymbol method, [NotNullWhen(returnValue: true)] INamedTypeSymbol? task)
{
return method.Name == "DisposeCoreAsync" &&
return (method.Name == "DisposeAsyncCore" || method.Name == "DisposeCoreAsync") &&
method.MethodKind == MethodKind.Ordinary &&
method.IsOverride &&
SymbolEqualityComparer.Default.Equals(method.ReturnType, task) &&
method.Parameters.Length == 1 &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Boolean;
}

/// <summary>
/// Checks if the given method has the signature "virtual ValueTask DisposeCoreAsync()" or "virtual ValueTask DisposeAsyncCore()".
/// </summary>
private static bool HasVirtualDisposeCoreAsyncMethodSignature(this IMethodSymbol method, [NotNullWhen(returnValue: true)] INamedTypeSymbol? valueTask)
{
return (method.Name == "DisposeAsyncCore" || method.Name == "DisposeCoreAsync") &&
method.MethodKind == MethodKind.Ordinary &&
method.IsVirtual &&
SymbolEqualityComparer.Default.Equals(method.ReturnType, valueTask) &&
method.Parameters.Length == 0;
}

/// <summary>
/// Gets the <see cref="DisposeMethodKind"/> for the given method.
/// </summary>
Expand Down Expand Up @@ -351,6 +363,10 @@ public static DisposeMethodKind GetDisposeMethodKind(this IMethodSymbol method,
{
return DisposeMethodKind.DisposeCoreAsync;
}
else if (method.HasVirtualDisposeCoreAsyncMethodSignature(valueTask))
{
return DisposeMethodKind.DisposeCoreAsync;
}
else if (method.HasDisposeCloseMethodSignature())
{
return DisposeMethodKind.Close;
Expand Down