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 3 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,177 @@ 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();

// How to fix: "error BC36945: The 'Async' modifier can only be used on Subs, or on Functions that return Task or Task(Of T)."?
MartyIX marked this conversation as resolved.
Show resolved Hide resolved
/*

await new VerifyVB.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
Imports System
Imports System.Threading.Tasks

Class A
Implements IAsyncDisposable

Public Function DisposeAsync() As ValueTask Implements IAsyncDisposable.DisposeAsync
Return Nothing
End Function
End Class

' No diagnostic for DisposeCoreAsync.
Class B
Implements IAsyncDisposable

Private ReadOnly disposedValueLock As Object = New Object()
Private disposedValue As Boolean
Private ReadOnly a As A

Public Sub New()
a = New A()
End Sub

Protected Overridable Async Function DisposeCoreAsync() As ValueTask
SyncLock disposedValueLock

If disposedValue Then
Return
End If

disposedValue = True
End SyncLock

Await a.DisposeAsync().ConfigureAwait(False)
End Function

Public Async Function DisposeAsync() As ValueTask Implements IAsyncDisposable.DisposeAsync
Await DisposeCoreAsync.ConfigureAwait(false)
GC.SuppressFinalize(Me)
End Function
End Class

' No diagnostic for DisposeAsyncCore.
Class C
Implements IAsyncDisposable

Private ReadOnly disposedValueLock As Object = New Object()
Private disposedValue As Boolean
Private ReadOnly a As A

Public Sub New()
a = New A()
End Sub

Protected Overridable Async Function DisposeAsyncCore() As ValueTask
SyncLock disposedValueLock

If disposedValue Then
Return
End If

disposedValue = True
End SyncLock

Await a.DisposeAsync().ConfigureAwait(False)
End Function

Public Function DisposeAsync() As ValueTask Implements IAsyncDisposable.DisposeAsync
Await DisposeAsyncCore.ConfigureAwait(false)
GC.SuppressFinalize(Me)
End Function
End Class"
}.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 == "DisposeCoreAsync" || method.Name == "DisposeAsyncCore") &&
MartyIX marked this conversation as resolved.
Show resolved Hide resolved
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 == "DisposeCoreAsync" || method.Name == "DisposeAsyncCore") &&
MartyIX marked this conversation as resolved.
Show resolved Hide resolved
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