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

Directions for supporting IAsyncDisposable #576

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -24,6 +24,22 @@ public void Dispose()
}
""";

private const string AsyncDisposable = """
namespace N
{
using System;
using System.Threading.Tasks;

public class AsyncDisposable : IAsyncDisposable
{
public ValueTask DisposeAsync()
{
return ValueTask.CompletedTask;
}
}
}
""";

[TestCase("new Disposable()")]
[TestCase("new Disposable() as object")]
[TestCase("(object) new Disposable()")]
Expand Down Expand Up @@ -164,6 +180,25 @@ public static long M()
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, Disposable, code);
}

[Test]
public static void NewAsyncDisposable()
{
var code = """
namespace N
{
public static class C
{
public static long M()
{
↓var disposable = new AsyncDisposable();
return 1;
}
}
}
""";
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, AsyncDisposable, code);
}

[Test]
public static void MethodCreatingDisposable1()
{
Expand Down
37 changes: 37 additions & 0 deletions IDisposableAnalyzers.Test/IDISP001DisposeCreatedTests/Valid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public void Dispose()
}
}";

private const string AsyncDisposable = """
namespace N
{
using System;
using System.Threading.Tasks;

public class AsyncDisposable : IAsyncDisposable
{
public ValueTask DisposeAsync()
{
return ValueTask.CompletedTask;
}
}
}
""";

[TestCase("1")]
[TestCase("new string(' ', 1)")]
[TestCase("typeof(IDisposable)")]
Expand Down Expand Up @@ -86,6 +102,27 @@ public void M()
RoslynAssert.Valid(Analyzer, Disposable, code);
}

[Test]
public static void WhenDisposingVariableAsync()
{
var code = @"
namespace N
{
using System.Threading.Tasks;

public class C
{
public async Task M()
{
var item = new AsyncDisposable();
await item.DisposeAsync();
}
}
}";

RoslynAssert.Valid(Analyzer, AsyncDisposable, code);
}

[Test]
public static void UsingFileStream()
{
Expand Down
21 changes: 11 additions & 10 deletions IDisposableAnalyzers/Helpers/Disposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ internal static bool IsPotentiallyAssignableFrom(ITypeSymbol type, Compilation c
return true;
}

internal static bool IsAssignableFrom(ITypeSymbol type, Compilation compilation) => type switch
{
null => false,
//// https://blogs.msdn.microsoft.com/pfxteam/2012/03/25/do-i-need-to-dispose-of-tasks/
{ ContainingNamespace: { MetadataName: "Tasks", ContainingNamespace: { MetadataName: "Threading", ContainingNamespace.MetadataName: "System" } }, MetadataName: "Task" } => false,
INamedTypeSymbol { ContainingNamespace: { MetadataName: "Tasks", ContainingNamespace: { MetadataName: "Threading", ContainingNamespace.MetadataName: "System" } }, MetadataName: "Task`1", TypeArguments: { Length: 1 } arguments }
=> IsAssignableFrom(arguments[0], compilation),
{ IsRefLikeType: true } => DisposeMethod.IsAccessibleOn(type, compilation),
_ => type.IsAssignableTo(KnownSymbols.IDisposable, compilation),
};
internal static bool IsAssignableFrom(ITypeSymbol type, Compilation compilation) =>
type switch
{
null => false,
//// https://blogs.msdn.microsoft.com/pfxteam/2012/03/25/do-i-need-to-dispose-of-tasks/
{ ContainingNamespace: { MetadataName: "Tasks", ContainingNamespace: { MetadataName: "Threading", ContainingNamespace.MetadataName: "System" } }, MetadataName: "Task" } => false,
INamedTypeSymbol { ContainingNamespace: { MetadataName: "Tasks", ContainingNamespace: { MetadataName: "Threading", ContainingNamespace.MetadataName: "System" } }, MetadataName: "Task`1", TypeArguments: { Length: 1 } arguments }
=> IsAssignableFrom(arguments[0], compilation),
{ IsRefLikeType: true } => DisposeMethod.IsAccessibleOn(type, compilation),
_ => type.IsAssignableTo(KnownSymbols.IDisposable, compilation) || type.IsAssignableTo(KnownSymbols.IAsyncDisposable, compilation),
};

internal static bool IsNop(ExpressionSyntax candidate, SemanticModel semanticModel, CancellationToken cancellationToken)
{
Expand Down