Skip to content

Commit

Permalink
Migrate S3699: Implement ShouldExecute for VB.NET (#7449)
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource authored Jun 16, 2023
1 parent 7546630 commit 5d2ab8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,31 @@ public sealed class ObjectsShouldNotBeDisposedMoreThanOnce : ObjectsShouldNotBeD
public static readonly DiagnosticDescriptor S3966 = DescriptorFactory.Create(DiagnosticId, MessageFormat);
protected override DiagnosticDescriptor Rule => S3966;

public override bool ShouldExecute() => true;
public override bool ShouldExecute()
{
var walker = new Walker();
walker.SafeVisit(Node);
return walker.Result;
}

protected override bool IsDispose(IMethodSymbol method) =>
method.IsIDisposableDispose()
|| method.IsIAsyncDisposableDisposeAsync()
|| method.ExplicitInterfaceImplementations.Any(x => x.IsIDisposableDispose() || x.IsIAsyncDisposableDisposeAsync());

private sealed class Walker : SafeVisualBasicSyntaxWalker
{
public bool Result { get; private set; }

public override void Visit(SyntaxNode node)
{
if (!Result)
{
base.Visit(node);
}
}

public override void VisitInvocationExpression(InvocationExpressionSyntax node) =>
Result = node.HasExactlyNArguments(0) || node.ArgumentList is null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ Class Program
d.Dispose() ' Noncompliant {{Resource 'd' has already been disposed explicitly or through a using statement implicitly. Remove the redundant disposal.}}
End Sub

Public Sub DisposedTwice_MemberAccess(d As IDisposable)
d.Dispose
d.Dispose ' Noncompliant
End Sub

Public Sub DisposedTwice_Alias()
Dim d As New DisposableAlias()
d.CleanUp()
Expand Down

0 comments on commit 5d2ab8e

Please sign in to comment.