Unused return value checker
The unused return value checker allows you to detect ignored results. These are values returned from expressions that produce something other than Unit, Nothing, or Nothing? and aren't:
Stored in a variable or property.
Returned or thrown.
Passed as an argument to another function.
Used as a receiver in a call or safe call.
Checked in a condition such as
if,when, orwhile.Used as the last statement of a lambda.
The checker doesn't report ignored results for increment operations like ++ and --, or for boolean shortcuts where the right-hand side exits the current function, for example, condition || return.
You can use the unused return value checker to catch bugs where a function call produces a meaningful result, but the result is silently dropped. This helps prevent unexpected behavior and makes such issues easier to track down.
Here's an example where a string is created but never used, so the checker reports it as an ignored result:
Configure the unused return value checker
You can control how the compiler reports ignored results with the -Xreturn-value-checker compiler option.
It has the following modes:
disabledisables the unused return value checker (default).checkenables the checker, and reports warnings for ignored results from marked functions.fullenables the checker, treats all functions in your project as marked, and reports warnings for ignored results.
To use the unused return value checker in your project, add the compiler option to your build configuration file:
Mark functions to check ignored results
When you set the -Xreturn-value-checker compiler option to check, the checker reports ignored results only from expressions that are marked, like most functions in the Kotlin standard library.
To mark your own code, use the @MustUseReturnValues annotation. You can apply it to a file, class, or function depending on the scope you want the checker to cover.
For example, you can mark an entire file:
Or a specific class:
Suppress reports for ignored results
You can suppress reports on specific functions by annotating them with @IgnorableReturnValue. Annotate functions where ignoring the result is common and expected, such as MutableList.add:
You can suppress a warning without annotating the function itself. To do this, assign the result to a special unnamed variable with an underscore syntax (_):
Ignored results in function overrides
When you override a function, the override inherits the reporting rules defined by the annotations on the base declaration. This also applies when the base declaration is part of the Kotlin standard library or of other library dependencies, so the checker reports ignored results for overrides of functions like Any.hashCode().
Additionally, you can't override a function marked with @IgnorableReturnValue with another function that requires its return value to be used. However, you can mark an override with @IgnorableReturnValue in a class or interface annotated with @MustUseReturnValues when its result can be safely ignored:
Interoperability with Java annotations
Some Java libraries use similar mechanisms with different annotations. The unused return value checker treats the following annotations as equivalent to using @MustUseReturnValues:
It also treats com.google.errorprone.annotations.CanIgnoreReturnValue as equivalent to using @IgnorableReturnValue.