boolean (PsiTypeCastExpression typeCast) { PsiExpression operand = typeCast.getOperand(); if (operand == null) return false; if (isInPolymorphicCall(typeCast)) return true; PsiType opType = operand.getType(); PsiTypeElement typeElement = typeCast.getCastType(); if (typeElement == null) return false; PsiType castType = typeElement.getType(); if (castType instanceof PsiPrimitiveType) { if (opType instanceof PsiPrimitiveType) { PsiElement parent = PsiUtil.skipParenthesizedExprUp(typeCast.getParent()); if ((parent instanceof PsiReturnStatement || parent instanceof PsiExpressionList || parent instanceof PsiVariable || parent instanceof PsiAssignmentExpression || parent instanceof PsiArrayAccessExpression && PsiTreeUtil.isAncestor(((PsiArrayAccessExpression)parent).getIndexExpression(), typeCast, false)) && castType.equals(ExpectedTypeUtils.findExpectedType(typeCast, false))) { return !TypeConversionUtil.isSafeConversion(castType, opType); // let's suppose that casts losing precision are important } else { return !castType.equals(opType); // cast might be necessary (e.g. ((double)1)/5) } } final PsiPrimitiveType unboxedOpType = PsiPrimitiveType.getUnboxedType(opType); if (unboxedOpType != null && !unboxedOpType.equals(castType) ) { return true; } } else if (castType instanceof PsiClassType && ((PsiClassType)castType).hasParameters()) { if (opType instanceof PsiClassType && ((PsiClassType)opType).isRaw()) return true; } PsiElement parent = PsiUtil.skipParenthesizedExprUp(typeCast.getParent()); final PsiExpression stripParenthesisOperand = PsiUtil.skipParenthesizedExprDown(operand); if (stripParenthesisOperand instanceof PsiFunctionalExpression) { if (isCastToSerializable(castType) && !isCastToSerializable(PsiTypesUtil.getExpectedTypeByParent(typeCast))) return true; } else if (stripParenthesisOperand instanceof PsiConditionalExpression) { if (PsiUtil.skipParenthesizedExprDown(((PsiConditionalExpression)stripParenthesisOperand).getThenExpression()) instanceof PsiFunctionalExpression || PsiUtil.skipParenthesizedExprDown(((PsiConditionalExpression)stripParenthesisOperand).getElseExpression()) instanceof PsiFunctionalExpression) { return true; } } if (parent instanceof PsiBinaryExpression expression) { PsiExpression firstOperand = expression.getLOperand(); PsiExpression otherOperand = expression.getROperand(); if (PsiTreeUtil.isAncestor(otherOperand, typeCast, false)) { PsiExpression temp = otherOperand; otherOperand = firstOperand; firstOperand = temp; } if (otherOperand != null && wrapperCastChangeSemantics(firstOperand, otherOperand, operand)) { return true; } } else if (parent instanceof PsiConditionalExpression) { if (opType instanceof PsiPrimitiveType && !(((PsiConditionalExpression)parent).getType() instanceof PsiPrimitiveType)) { if (PsiPrimitiveType.getUnboxedType(PsiTypesUtil.getExpectedTypeByParent(parent)) != null) { return true; } } } else if (parent instanceof PsiLocalVariable) { return ((PsiLocalVariable)parent).getTypeElement().isInferredType(); } return false; }