[java-highlighting] fixes after recent changes in highlighting

Part of IDEA-365344 Create a new Java error highlighter with minimal dependencies (PSI only)

GitOrigin-RevId: 5fbcd6d223a0ea3c04bc0d219cff125938470816
This commit is contained in:
Tagir Valeev
2025-01-31 11:50:27 +01:00
committed by intellij-monorepo-bot
parent e985705e03
commit 9ff09939f1
7 changed files with 30 additions and 20 deletions

View File

@@ -1319,6 +1319,7 @@ final class ExpressionChecker {
}
myVisitor.report(JavaErrorKinds.CAST_INCONVERTIBLE.create(
expression, new JavaIncompatibleTypeErrorContext(operandType, checkType)));
return;
}
PsiPrimaryPattern pattern = expression.getPattern();
if (pattern instanceof PsiDeconstructionPattern deconstruction) {

View File

@@ -757,6 +757,11 @@ final class JavaErrorVisitor extends JavaElementVisitor {
if (resolved != null && parent instanceof PsiReferenceList referenceList && !hasErrorResults()) {
checkElementInReferenceList(ref, referenceList, result);
}
if (!hasErrorResults()) myClassChecker.checkAbstractInstantiation(ref);
if (!hasErrorResults()) myClassChecker.checkExtendsDuplicate(ref, resolved);
if (!hasErrorResults()) myClassChecker.checkClassExtendsForeignInnerClass(ref, resolved);
if (!hasErrorResults()) myGenericsChecker.checkSelectStaticClassFromParameterizedType(resolved, ref);
if (!hasErrorResults() && parent instanceof PsiNewExpression newExpression) myGenericsChecker.checkDiamondTypeNotAllowed(newExpression);
if (!hasErrorResults() && (!(parent instanceof PsiNewExpression newExpression) || !newExpression.isArrayCreation())) {
myGenericsChecker.checkParameterizedReferenceTypeArguments(resolved, ref, result.getSubstitutor());
}
@@ -791,11 +796,6 @@ final class JavaErrorVisitor extends JavaElementVisitor {
if (parent instanceof PsiAnonymousClass psiAnonymousClass && ref.equals(psiAnonymousClass.getBaseClassReference())) {
if (!hasErrorResults()) myGenericsChecker.checkGenericCannotExtendException(psiAnonymousClass);
}
if (!hasErrorResults()) myClassChecker.checkAbstractInstantiation(ref);
if (!hasErrorResults()) myClassChecker.checkExtendsDuplicate(ref, resolved);
if (!hasErrorResults()) myClassChecker.checkClassExtendsForeignInnerClass(ref, resolved);
if (!hasErrorResults() && parent instanceof PsiNewExpression newExpression) myGenericsChecker.checkDiamondTypeNotAllowed(newExpression);
if (!hasErrorResults()) myGenericsChecker.checkSelectStaticClassFromParameterizedType(resolved, ref);
if (!hasErrorResults() && resolved instanceof PsiClass psiClass) myExpressionChecker.checkRestrictedIdentifierReference(ref, psiClass);
return result;
}

View File

@@ -223,11 +223,11 @@ final class PatternChecker {
PsiType expressionType = expression.getOperand().getType();
if (expressionType != null && checkType.isAssignableFrom(expressionType)) {
if (checkType.equals(expressionType)) {
myVisitor.report(JavaErrorKinds.PATTERN_INSTANCEOF_EQUALS.create(expression, checkType));
myVisitor.report(JavaErrorKinds.PATTERN_INSTANCEOF_EQUALS.create(pattern, checkType));
}
else {
myVisitor.report(JavaErrorKinds.PATTERN_INSTANCEOF_SUPERTYPE.create(
expression, new JavaIncompatibleTypeErrorContext(checkType, expressionType)));
pattern, new JavaIncompatibleTypeErrorContext(checkType, expressionType)));
}
}
}

View File

@@ -734,13 +734,13 @@ public final class JavaErrorKinds {
})
.withRawDescription((list, ctx) -> message("pattern.deconstruction.count.mismatch",
ctx.recordComponents().length, ctx.patternComponents().length));
public static final Parameterized<PsiInstanceOfExpression, JavaIncompatibleTypeErrorContext> PATTERN_INSTANCEOF_SUPERTYPE =
parameterized(PsiInstanceOfExpression.class, JavaIncompatibleTypeErrorContext.class, "pattern.instanceof.supertype")
public static final Parameterized<PsiTypeTestPattern, JavaIncompatibleTypeErrorContext> PATTERN_INSTANCEOF_SUPERTYPE =
parameterized(PsiTypeTestPattern.class, JavaIncompatibleTypeErrorContext.class, "pattern.instanceof.supertype")
.withAnchor((expr, context) -> expr.getCheckType())
.withRawDescription((expr, context) -> message(
"pattern.instanceof.supertype", context.lType().getPresentableText(), requireNonNull(context.rType()).getPresentableText()));
public static final Parameterized<PsiInstanceOfExpression, PsiType> PATTERN_INSTANCEOF_EQUALS =
parameterized(PsiInstanceOfExpression.class, PsiType.class, "pattern.instanceof.equals")
public static final Parameterized<PsiTypeTestPattern, PsiType> PATTERN_INSTANCEOF_EQUALS =
parameterized(PsiTypeTestPattern.class, PsiType.class, "pattern.instanceof.equals")
.withAnchor((expr, context) -> expr.getCheckType())
.withRawDescription((expr, context) -> message("pattern.instanceof.equals", context.getPresentableText()));

View File

@@ -324,6 +324,17 @@ final class JavaErrorFixProvider {
sink.accept(QuickFixFactory.getInstance().createDeleteFix(elementsToDelete, text));
}
});
fix(UNSUPPORTED_FEATURE, error -> {
if (error.context() == JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS &&
error.psi() instanceof PsiInstanceOfExpression instanceOfExpression) {
PsiTypeElement element = InstanceOfUtils.findCheckTypeElement(instanceOfExpression);
PsiType operandType = instanceOfExpression.getOperand().getType();
if (element != null && operandType != null && TypeConversionUtil.isPrimitiveAndNotNull(element.getType())) {
return myFactory.createReplacePrimitiveWithBoxedTypeAction(operandType, requireNonNull(element));
}
}
return null;
});
fix(CAST_INCONVERTIBLE, error -> {
if (error.psi() instanceof PsiInstanceOfExpression instanceOfExpression &&
TypeConversionUtil.isPrimitiveAndNotNull(error.context().rType())) {
@@ -332,12 +343,10 @@ final class JavaErrorFixProvider {
}
return null;
});
JavaFixProvider<PsiInstanceOfExpression, Object> redundantInstanceOfFix = error -> {
if (error.psi().getPattern() instanceof PsiTypeTestPattern pattern) {
PsiPatternVariable variable = pattern.getPatternVariable();
if (variable != null && !VariableAccessUtils.variableIsUsed(variable, variable.getDeclarationScope())) {
return new RedundantInstanceofFix(error.psi());
}
JavaFixProvider<PsiTypeTestPattern, Object> redundantInstanceOfFix = error -> {
PsiPatternVariable variable = error.psi().getPatternVariable();
if (variable != null && !VariableAccessUtils.variableIsUsed(variable, variable.getDeclarationScope())) {
return new RedundantInstanceofFix(error.psi().getParent());
}
return null;
};
@@ -392,7 +401,7 @@ final class JavaErrorFixProvider {
error -> error.context() instanceof PsiClass cls ? myFactory.createChangeClassSignatureFromUsageFix(cls, error.psi()) : null);
JavaFixProvider<PsiTypeElement, TypeParameterBoundMismatchContext> addBoundFix = error -> {
if (error.context().bound() instanceof PsiClassType bound) {
PsiClass psiClass = bound.resolve();
PsiClass psiClass = PsiUtil.resolveClassInClassTypeOnly(error.context().actualType());
if (psiClass != null) {
return myFactory.createExtendsListFix(psiClass, bound, true);
}

View File

@@ -3,7 +3,7 @@ class Test {
public static void main(String[] args) {
Box<String> stringBox = new Box<String>("123");
stringBox.transform(new Fn<error descr="Wrong number of type arguments: 1; required: 2"><String,<error descr="Identifier expected"> </error>></error>() {});
stringBox.transform(new <error descr="Class 'Anonymous class derived from Fn' must implement abstract method 'apply(A)' in 'Fn'">Fn<String,<error descr="Identifier expected"> </error>></error>() {});
}

View File

@@ -5,7 +5,7 @@ interface I {
class A implements I {
{
System.out.println(A./*c1*/<error descr="Static method may only be called on its containing interface">foo</error>());
Runnable r = <error descr="Static method may only be called on its containing interface">A/*c2*/::<String>foo;</error>
Runnable r = A/*c2*/::<String><error descr="Static method may only be called on its containing interface">foo</error>;
System.out.println(r);
}
}