Skip to content
Sarj Code Standards

no-return-disposed-resource

Do not return a resource whose using declaration disposes it before the caller receives it.

Why

Leaving the resource's using scope invokes its disposer before the returned value reaches its consumer, producing an already disposed dependency.

Fix

Keep consumers inside the resource scope, yield it through an awaited fixture use callback, or transfer ownership without a using declaration. Return an independent snapshot when appropriate.

Examples

Before — flagged The resource is disposed before return
src/client.ts
function client() {
using handle = acquire();
return handle;
}
After — preferred Return a value independent of resource lifetime
src/client.ts
function client() {
using handle = acquire();
return handle.snapshot();
}