Java inspection: Support inferred return type of lambda expression in EqualsReplaceableByObjectsCallInspection (IDEA-161076)

This commit is contained in:
Pavel Dolgov
2016-09-16 17:26:41 +03:00
parent b749986366
commit 63131ab1b2
3 changed files with 29 additions and 15 deletions
@@ -37,7 +37,7 @@ import java.util.*;
* @author Pavel.Dolgov
*/
public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocalInspectionTool {
private static final Logger LOG = Logger.getInstance("#" + ReturnSeparatedFromComputationInspection.class.getName());
private static final Logger LOG = Logger.getInstance(ReturnSeparatedFromComputationInspection.class);
@NotNull
@Override
@@ -89,20 +89,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
return ((PsiMethod)returnFrom).getReturnType();
}
if (returnFrom instanceof PsiLambdaExpression) {
return getNonParametrizedReturnType((PsiLambdaExpression)returnFrom);
}
return null;
}
@Nullable
private static PsiType getNonParametrizedReturnType(PsiLambdaExpression lambdaExpression) {
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(lambdaExpression.getFunctionalInterfaceType());
if (interfaceMethod != null) {
final PsiType returnType = interfaceMethod.getReturnType();
if (returnType instanceof PsiPrimitiveType ||
returnType instanceof PsiClassType && ((PsiClassType)returnType).getParameterCount() == 0) {
return returnType;
}
return LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)returnFrom);
}
return null;
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
import java.util.stream.Stream;
class T {
String[] f(String[] a) {
return Stream.of(a).map(s -> {
String r;
if (s.startsWith("#")) return s.substring(1);
else if (s.startsWith("//")) return s.substring(2);
else return s;
}).toArray(String[]::new);
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
import java.util.stream.Stream;
class T {
String[] f(String[] a) {
return Stream.of(a).map(s -> {
String r;
if (s.startsWith("#")) r = s.substring(1);
else if (s.startsWith("//")) r = s.substring(2);
else r = s;
re<caret>turn r;
}).toArray(String[]::new);
}
}