Skip to content

Fixed false warning for IDISP010 when using abstract override #579

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

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
65 changes: 65 additions & 0 deletions IDisposableAnalyzers.Test/IDISP010CallBaseDisposeTests/Valid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,71 @@ protected override void Dispose(bool disposing)
RoslynAssert.Valid(Analyzer, DisposableCode, baseClass, code);
}

[Test]
public static void WhenBaseDisposeIsAbstract()
{
var baseClass = @"
namespace N
{
using System;

public abstract class BaseClass : IDisposable
{
/// <inheritdoc/>
public abstract void Dispose();
}
}";
var code = @"
namespace N
{
using System;

public class C : BaseClass
{
private readonly IDisposable disposable = new Disposable();
private bool disposed;

public override void Dispose()
{
if (this.disposed)
{
return;
}

this.disposed = true;
this.disposable.Dispose();
}
}
}";

RoslynAssert.Valid(Analyzer, DisposableCode, baseClass, code);
}

[Test]
public static void WhenBaseDisposeIsAbstractAndExternal()
{
var code = @"
using System;
using System.Buffers;

sealed class Manager : MemoryManager<byte>
{
/// <inheritdoc />
public override void Unpin() { }

/// <inheritdoc />
public override MemoryHandle Pin(int elementIndex = 0) => default;

/// <inheritdoc />
public override Span<byte> GetSpan() => default;

/// <inheritdoc />
protected override void Dispose(bool disposing) { } // <-- lint occurs here
}";

RoslynAssert.Valid(Analyzer, code);
}

[Test]
public static void WhenNoBaseClass()
{
Expand Down
2 changes: 1 addition & 1 deletion IDisposableAnalyzers/Analyzers/DisposeMethodAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static bool IsInterfaceImplementation(IMethodSymbol method)

private static bool ShouldCallBase(SymbolAndDeclaration<IMethodSymbol, MethodDeclarationSyntax> method, SyntaxNodeAnalysisContext context)
{
if (method is { Symbol: { IsOverride: true, OverriddenMethod: { } overridden } } &&
if (method is { Symbol: { IsOverride: true, OverriddenMethod: { IsAbstract: false } overridden } } &&
DisposeMethod.FindBaseCall(method.Declaration, context.SemanticModel, context.CancellationToken) is null)
{
if (overridden.DeclaringSyntaxReferences.Length == 0)
Expand Down