Skip to content

Commit bb97a5d

Browse files
committed
fix(extractor): recognize null-conditional x?.Dispose() as a release in EmitFlowExpr
`EmitFlowExpr` only treated `x.Dispose()` (member access) as a release; `x?.Dispose()` (null-conditional — a member BINDING under a conditional access) fell through to "use". So a `finally { x?.Dispose(); }` modelled the dispose as a use, never releasing x. The finally-before-return threading (this PR) widened that latent false positive onto the return path: a try-with-finally + return that disposes via `?.` was bailed before, but is now analysed and would falsely report OWN001 (Codex review). Fix: recognize `x?.Dispose()`/`x?.Close()`/`x?.DisposeAsync()` as a release too, mirroring IsDisposeShaped (which already handled the member-binding form for the may-throw / catch checks). Pinned by a new sample NullCondFinallyDispose (`ncf` must stay silent in CI). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 95af05c commit bb97a5d

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ jobs:
316316
# `other`: disposed by the finally, so threaded before the early return -> silent.
317317
# `doClean`: acquire+dispose balanced each `do` iteration. `swAll`: every `switch`
318318
# case disposes (no default) -> last case is the tail, no phantom no-match leak.
319-
for ok in clean looped esc exemptTask whileClean asyncDisposed asyncDisposedCfg tfClean tfCatch tfRet tfNull cda daci cif ctorLater lamPrior other doClean swAll; do
319+
# `ncf`: `ncf?.Dispose()` (null-conditional) in a threaded finally IS a release
320+
# (member-binding form), so it is disposed on the return path -> silent (Codex review).
321+
for ok in clean looped esc exemptTask whileClean asyncDisposed asyncDisposedCfg tfClean tfCatch tfRet tfNull cda daci cif ctorLater lamPrior other doClean swAll ncf; do
320322
if echo "$out" | grep -q "'$ok'"; then echo "FAIL: silent/exempt case '$ok' was reported"; exit 1; fi
321323
done
322324
echo "OK: flow-sensitive OWN001/002/003 on real C# (path-sensitive, loops via while/foreach/for, try/finally sequential, never-vs-every-path wording, dispose-optional exempt, beyond flat)"

‎frontend/roslyn/OwnSharp.Extractor/Program.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,20 @@ static void EmitFlowExpr(ExpressionSyntax expr, HashSet<string> tracked, List<ob
612612
nodes.Add(new { op = "release", var = rid.Identifier.Text, line = LineOf(inv) });
613613
return;
614614
}
615+
// x?.Dispose()/x?.Close()/x?.DisposeAsync() (null-conditional) is the release too — the
616+
// call is a member BINDING under a conditional access, not a member access. Mirrors
617+
// IsDisposeShaped so a `?.` dispose (e.g. in a finally) is not mistaken for a bare use,
618+
// which would otherwise falsely flag the resource as leaked.
619+
if (expr is ConditionalAccessExpressionSyntax cond
620+
&& cond.Expression is IdentifierNameSyntax cid
621+
&& tracked.Contains(cid.Identifier.Text)
622+
&& cond.WhenNotNull is InvocationExpressionSyntax condInv
623+
&& condInv.Expression is MemberBindingExpressionSyntax mb
624+
&& mb.Name.Identifier.Text is "Dispose" or "Close" or "DisposeAsync")
625+
{
626+
nodes.Add(new { op = "release", var = cid.Identifier.Text, line = LineOf(cond) });
627+
return;
628+
}
615629
// any other reference to a tracked local -> use (once per local in this expr).
616630
var used = new SortedSet<string>(StringComparer.Ordinal);
617631
foreach (var idn in expr.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>())

‎frontend/roslyn/samples/FlowLocalsSample.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ public void EarlyReturnLeak(bool c)
281281
earlyRet.Dispose();
282282
}
283283

284+
// `ncf?.Dispose()` (null-conditional) in a finally IS a release — EmitFlowExpr recognizes
285+
// the member-binding form, not just `ncf.Dispose()`. With the return threaded through the
286+
// finally, `ncf` is disposed on the early-return path too -> silent (Codex review on PR #34:
287+
// a `?.Dispose()` in a now-threaded finally must not be mistaken for a bare use -> false leak).
288+
public void NullCondFinallyDispose(bool c)
289+
{
290+
var ncf = new MemoryStream();
291+
try { if (c) return; ncf.WriteByte(1); }
292+
finally { ncf?.Dispose(); }
293+
}
294+
284295
// `do { B } while(c)` runs B 1+ times -> desugared to `B; while(c){ B }`. A local acquired
285296
// in the body and never disposed leaks per iteration -> OWN001 on `doLeak`.
286297
public void DoLeak(bool c)

0 commit comments

Comments
 (0)