DataFlowInspection: highlight method references known to return constant true/false value with quick-fix changing them to trivial lambdas (partially fixes IDEA-170885).

This commit is contained in:
Tagir Valeev
2017-04-26 12:43:30 +07:00
parent e018876a4c
commit 9127146364
13 changed files with 347 additions and 37 deletions
@@ -0,0 +1,29 @@
// "Fix all 'Constant conditions & exceptions' problems in file" "true"
import org.jetbrains.annotations.*;
import java.util.*;
import java.util.stream.Stream;
public class MethodReferenceConstantValue {
@Contract(value = "!null -> false", pure = true)
public boolean strangeMethod(String s) {
return s == null ? new Random().nextBoolean() : false;
}
public void test(Optional<String> opt) {
X x = (methodReferenceConstantValue, s) -> false;
Boolean aBoolean = opt.map(s -> false)
.map(o -> true)
.map(o -> false)
.orElse(false);
if (opt.isPresent()) {
Stream.generate(() -> true)
.limit(10)
.forEach(System.out::println);
}
}
interface X {
boolean action(@Nullable MethodReferenceConstantValue a, @NotNull String b);
}
}
@@ -0,0 +1,29 @@
// "Fix all 'Constant conditions & exceptions' problems in file" "true"
import org.jetbrains.annotations.*;
import java.util.*;
import java.util.stream.Stream;
public class MethodReferenceConstantValue {
@Contract(value = "!null -> false", pure = true)
public boolean strangeMethod(String s) {
return s == null ? new Random().nextBoolean() : false;
}
public void test(Optional<String> opt) {
X x = MethodReferenceConstantValue::strangeMethod;
Boolean aBoolean = opt.map(th<caret>is::strangeMethod)
.map(Objects::nonNull)
.map(Objects::isNull)
.orElse(false);
if (opt.isPresent()) {
Stream.generate(opt::isPresent)
.limit(10)
.forEach(System.out::println);
}
}
interface X {
boolean action(@Nullable MethodReferenceConstantValue a, @NotNull String b);
}
}