restrict anonym -> lambda: reject if return type would be changed (IDEA-154805)

This commit is contained in:
Anna.Kozlova
2016-04-29 14:41:28 +02:00
parent 567fc9ae88
commit f52458abdf
5 changed files with 57 additions and 11 deletions
@@ -139,6 +139,15 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
if (inferredType == null) {
return true;
}
PsiType inferenceMethodReturnType = LambdaUtil.getFunctionalInterfaceReturnType(inferredType);
PsiType existingMethodReturnType = method.getReturnType();
if (existingMethodReturnType == null ||
inferenceMethodReturnType != null &&
!PsiType.VOID.equals(inferenceMethodReturnType) && !TypeConversionUtil.isAssignable(existingMethodReturnType, inferenceMethodReturnType)) {
return true;
}
final ForbiddenRefsChecker checker = new ForbiddenRefsChecker(method, aClass, inferredType != PsiType.NULL ? inferredType : null);
final PsiCodeBlock body = method.getBody();
LOG.assertTrue(body != null);
@@ -1,4 +1,4 @@
// "Replace with lambda" "true"
// "Replace with lambda" "false"
class A {
{
bar(new Throwabl<caret>eComputable<String, Exception>() {
@@ -1,4 +1,4 @@
// "Replace with lambda" "true"
// "Replace with lambda" "false"
import java.util.*;
class Test2 {
@@ -0,0 +1,33 @@
// "Replace with lambda" "false"
import java.util.*;
import java.util.function.Function;
import static java.util.Collections.emptyList;
class Ambiguous {
public void setRoots(List<String> roots) {}
public static <T> List<T> concat(Iterable<? extends Collection<T>> list) {
return new ArrayList<T>();
}
public static <T> List<T> concat(List<List<? extends T>> lists) {
return new ArrayList<T>();
}
public static <T,V> List<V> map(Collection<? extends T> iterable, Function<T, V> mapping) {
return emptyList();
}
public void anonymousToLambda(HashSet<String> modules) {
setRoots(Ambiguous.concat(Ambiguous.map(modules, new Function<String, List<String>>() {
@Override
public List<String> apply(String s) {
return null;
}
})
));
}
}
@@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@@ -7,16 +8,19 @@ class A {
ArrayList<String> strings = new ArrayList<String>();
Stream<String> it = strings.stream();
int i = (int) it.flatMap(o -> {
if ('a' > 2) {
return StreamSupport.stream(getIterable().spliterator(), false);
} else if ('c' < 123) {
ArrayList<String> strings1 = new ArrayList<>();
strings1.add(o);
return strings1.stream();
int i = (int) it.flatMap((o) -> StreamSupport.stream(new Function<String, Iterable<String>>() {
@Override
public Iterable<String> apply(String o) {
if ('a' > 2) {
return getIterable();
} else if ('c' < 123) {
ArrayList<String> strings1 = new ArrayList<>();
strings1.add(o);
return strings1;
}
return null;
}
return null;
}).count();
}.apply(o).spliterator(), false)).count();
}