Skip to content

Commit c1ed4c5

Browse files
MafiiJohanLarsson
authored andcommitted
Refactor IsVariableDeclarationWithDisposeAsExtensionCall
1 parent b4c952d commit c1ed4c5

File tree

1 file changed

+5
-45
lines changed

1 file changed

+5
-45
lines changed

IDisposableAnalyzers/Helpers/Disposable.ShouldDispose.cs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -118,50 +118,10 @@ static bool ShouldNotDispose(IdentifierNameSyntax usage, Recursion recursion)
118118
}
119119
}
120120

121+
// For simplicity reasons, we do not validate the variable type, or what is to the left of the call.
122+
// Instead, we simply check if the last invocation of the rhs is `.DisposeWith(<one argument>);`
121123
private static bool IsVariableDeclarationWithDisposeAsExtensionCall(BlockSyntax localBlock)
122-
{
123-
if (localBlock is not { Statements: { Count: 1 } statements })
124-
{
125-
return false;
126-
}
127-
128-
if (statements[0] is not LocalDeclarationStatementSyntax { Declaration: { } variableDeclaration })
129-
{
130-
return false;
131-
}
132-
133-
if (variableDeclaration.Variables is not { Count: 1 } vars)
134-
{
135-
return false;
136-
}
137-
138-
if (vars[0].Initializer is not { } rhs)
139-
{
140-
return false;
141-
}
142-
143-
// For simplicity reasons, we do not validate the variable type, or what is to the left of the call.
144-
// Instead, we simply check if the last invocation of the rhs is `.DisposeWith(<one argument>);`
145-
if (rhs.Value is not InvocationExpressionSyntax lastInvocationInChain)
146-
{
147-
return false;
148-
}
149-
150-
if (lastInvocationInChain.Expression is not MemberAccessExpressionSyntax memberAccess)
151-
{
152-
return false;
153-
}
154-
155-
if (memberAccess.Name.Identifier.Value is not "DisposeWith")
156-
{
157-
return false;
158-
}
159-
160-
if (lastInvocationInChain.ArgumentList.Arguments is not { Count: 1 } arguments)
161-
{
162-
return false;
163-
}
164-
165-
return true;
166-
}
124+
=> localBlock is { Statements: { Count: 1 } statements } &&
125+
statements[0] is LocalDeclarationStatementSyntax { Declaration.Variables: { Count: 1 } vars } &&
126+
vars[0].Initializer is { Value: InvocationExpressionSyntax { ArgumentList.Arguments.Count: 1, Expression: MemberAccessExpressionSyntax { Name.Identifier.Value: "DisposeWith" } } };
167127
}

0 commit comments

Comments
 (0)