mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
IDEA-216467 "Lambda can be replaced with method reference" does not detect all cases with varargs
GitOrigin-RevId: 232800df8754a17bc8358640b675bc5a8903dfce
This commit is contained in:
committed by
intellij-monorepo-bot
parent
dca558d695
commit
b0929155eb
@@ -0,0 +1,10 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class Example {
|
||||
public static void main(String[] args) {
|
||||
BiFunction<String, String, List<String>> f3 = Arrays::asList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Example {
|
||||
public static void main(String[] args) {
|
||||
Function<String, Example> f1 = Example::new;
|
||||
}
|
||||
public Example(String a, String... b) {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
class Example {
|
||||
public static void main(String[] args) {
|
||||
BiFunction<String, String, Example> f3 = Example::new;
|
||||
}
|
||||
public Example(String a, String... b) {}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class Example {
|
||||
public static void main(String[] args) {
|
||||
BiFunction<String, String, List<String>> f3 = (a, b) -> Arrays.<caret>asList(a, b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Example {
|
||||
public static void main(String[] args) {
|
||||
Function<String, Example> f1 = a -> new<caret> Example(a);
|
||||
}
|
||||
public Example(String a, String... b) {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
class Example {
|
||||
public static void main(String[] args) {
|
||||
BiFunction<String, String, Example> f3 = (a, b) -> new<caret> Example(a, b);
|
||||
}
|
||||
public Example(String a, String... b) {}
|
||||
}
|
||||
@@ -197,9 +197,14 @@ public class LambdaCanBeMethodReferenceInspection extends AbstractBaseJavaLocalI
|
||||
return !(callExpression instanceof PsiNewExpression && qualifier != null);
|
||||
}
|
||||
|
||||
final int offset = parameters.length - calledParametersCount;
|
||||
if (expressions.length > calledParametersCount || offset < 0) {
|
||||
return false;
|
||||
final int offset = ExpressionUtils.isReferenceTo(qualifier, parameters[0]) ? 1 : 0;
|
||||
if (parameters.length != expressions.length + offset) return false;
|
||||
|
||||
if (psiMethod.isVarArgs()) {
|
||||
if (expressions.length < calledParametersCount - 1) return false;
|
||||
}
|
||||
else {
|
||||
if (expressions.length != calledParametersCount) return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < expressions.length; i++) {
|
||||
@@ -208,28 +213,14 @@ public class LambdaCanBeMethodReferenceInspection extends AbstractBaseJavaLocalI
|
||||
}
|
||||
}
|
||||
|
||||
if (offset == 0) {
|
||||
if (qualifier != null) {
|
||||
final boolean[] parameterUsed = new boolean[] {false};
|
||||
qualifier.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitElement(@NotNull PsiElement element) {
|
||||
if (parameterUsed[0]) return;
|
||||
super.visitElement(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
super.visitReferenceExpression(expression);
|
||||
parameterUsed[0] |= ArrayUtil.find(parameters, expression.resolve()) >= 0;
|
||||
}
|
||||
});
|
||||
return !parameterUsed[0];
|
||||
}
|
||||
return true;
|
||||
if (offset == 0 && qualifier != null) {
|
||||
return SyntaxTraverser.psiTraverser(qualifier)
|
||||
.filter(PsiReferenceExpression.class)
|
||||
.map(PsiReferenceExpression::resolve)
|
||||
.filter(target -> ArrayUtil.find(parameters, target) >= 0)
|
||||
.first() == null;
|
||||
}
|
||||
|
||||
return ExpressionUtils.isReferenceTo(qualifier, parameters[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user