Header menu logo G-Research F# Analyzers

DisposedBeforeAsyncRunAnalyzer

Problem

A use statement in an async or task returning function can be problematic as the value is disposed before the returned workflow is run.

let f () =
    use t = new DisposableThing()
    async {
        return "hi"
    }

Fix

If applicable to your context, move the use statement into the async.

let f () =
    async {
        use t = new DisposableThing()
        return "hi"
    }

If the use before the async is really your intent, you can disable the warning with a line comment on top of the use statement containing disposed before returned async runs or disposed before returned task runs.

let f () =
    // Note: disposed before returned async runs
    use t = new DisposableThing()
    async {
        return "hi"
    }
val f: unit -> Async<string>
val t: System.IDisposable
val async: AsyncBuilder

Type something to start searching.