mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
lvti: forbid var/explicit type mix
don't treat var as explicit type
This commit is contained in:
+2
@@ -331,6 +331,8 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
|
||||
.descriptionAndTooltip("Lambda expression not expected here").create());
|
||||
}
|
||||
|
||||
if (!myHolder.hasErrorResults()) myHolder.add(LambdaHighlightingUtil.checkConsistentParameterDeclaration(expression));
|
||||
|
||||
PsiType functionalInterfaceType = null;
|
||||
if (!myHolder.hasErrorResults()) {
|
||||
functionalInterfaceType = expression.getFunctionalInterfaceType();
|
||||
|
||||
+21
@@ -116,4 +116,25 @@ public class LambdaHighlightingUtil {
|
||||
}
|
||||
return functionalInterfaceType.getPresentableText() + " is not a functional interface";
|
||||
}
|
||||
|
||||
public static HighlightInfo checkConsistentParameterDeclaration(PsiLambdaExpression expression) {
|
||||
PsiParameter[] parameters = expression.getParameterList().getParameters();
|
||||
if (parameters.length < 2) return null;
|
||||
boolean hasExplicitParameterTypes = hasExplicitType(parameters[0]);
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
if (hasExplicitParameterTypes != hasExplicitType(parameters[i])) {
|
||||
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
|
||||
.descriptionAndTooltip("Cannot mix 'var' and explicitly typed parameters in lambda expression")
|
||||
.range(expression.getParameterList())
|
||||
.create();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean hasExplicitType(PsiParameter parameter) {
|
||||
PsiTypeElement typeElement = parameter.getTypeElement();
|
||||
return typeElement != null && !typeElement.isInferredType();
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -159,7 +159,9 @@ public class PsiLambdaExpressionImpl extends JavaStubPsiElement<FunctionalExpres
|
||||
public boolean hasFormalParameterTypes() {
|
||||
final PsiParameter[] parameters = getParameterList().getParameters();
|
||||
for (PsiParameter parameter : parameters) {
|
||||
if (parameter.getTypeElement() == null) return false;
|
||||
PsiTypeElement typeElement = parameter.getTypeElement();
|
||||
if (typeElement == null) return false;
|
||||
if (typeElement.isInferredType()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
interface I {
|
||||
void m(int i, int j);
|
||||
}
|
||||
|
||||
class Main {
|
||||
void foo() {
|
||||
I in = <error descr="Cannot mix 'var' and explicitly typed parameters in lambda expression">(var i, int j)</error> -> {};
|
||||
I in1 = (var i, <error descr="Cannot resolve symbol 'j'">j</error><error descr="Identifier expected">)</error> -> {};
|
||||
}
|
||||
}
|
||||
+8
@@ -21,6 +21,14 @@ public class LightAdvHighlightingJdk11Test extends LightDaemonAnalyzerTestCase {
|
||||
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_10, getModule(), getTestRootDisposable());//todo
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);
|
||||
}
|
||||
|
||||
public void testMixedVarAndExplicitTypesInLambdaDeclaration() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testGotoDeclarationOnLambdaVarParameter() {
|
||||
configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
|
||||
final int offset = getEditor().getCaretModel().getOffset();
|
||||
|
||||
Reference in New Issue
Block a user