LambdaUtil#isIdentityLambda

This commit is contained in:
Tagir Valeev
2017-01-24 15:34:51 +07:00
parent 1820f2db99
commit 1bfb45a6d4
3 changed files with 28 additions and 10 deletions
@@ -183,12 +183,12 @@ public class RedundantStreamOptionalCallInspection extends BaseJavaBatchLocalIns
}
if (expression instanceof PsiLambdaExpression) {
PsiLambdaExpression lambda = (PsiLambdaExpression)expression;
if (LambdaUtil.isIdentityLambda(lambda)) return true;
if (!allowBoxUnbox) return false;
PsiExpression body = LambdaUtil.extractSingleExpressionFromBody(lambda.getBody());
PsiParameter[] parameters = lambda.getParameterList().getParameters();
if (parameters.length != 1) return false;
PsiParameter parameter = parameters[0];
if (ExpressionUtils.isReferenceTo(body, parameter)) return true;
if (!allowBoxUnbox) return false;
PsiMethodCallExpression call = tryCast(PsiUtil.skipParenthesizedExprDown(body), PsiMethodCallExpression.class);
if (call == null) return false;
PsiReferenceExpression methodExpression = call.getMethodExpression();
@@ -410,13 +410,7 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
"valueOf", PsiType.BOOLEAN)) {
return true;
}
if (arg instanceof PsiLambdaExpression) {
PsiLambdaExpression lambda = (PsiLambdaExpression)arg;
PsiExpression body = LambdaUtil.extractSingleExpressionFromBody(lambda.getBody());
PsiParameterList parameters = lambda.getParameterList();
return parameters.getParametersCount() == 1 && ExpressionUtils.isReferenceTo(body, parameters.getParameters()[0]);
}
return false;
return arg instanceof PsiLambdaExpression && LambdaUtil.isIdentityLambda((PsiLambdaExpression)arg);
}
@Nullable
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -931,6 +931,30 @@ public class LambdaUtil {
return variable.getName() + " -> " + expression.getText();
}
/**
* Returns true if lambda has single parameter and its return value is the same as parameter.
*
* <p>
* The lambdas like this are considered identity lambda: {@code x -> x}, {@code x -> {return x;}}
* {@code (String x) -> (x)}, etc.</p>
*
* <p>
* This method does not check the lambda type, also it does not check whether auto-(un)boxing occurs,
* so a lambda like {@code ((Predicate<Boolean>)b -> b)} is also identity lambda even though it performs
* auto-unboxing.
* </p>
*
* @param lambda a lambda to check
* @return true if the supplied lambda is an identity lambda
*/
public static boolean isIdentityLambda(PsiLambdaExpression lambda) {
PsiParameterList parameters = lambda.getParameterList();
if(parameters.getParametersCount() != 1) return false;
PsiExpression expression = PsiUtil.skipParenthesizedExprDown(extractSingleExpressionFromBody(lambda.getBody()));
return expression instanceof PsiReferenceExpression &&
((PsiReferenceExpression)expression).isReferenceTo(parameters.getParameters()[0]);
}
public static class TypeParamsChecker extends PsiTypeVisitor<Boolean> {
private PsiMethod myMethod;
private final PsiClass myClass;