A lambda body is a block with a single statement where an expression would do
What it finds
x -> { return x * 2; } wraps a single expression in a block and a return. The expression alone, x -> x * 2, says the same with less to read. The same holds for a block with a single expression statement. The fix writes the expression form. For an expression statement it is unsafe: a lambda whose expression has a value also fits functional interfaces that return one, which can change the overload a call picks.
From the rule's source, lambda_return_block.rs.
import java.util.List;import java.util.function.Function; class Mapping { void run(List<String> names) { Function<String, Integer> length = s -> { return s.length(); };tx0 reports: this lambda body can be the expression alone names.forEach(n -> { System.out.println(n); });tx0 reports: this lambda body can be the expression alone names.forEach(n -> { String upper = n.toUpperCase(); System.out.println(upper); }); }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:lambda-block-body" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:lambda-block-body] reason="..."tx0 explain tx0/java:lambda-block-body shows the same in the terminal. Found a false positive? Report it.