IsInstanceLambdaUsage
SUGGESTION
Simplification
Summary
Prefer
Class::isInstance
method reference over equivalent lambda expression
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("IsInstanceLambdaUsage")
to the enclosing element.Disable this pattern completely by adding
-Xep:IsInstanceLambdaUsage:OFF
as compiler argument. Learn more.
Samples
Replacement
Shows the difference in example code before and after the bug pattern is applied.
import java.util.stream.Stream;
class A {
void m() {
- Stream.of(1).filter(i -> i instanceof Integer);
+ Stream.of(1).filter(Integer.class::isInstance);
}
}
Identification
Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains:
comment is placed above any violating line.
import java.util.stream.Stream;
import reactor.core.publisher.Flux;
class A {
void m() {
Integer localVariable = 0;
Stream.of(0).map(i -> i);
Stream.of(1).map(i -> i + 1);
Stream.of(2).filter(Integer.class::isInstance);
Stream.of(3).filter(i -> i.getClass() instanceof Class);
Stream.of(4).filter(i -> localVariable instanceof Integer);
// XXX: Ideally this case is also flagged. Pick this up in the context of merging the
// `IsInstanceLambdaUsage` and `MethodReferenceUsage` checks, or introduce a separate check that
// simplifies unnecessary block lambda expressions.
Stream.of(5)
.filter(
i -> {
return i instanceof Integer;
});
Flux.just(6, "foo").distinctUntilChanged(v -> v, (a, b) -> a instanceof Integer);
// BUG: Diagnostic contains:
Stream.of(7).filter(i -> i instanceof Integer);
}
}