@@ -2233,21 +2233,53 @@ or ImplicitObjectCreationExpressionSyntax
22332233 // disposes. Owned (not injected) = in `constructed` (computed above);
22342234 // released = a `<field>.Dispose()` call somewhere in the class.
22352235 var disposed = new HashSet < string > ( ) ;
2236+ // A local that ALIASES a field of this class — `var cts = _cts;` / `var cts = this._cts;` —
2237+ // and is then disposed THROUGH the alias (`cts.Dispose()`) releases the field's object: the
2238+ // alias and the field name the same instance. Map each such alias LOCAL SYMBOL -> its field
2239+ // so the disposal scan below credits the FIELD. Mined: Npgsql's NpgsqlDataSource disposes its
2240+ // CancellationTokenSource via `var cts = _cts; cts.Dispose();`. Sound by construction:
2241+ // - the initializer is a `this`/bare field reference (never `other._f`) that BINDS to a real
2242+ // field symbol (not a same-named local);
2243+ // - aliases are keyed by the LOCAL SYMBOL, not its name, so a same-named local in another
2244+ // method (or a second alias reusing the name) is a DISTINCT entry, never conflated (Codex);
2245+ // - an alias REASSIGNED anywhere — by `=`, or rebound through a `ref`/`out` argument — is
2246+ // excluded: a rebound local no longer tracks the field, so we decline to credit it (Codex).
2247+ var reassignedAliases = new HashSet < ISymbol > ( SymbolEqualityComparer . Default ) ;
2248+ foreach ( var asg in assigns )
2249+ if ( model . GetSymbolInfo ( asg . Left ) . Symbol is ILocalSymbol rls )
2250+ reassignedAliases . Add ( rls ) ;
2251+ foreach ( var arg in cls . DescendantNodes ( ) . OfType < ArgumentSyntax > ( ) )
2252+ if ( ( arg . RefKindKeyword . IsKind ( SyntaxKind . RefKeyword ) || arg . RefKindKeyword . IsKind ( SyntaxKind . OutKeyword ) )
2253+ && model . GetSymbolInfo ( arg . Expression ) . Symbol is ILocalSymbol als )
2254+ reassignedAliases . Add ( als ) ;
2255+ var aliasToField = new Dictionary < ISymbol , string > ( SymbolEqualityComparer . Default ) ;
2256+ foreach ( var decl in cls . DescendantNodes ( ) . OfType < VariableDeclaratorSyntax > ( ) )
2257+ if ( decl . Initializer ? . Value is { } init
2258+ && ThisFieldName ( init ) is { } af
2259+ && model . GetSymbolInfo ( init ) . Symbol is IFieldSymbol
2260+ && model . GetDeclaredSymbol ( decl ) is ILocalSymbol aliasSym
2261+ && ! reassignedAliases . Contains ( aliasSym ) )
2262+ aliasToField [ aliasSym ] = af ;
2263+ // a `.Dispose()`/`.DisposeAsync()` on a field — directly (`_f.Dispose()`) or through an
2264+ // alias local (translated by SYMBOL via aliasToField) — releases that field.
22362265 foreach ( var inv in cls . DescendantNodes ( ) . OfType < InvocationExpressionSyntax > ( ) )
22372266 if ( inv . Expression is MemberAccessExpressionSyntax m
22382267 && m . Name . Identifier . Text is "Dispose" or "DisposeAsync"
22392268 && FieldName ( m . Expression ) is { } df )
2240- disposed . Add ( df ) ;
2269+ disposed . Add ( model . GetSymbolInfo ( m . Expression ) . Symbol is ILocalSymbol ls
2270+ && aliasToField . TryGetValue ( ls , out var fa ) ? fa : df ) ;
22412271 // Also the NULL-CONDITIONAL form `field?.Dispose()` (a ConditionalAccess whose
22422272 // WhenNotNull is the `.Dispose()` invocation, NOT a plain MemberAccess) — the
22432273 // dominant disposal shape the match above misses. Mined as an FP across ImageSharp:
22442274 // `this.memoryStream?.Dispose()` (ZipExrCompressor/DeflateCompressor/IccDataWriter)
22452275 // and the BufferedStreams benchmark's `[GlobalCleanup]` `field?.Dispose()` calls.
2276+ // (The same alias-by-symbol translation applies — `cts?.Dispose()` on an aliasing local.)
22462277 foreach ( var cae in cls . DescendantNodes ( ) . OfType < ConditionalAccessExpressionSyntax > ( ) )
22472278 if ( FieldName ( cae . Expression ) is { } cdf
22482279 && cae . WhenNotNull is InvocationExpressionSyntax { Expression : MemberBindingExpressionSyntax mb }
22492280 && mb . Name . Identifier . Text is "Dispose" or "DisposeAsync" )
2250- disposed . Add ( cdf ) ;
2281+ disposed . Add ( model . GetSymbolInfo ( cae . Expression ) . Symbol is ILocalSymbol lc
2282+ && aliasToField . TryGetValue ( lc , out var fc ) ? fc : cdf ) ;
22512283
22522284 // P-004 EventSource counter exemption: inside an EventSource, a DiagnosticCounter field
22532285 // constructed with `this` is registered to (and lifetime-owned by) the source — a
0 commit comments