@@ -704,10 +704,20 @@ static void EmitFlowExpr(ExpressionSyntax expr, HashSet<string> tracked, Semanti
704704 nodes . Add ( new { op = "release" , var = pbuf , line = LineOf ( expr ) } ) ;
705705 return ;
706706 }
707- // any other reference to a tracked local -> use (once per local in this expr).
707+ // Foo(s) where Foo consumes (disposes) a by-value IDisposable parameter -> the handoff
708+ // RELEASES the matching argument(s) here (the inter-procedural consume contract,
709+ // modelled at the call site like pool Return). A later use of an argument is then a
710+ // use-after-handoff (OWN002). Do NOT return — other tracked arguments of the same call
711+ // (`Consume(s, t)`) still need their `use` below; a consumed arg is excluded from it.
712+ var consumed = ConsumeReleaseArgs ( expr , model ) ;
713+ foreach ( var c in consumed )
714+ if ( tracked . Contains ( c ) )
715+ nodes . Add ( new { op = "release" , var = c , line = LineOf ( expr ) } ) ;
716+ // any other reference to a tracked local -> use (once per local; a consumed arg is a
717+ // release above, never also a use).
708718 var used = new SortedSet < string > ( StringComparer . Ordinal ) ;
709719 foreach ( var idn in expr . DescendantNodesAndSelf ( ) . OfType < IdentifierNameSyntax > ( ) )
710- if ( tracked . Contains ( idn . Identifier . Text ) )
720+ if ( tracked . Contains ( idn . Identifier . Text ) && ! consumed . Contains ( idn . Identifier . Text ) )
711721 used . Add ( idn . Identifier . Text ) ;
712722 foreach ( var u in used )
713723 nodes . Add ( new { op = "use" , var = u , line = LineOf ( expr ) } ) ;
@@ -783,6 +793,58 @@ static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
783793 return ns is { Name : "System" } && ns . ContainingNamespace is { IsGlobalNamespace : true } ;
784794}
785795
796+ // The local names of arguments handed to a first-party CONSUMER at this call — a method
797+ // whose own body disposes the by-value IDisposable parameter the argument binds to. Such
798+ // an argument's ownership moves into the callee and is discharged there, so the handoff is
799+ // modelled as a RELEASE of the argument at the call site (the same shape as pool
800+ // `Return(buf)`); a later use is then a use-after-handoff (OWN002). Inspecting the callee's
801+ // OWN body means no cross-call signature table and no dangling-callee crash — a callee with
802+ // no body (interface / abstract / extern) or that does not dispose the param contributes
803+ // nothing, and the argument stays an ordinary escape. Arguments resolve to parameters by
804+ // NAME when `name:` is used, else by position; partial declarations are scanned for a body.
805+ static List < string > ConsumeReleaseArgs ( ExpressionSyntax e , SemanticModel model )
806+ {
807+ var consumed = new List < string > ( ) ;
808+ if ( e is not InvocationExpressionSyntax inv
809+ || model . GetSymbolInfo ( inv ) . Symbol is not IMethodSymbol sym )
810+ return consumed ;
811+ SyntaxNode ? body = null ;
812+ foreach ( var r in sym . DeclaringSyntaxReferences )
813+ if ( r . GetSyntax ( ) is BaseMethodDeclarationSyntax d
814+ && ( ( SyntaxNode ? ) d . Body ?? d . ExpressionBody ) is { } b )
815+ {
816+ body = b ;
817+ break ;
818+ }
819+ if ( body is null )
820+ return consumed ;
821+ var args = inv . ArgumentList . Arguments ;
822+ for ( int i = 0 ; i < args . Count ; i ++ )
823+ {
824+ // map argument -> parameter: by name for `name: value`, else by position.
825+ var p = args [ i ] . NameColon is { } nc
826+ ? sym . Parameters . FirstOrDefault ( q => q . Name == nc . Name . Identifier . Text )
827+ : ( i < sym . Parameters . Length ? sym . Parameters [ i ] : null ) ;
828+ if ( p is { RefKind : RefKind . None } && ImplementsIDisposable ( p . Type )
829+ && DisposesLocal ( body , p . Name )
830+ && args [ i ] . Expression is IdentifierNameSyntax aid )
831+ consumed . Add ( aid . Identifier . Text ) ;
832+ }
833+ return consumed ;
834+ }
835+
836+ // Does `body` dispose the local/parameter named `name` — a `name.Dispose()` / `.Close()` /
837+ // `.DisposeAsync()` call (the consume signal)?
838+ static bool DisposesLocal ( SyntaxNode body , string name )
839+ {
840+ foreach ( var i in body . DescendantNodesAndSelf ( ) . OfType < InvocationExpressionSyntax > ( ) )
841+ if ( i . Expression is MemberAccessExpressionSyntax m
842+ && m . Name . Identifier . Text is "Dispose" or "Close" or "DisposeAsync"
843+ && m . Expression is IdentifierNameSyntax id && id . Identifier . Text == name )
844+ return true ;
845+ return false ;
846+ }
847+
786848// A field/local type treated as owned-disposable (syntax-only heuristic — no
787849// semantic model): a curated set plus a few suffixes. Gated on the class `new`ing
788850// the value, so injected/borrowed disposables are not flagged. Timer types are
@@ -1343,9 +1405,21 @@ or ImplicitObjectCreationExpressionSyntax } init
13431405 var nm = idn . Identifier . Text ;
13441406 if ( ! candidates . Contains ( nm ) )
13451407 continue ;
1408+ // ... unless it is handed to a CONSUMER (a first-party method that
1409+ // disposes a by-value IDisposable param) as a bare `Consume(s);`
1410+ // statement: that is a handoff RELEASED at the call site, not an escape
1411+ // (else the use-after-handoff would be hidden). Tied to the statement
1412+ // form the flow pass lowers, so a local is never exempted without a
1413+ // matching release (a `var n = Consume(s)` initializer is NOT lowered
1414+ // here, so it stays an escape rather than a false leak).
1415+ bool consumedArg = idn . Parent is ArgumentSyntax
1416+ && idn . Parent . Parent is ArgumentListSyntax
1417+ && idn . Parent . Parent . Parent is InvocationExpressionSyntax cinv
1418+ && cinv . Parent is ExpressionStatementSyntax
1419+ && ConsumeReleaseArgs ( cinv , model ) . Contains ( nm ) ;
13461420 if ( idn . Parent is ReturnStatementSyntax
13471421 || ( idn . Parent is AssignmentExpressionSyntax asg && asg . Right == idn )
1348- || ( idn . Parent is ArgumentSyntax && ! poolBuffers . Contains ( nm ) ) )
1422+ || ( idn . Parent is ArgumentSyntax && ! poolBuffers . Contains ( nm ) && ! consumedArg ) )
13491423 escapedLocals . Add ( nm ) ;
13501424 }
13511425 var tracked = new HashSet < string > ( candidates ) ;
0 commit comments