boolean (@NotNull PsiCaseLabelElement label, @NotNull Consumer<? super HighlightInfo.Builder> errorSink) { if (label instanceof PsiDefaultCaseLabelElement) return false; if (!(label instanceof PsiParenthesizedExpression) && isNullType(label)) { if (mySelectorType instanceof PsiPrimitiveType && !isNullType(mySelector)) { HighlightInfo.Builder error = createError(label, JavaErrorBundle.message("incompatible.switch.null.type", "null", JavaHighlightUtil.formatType(mySelectorType))); errorSink.accept(error); return true; } return false; } if (label instanceof PsiPattern) { PsiPattern elementToReport = JavaPsiPatternUtil.getTypedPattern(label); if (elementToReport == null) return false; PsiTypeElement typeElement = JavaPsiPatternUtil.getPatternTypeElement(elementToReport); if (typeElement == null) return false; PsiType patternType = typeElement.getType(); if (!(patternType instanceof PsiClassType) && !(patternType instanceof PsiArrayType)) { String expectedTypes = JavaErrorBundle.message("switch.class.or.array.type.expected"); String message = JavaErrorBundle.message("unexpected.type", expectedTypes, JavaHighlightUtil.formatType(patternType)); HighlightInfo.Builder info = createError(elementToReport, message); PsiPrimitiveType primitiveType = ObjectUtils.tryCast(patternType, PsiPrimitiveType.class); if (primitiveType != null) { IntentionAction fix = getFixFactory().createReplacePrimitiveWithBoxedTypeAction(mySelectorType, typeElement); if (fix != null) { info.registerFix(fix, null, null, null, null); } } errorSink.accept(info); return true; } if ((!ContainerUtil.and(getAllTypes(mySelectorType), type -> TypeConversionUtil.areTypesConvertible(type, patternType)) || // 14.30.3 A type pattern that declares a pattern variable of a reference type U is // applicable at another reference type T if T is checkcast convertible to U (JEP 440-441) // There is no rule that says that a reference type applies to a primitive type (mySelectorType instanceof PsiPrimitiveType && HighlightingFeature.PATTERN_GUARDS_AND_RECORD_PATTERNS.isAvailable(label))) && //null type is applicable to any class type !mySelectorType.equals(PsiTypes.nullType())) { HighlightInfo.Builder error = HighlightUtil.createIncompatibleTypeHighlightInfo(mySelectorType, patternType, elementToReport.getTextRange(), 0); errorSink.accept(error); return true; } HighlightInfo.Builder error = getUncheckedPatternConversionError(elementToReport); if (error != null) { errorSink.accept(error); return true; } PsiDeconstructionPattern deconstructionPattern = JavaPsiPatternUtil.findDeconstructionPattern(elementToReport); return createDeconstructionErrors(deconstructionPattern, errorSink); } else if (label instanceof PsiExpression expr) { if (mySelectorType.equals(PsiTypes.nullType())) { HighlightInfo.Builder info = HighlightUtil.createIncompatibleTypeHighlightInfo(mySelectorType, expr.getType(), expr.getTextRange(), 0); errorSink.accept(info); return true; } HighlightInfo.Builder info = HighlightUtil.checkAssignability(mySelectorType, expr.getType(), expr, expr); if (info != null) { errorSink.accept(info); return true; } if (label instanceof PsiReferenceExpression ref) { String enumConstName = evaluateEnumConstantName(ref); if (enumConstName != null) { HighlightInfo.Builder error = createQualifiedEnumConstantInfo(ref); if (error != null) { errorSink.accept(error); return true; } return false; } } Object constValue = evaluateConstant(expr); if (constValue == null) { HighlightInfo.Builder error = createError(expr, JavaErrorBundle.message("constant.expression.required")); errorSink.accept(error); return true; } if (ConstantExpressionUtil.computeCastTo(constValue, mySelectorType) == null) { HighlightInfo.Builder error = HighlightUtil.createIncompatibleTypeHighlightInfo(mySelectorType, expr.getType(), label.getTextRange(), 0); errorSink.accept(error); return true; } SelectorKind kind = getSwitchSelectorKind(); if (kind == SelectorKind.INT || kind == SelectorKind.STRING) { return false; } HighlightInfo.Builder infoIncompatibleTypes = createError(expr, JavaErrorBundle.message("switch.pattern.expected", JavaHighlightUtil.formatType(mySelectorType))); errorSink.accept(infoIncompatibleTypes); return true; } HighlightInfo.Builder error = createError(label, JavaErrorBundle.message("switch.constant.expression.required")); errorSink.accept(error); return true; }