File: FutureReturnValueIgnored.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 (32 lines) | stat: -rw-r--r-- 991 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
Methods that return `java.util.concurrent.Future` and its subclasses generally
indicate errors by returning a future that eventually fails.

If you don’t check the return value of these methods, you will never find out if
they threw an exception.

Nested futures can also result in missed cancellation signals or suppressed
exceptions - see
[Avoiding Nested Futures](https://github.com/google/guava/wiki/ListenableFutureExplained#avoid-nested-futures)
for details.

## Suppression

In certain scenarios like tests, there might be a need of not using the future
values. One can suppress such false positives by either suppressing the check
directly or by saving the future in variables named with prefix `unused`. For
example:

```java
@SuppressWarnings("FutureReturnValueIgnored")
@Test
public void futureInvocation_noMemoryLeak() {
  functionReturningFuture();
}
```

```java
@Test
public void futureInvocation_noMemoryLeak() {
  Future<?> unusedFuture = functionReturningFuture();
}
```