File: ReturnValueIgnored.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (34 lines) | stat: -rw-r--r-- 1,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Certain library methods do nothing useful if their return value is ignored. For
example, String.trim() has no side effects, and you must store the return value
of String.intern() to access the interned string. This check encodes a list of
methods in the JDK whose return value must be used and issues an error if they
are not.

## `Optional.orElseThrow` {#orElseThrow}

Don't call `orElseThrow` just for its side-effects. When the result of a call to
`orElseThrow` is discarded, it may be unclear to future readers whether the
result is being discarded deliberately or accidentally. That is, avoid:

```java
// return value of orElseThrow() is silently ignored here
optional.orElseThrow(() -> new AssertionError("something has gone terribly wrong"));
```

Instead of calling `orElseThrow` for its side-effects, prefer an explicit call
to `isPresent()`, or use one of `checkState` or `checkArgument` from
[Guava's `Preconditions` class](https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/base/Preconditions.html).

```java
if (!optional.isPresent()) {
  throw new AssertionError("something has gone terribly wrong");
}
```

``` {.good}
checkState(optional.isPresent(), "something has gone terribly wrong");
```

``` {.good}
checkArgument(optional.isPresent(), "something has gone terribly wrong");
```