Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document usage of -Wnonunit-statement #4054

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,28 @@ It is still worth keeping in mind that this is only a partial solution. Whenever
> Note: It is actually possible to implement a *proper* solution for cancelation here simply by applying the `cancelable` to the inner `blocking` effect and defining the handler to be `IO.blocking(fis.close())`, in addition to adding some error handling logic to catch the corresponding exception. This is a special case however, specific to `FileInputStream`, and doesn't make for as nice of an example. :-)

The reason this is safe is it effectively leans entirely on *cooperative* cancelation. It's relatively common to have effects which cannot be canceled by normal means (and thus are, correctly, `uncancelable`) but which *can* be terminated early by using some ancillary protocol (in this case, an `AtomicBoolean`). Note that nothing is magic here and this is still fully safe with respect to backpressure and other finalizers. For example, `fis.close()` will still be run at the proper time, and cancelation of this fiber will only complete when all finalizers are done, exactly the same as non-`uncancelable` effects.

## What do non-unit statement warnings mean?

We recommend to enable the compiler warning `-Wnonunit-statement`. If you do so you get compiler warnings for some common bug patterns.

For example the following code emits the warning: `unused value of type cats.effect.IO[Unit]`.

```scala
def example(input: String): IO[String] = {
IO.println(input)
IO(input)
froth marked this conversation as resolved.
Show resolved Hide resolved
}
```

This warning tells you, that the `IO.println` statement is created and then discarded without being used. Consequently this code will not print anything. To fix this you have to connect both IO instances.
froth marked this conversation as resolved.
Show resolved Hide resolved

Working version of the above example (emitting no warnings):

```scala
def example(input: String): IO[String] = {
IO.println(input)
.flatMap(_ => IO(input))
froth marked this conversation as resolved.
Show resolved Hide resolved
}
```

6 changes: 6 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Naturally, if you're using ScalaJS, you should replace the double `%%` with a tr
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1")
```

It is remommended to enable warnings for non-unit statements(see [FAQ](./faq.md#what-do-non-unit-statement-warnings-mean)):
froth marked this conversation as resolved.
Show resolved Hide resolved

```scala
scalacOptions += "-Wnonunit-statement"
```

Alternatively, you can use the Cats Effect 3 Giter8 template, which sets up some basic project infrastructure:

```bash
Expand Down
Loading