Skip to content

Commit

Permalink
Merge pull request #6147 from myblindy/patch-01
Browse files Browse the repository at this point in the history
Fixes #6075
  • Loading branch information
Evangelink authored Nov 9, 2022
2 parents c2bc164 + 790bef2 commit 31373ce
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,186 @@ End Function
}.RunAsync();
}

[Fact, WorkItem(6075, "https://github.com/dotnet/roslyn-analyzers/issues/6075")]
public async Task AsyncDisposableDisposedInExplicitAsyncDisposable_Disposed_NoDiagnosticAsync()
{
await new VerifyCS.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
class FileStream2 : IAsyncDisposable
{
public ValueTask DisposeAsync() => default;
}
public sealed class Test : IAsyncDisposable, IDisposable
{
private readonly HttpClient client;
private readonly FileStream2 stream;
public Test()
{
client = new HttpClient();
stream = new FileStream2();
}
public void Dispose()
{
client.Dispose();
}
async ValueTask IAsyncDisposable.DisposeAsync()
{
await stream.DisposeAsync();
}
}
"
}.RunAsync();

await new VerifyVB.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
Imports System
Imports System.IO
Imports System.Net.Http
Imports System.Threading.Tasks
class FileStream2
implements IAsyncDisposable
public function DisposeAsync() as ValueTask implements IAsyncDisposable.DisposeAsync
return nothing
end function
end class
public class Test
implements IAsyncDisposable, IDisposable
private readonly client as HttpClient
private readonly stream as FileStream2
public sub new()
client = new HttpClient
stream = new FileStream2
end sub
public sub Dispose() implements IDisposable.Dispose
client.Dispose()
end sub
function DisposeAsync() as ValueTask implements IAsyncDisposable.DisposeAsync
return stream.DisposeAsync()
end function
end class
"
}.RunAsync();

await new VerifyVB.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
Imports System
Imports System.IO
Imports System.Net.Http
Imports System.Threading.Tasks
class FileStream2
implements IAsyncDisposable
public function DisposeAsync() as ValueTask implements IAsyncDisposable.DisposeAsync
return nothing
end function
end class
public class Test
implements IAsyncDisposable, IDisposable
private readonly client as HttpClient
private readonly stream as FileStream2
public sub new()
client = new HttpClient
stream = new FileStream2
end sub
public sub Dispose() implements IDisposable.Dispose
client.Dispose()
end sub
rem arbitrary implementation name
function DisposeOtherAsync() as ValueTask implements IAsyncDisposable.DisposeAsync
return stream.DisposeAsync()
end function
end class
"
}.RunAsync();
}

[Fact, WorkItem(6075, "https://github.com/dotnet/roslyn-analyzers/issues/6075")]
public async Task DisposableDisposedInExplicitDisposable_Disposed_NoDiagnosticAsync()
{
await new VerifyCS.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
public sealed class Test : IDisposable
{
private readonly HttpClient client;
private readonly FileStream stream;
public Test()
{
client = new HttpClient();
stream = new FileStream(""C://some-path"", FileMode.CreateNew);
}
void IDisposable.Dispose()
{
client.Dispose();
stream.Dispose();
}
}
"
}.RunAsync();

await new VerifyVB.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithAsyncInterfaces,
TestCode = @"
Imports System
Imports System.IO
Imports System.Net.Http
Imports System.Threading.Tasks
public class Test
implements IDisposable
private readonly client as HttpClient
private readonly stream as FileStream
public sub new()
client = new HttpClient
stream = new FileStream(""C://some-path"", FileMode.CreateNew)
end sub
public sub Dispose() implements IDisposable.Dispose
client.Dispose()
stream.Dispose()
end sub
end class
"
}.RunAsync();
}

[Fact]
public async Task DisposableAllocation_AssignedThroughLocal_Disposed_NoDiagnosticAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public static DisposeMethodKind GetDisposeMethodKind(
{
return DisposeMethodKind.DisposeBool;
}
else if (method.HasDisposeAsyncMethodSignature(task, valueTask))
else if (method.IsAsyncDisposeImplementation(iAsyncDisposable, valueTask) || method.HasDisposeAsyncMethodSignature(task, valueTask))
{
return DisposeMethodKind.DisposeAsync;
}
Expand Down

0 comments on commit 31373ce

Please sign in to comment.