@@ -17,6 +17,20 @@ private FinalizerContextWalker()
1717
1818 internal IReadOnlyList < SyntaxNode > UsedReferenceTypes => this . usedReferenceTypes ;
1919
20+ public override void VisitIfStatement ( IfStatementSyntax node )
21+ {
22+ if ( node . Condition is IdentifierNameSyntax identifierName &&
23+ node . TryFirstAncestor ( out MethodDeclarationSyntax ? methodDeclaration ) &&
24+ methodDeclaration . TryFindParameter ( identifierName . Identifier . Text , out _ ) )
25+ {
26+ this . Visit ( node . Else ) ;
27+ }
28+ else
29+ {
30+ base . VisitIfStatement ( node ) ;
31+ }
32+ }
33+
2034 public override void VisitInvocationExpression ( InvocationExpressionSyntax node )
2135 {
2236 if ( ! IsDisposeBool ( node ) )
@@ -48,21 +62,14 @@ public override void VisitIdentifierName(IdentifierNameSyntax node)
4862
4963 internal static FinalizerContextWalker Borrow ( BaseMethodDeclarationSyntax node , SemanticModel semanticModel , CancellationToken cancellationToken )
5064 {
51- var walker = BorrowAndVisit ( node , SearchScope . Recursive , semanticModel , cancellationToken , ( ) => new FinalizerContextWalker ( ) ) ;
52- if ( node is MethodDeclarationSyntax )
53- {
54- walker . usedReferenceTypes . RemoveAll ( x => IsInIfDisposing ( x ) ) ;
55- }
65+ var walker = BorrowAndVisit ( node , SearchScope . Type , semanticModel , cancellationToken , ( ) => new FinalizerContextWalker ( ) ) ;
5666
5767 foreach ( var target in walker . Targets )
5868 {
59- if ( ! IsInIfDisposing ( target . Source ) )
69+ using var recursiveWalker = TargetWalker . Borrow ( target , walker . Recursion ) ;
70+ if ( recursiveWalker . UsedReferenceTypes . Count > 0 )
6071 {
61- using var recursiveWalker = TargetWalker . Borrow ( target , walker . Recursion ) ;
62- if ( recursiveWalker . UsedReferenceTypes . Count > 0 )
63- {
64- walker . usedReferenceTypes . Add ( target . Source ) ;
65- }
72+ walker . usedReferenceTypes . Add ( target . Source ) ;
6673 }
6774 }
6875
@@ -93,24 +100,6 @@ private static bool IsAssignedNull(SyntaxNode node)
93100 return false ;
94101 }
95102
96- private static bool IsInIfDisposing ( SyntaxNode node )
97- {
98- if ( node . TryFirstAncestor ( out IfStatementSyntax ? ifStatement ) )
99- {
100- if ( ifStatement . Statement . Contains ( node ) &&
101- ifStatement . Condition is IdentifierNameSyntax identifierName &&
102- ifStatement . TryFirstAncestor ( out MethodDeclarationSyntax ? methodDeclaration ) &&
103- methodDeclaration . TryFindParameter ( identifierName . Identifier . Text , out _ ) )
104- {
105- return true ;
106- }
107-
108- return IsInIfDisposing ( ifStatement ) ;
109- }
110-
111- return false ;
112- }
113-
114103 private sealed class TargetWalker : ExecutionWalker < TargetWalker >
115104 {
116105 private readonly List < SyntaxNode > usedReferenceTypes = new List < SyntaxNode > ( ) ;
0 commit comments