dotnet / dotnet/fsharp

Forbid CE syntax which misleadingly looks like an early return

Open
#15,759 5 comments 2 reactions 0 assignees View on GitHub
Analyzers Area-Diagnostics Feature Request
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

**Is your feature request related to a problem? Please describe.**

The following code compiles, but its semantics are not at all obvious: the `return` keyword is not in fact returning anything, and it prints "Here".

```fsharp
async {
if true then
return ()
printfn "Here"
return ()
}
|> Async.RunSynchronously
```

I claim this behaviour is very counterintuitive, because `return` is explicitly a word from the imperative paradigm, but it does not behave like the corresponding imperative construct.

(Note that if you try and early-`return` anything other than `unit`, you are correctly told that the thing you're trying to `return` needs to be of `unit` type to satisfy the constraint that it's the body of an `if` block.)

**Describe the solution you'd like**

A warning indicating that this code likely does not do what I expect, suggesting that I remove the `return` keyword.

I don't know how wide-ranging this problem is, because I haven't thought hard about it. Does the warning make sense only for the `async` computation expression, or will it have similarly unexpected semantics in every computation expression that implements `Return`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.