lvti: special highlight for self referencing variables (IDEA-224449)

assume that null as type of the left side of the assignment is not possible

GitOrigin-RevId: c407cdc11e991c8db24ffd683f3b8ea883d41244
This commit is contained in:
Anna.Kozlova
2019-10-09 11:37:00 +02:00
committed by intellij-monorepo-bot
parent e819ba58d4
commit 8db821428d
4 changed files with 12 additions and 4 deletions

View File

@@ -31,6 +31,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.PsiTypeElementImpl;
import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession;
import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
@@ -41,6 +42,8 @@ import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.scope.processor.VariablesNotProcessor;
import com.intellij.psi.scope.util.PsiScopesUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.templateLanguages.OuterLanguageElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.*;
@@ -451,7 +454,8 @@ public class HighlightUtil extends HighlightUtilBase {
PsiType lType = variable.getType();
if (PsiType.NULL.equals(lType)) {
String message = JavaErrorMessages.message("lvti.null");
boolean isSelfReferencing = ReferencesSearch.search(variable, new LocalSearchScope(initializer)).findFirst() != null;
String message = JavaErrorMessages.message(isSelfReferencing ? "lvti.selfReferenced" : "lvti.null");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip(message).range(typeElement).create();
}
if (PsiType.VOID.equals(lType)) {
@@ -498,6 +502,9 @@ public class HighlightUtil extends HighlightUtilBase {
if (rType == null) {
rType = expression.getType();
}
if (lType == PsiType.NULL) {
return null;
}
HighlightInfo highlightInfo = createIncompatibleTypeHighlightInfo(lType, rType, textRange, navigationShift);
if (rType != null && expression != null && isCastIntentionApplicable(expression, lType)) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createAddTypeCastFix(lType, expression));

View File

@@ -459,6 +459,7 @@ lvti.compound='var' is not allowed in a compound declaration
lvti.array='var' is not allowed as an element type of an array
lvti.null=Cannot infer type: variable initializer is 'null'
lvti.void=Cannot infer type: variable initializer is 'void'
lvti.selfReferenced=Cannot infer type: variable initializer is self-referencing
feature.generics=Generics
feature.annotations=Annotations

View File

@@ -9,7 +9,7 @@ class Main {
var d2 = new int[4];
<error descr="Cannot infer type: 'var' on variable without initializer">var</error> e;
var f = <error descr="Array initializer is not allowed here">{ 6 }</error>;
var g = (<error descr="Incompatible types. Found: 'int', required: 'null'">g = 7</error>);
<error descr="Cannot infer type: variable initializer is self-referencing">var</error> g = (g = 7);
}
private static void localVariableType() {

View File

@@ -9,9 +9,9 @@ class MyTest {
public List<Integer> someMethod() {
<error descr="Cannot infer type: 'var' on variable without initializer">var</error> listOfInteger;
Integer[] arrayOfInteger = {2, 4, 8};
<error descr="Incompatible types. Found: 'java.util.ArrayList<java.lang.Integer>', required: 'null'">listOfInteger = Arrays.stream(arrayOfInteger)
listOfInteger = Arrays.stream(arrayOfInteger)
.filter(number -> number >= 4)
.collect(Collectors.toCollection(ArrayList::new))</error>;
.collect(Collectors.toCollection(ArrayList::new));
return listOfInteger;
}
}