mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
[java-inspections] IDEA-283015: fix at isSafeLambdaReplacement level (IJ-CR-17732)
GitOrigin-RevId: 07a8235e72509e11a48a2304fbfc82aa637c8cf6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1806d3fb65
commit
064235a0b3
@@ -4,6 +4,7 @@ package com.intellij.psi;
|
||||
import com.intellij.codeInsight.AnnotationTargetUtil;
|
||||
import com.intellij.core.JavaPsiBundle;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
|
||||
@@ -939,6 +940,19 @@ public final class LambdaUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (PsiPolyExpressionUtil.isInAssignmentOrInvocationContext(lambda)) {
|
||||
PsiType origType = lambda.getFunctionalInterfaceType();
|
||||
if (origType != null) {
|
||||
Project project = lambda.getProject();
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
PsiDeclarationStatement declaration = factory.createVariableDeclarationStatement("$$$", origType, lambda, lambda);
|
||||
PsiLocalVariable variable = (PsiLocalVariable)declaration.getDeclaredElements()[0];
|
||||
PsiLambdaExpression lambdaCopy = (PsiLambdaExpression)variable.getInitializer();
|
||||
PsiExpression replacement = replacer.apply(lambdaCopy);
|
||||
PsiType type = replacement.getType();
|
||||
return type != null && origType.isAssignableFrom(type);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda expression with 'Function.identity()'" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
Function<String, String> myFunc = Function.identity();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda expression with 'Function.identity()'" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
Function<? super CharSequence, ? extends CharSequence> myFunc = Function.identity();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda expression with 'Function.identity()'" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
Function<String, String> myFunc = c <caret>-> c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda expression with 'Function.identity()'" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
Function<? super CharSequence, ? extends CharSequence> myFunc = c <caret>-> c;
|
||||
}
|
||||
}
|
||||
@@ -85,14 +85,6 @@ public class LambdaCanBeMethodCallInspection extends AbstractBaseJavaLocalInspec
|
||||
if (typeParameters.length != 2 || !typeParameters[1].isAssignableFrom(typeParameters[0])) return;
|
||||
@NlsSafe String replacement = CommonClassNames.JAVA_UTIL_FUNCTION_FUNCTION + ".identity()";
|
||||
if (!LambdaUtil.isSafeLambdaReplacement(lambda, replacement)) return;
|
||||
PsiElement parent = PsiUtil.skipParenthesizedExprUp(lambda.getParent());
|
||||
if (parent instanceof PsiAssignmentExpression || parent instanceof PsiReturnStatement ||
|
||||
parent instanceof PsiVariable || parent instanceof PsiLambdaExpression) {
|
||||
if (typeParameters[1] instanceof PsiClassType && typeParameters[0] instanceof PsiClassType &&
|
||||
!typeParameters[0].equals(typeParameters[1])) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
registerProblem(lambda, "Function.identity()", replacement);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user