UnusedLambdaParameterShouldBeUnnamed
Since Checkstyle 10.18.0
Description
Ensures that lambda parameters that are not used are declared as an unnamed variable.
Rationale:
- Improves code readability by clearly indicating which parameters are unused.
-
Follows Java conventions for denoting unused parameters with an
underscore (
_
).
See the Java Language Specification for more information about unnamed variables.
Attention: Unnamed variables are available as a preview feature in Java 21, and became an official part of the language in Java 22. This check should be activated only on source code which meets those requirements.
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="UnusedLambdaParameterShouldBeUnnamed"/>
</module>
</module>
Example:
class Example1 {
int x;
void test() {
// violation below, 'Unused lambda parameter 'x' should be unnamed'
Function<Integer, Integer> f1 = (x) -> this.x + 1;
// ok below, parameter is unnamed
Function<Integer, Integer> f2 = (_) -> this.x + 1;
// ok below, parameter is used
Function<Integer, Integer> f3 = (x) -> x + 1;
// violation below, 'Unused lambda parameter 'y' should be unnamed'
BiFunction<Integer, Integer, Integer> bf1 = (x, y) -> x + this.x;
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.coding