boolean (PsiExpression boxingExpression, PsiExpression boxedExpression) { if (ExpressionUtils.isVoidContext(boxingExpression)) { // removing the boxing in this case will make the code uncompilable return true; } PsiElement parent = boxingExpression.getParent(); while (parent instanceof PsiParenthesizedExpression) { boxingExpression = (PsiExpression)parent; parent = parent.getParent(); } if (parent instanceof PsiReferenceExpression || parent instanceof PsiSynchronizedStatement) { return true; } else if (parent instanceof PsiVariable) { PsiTypeElement typeElement = ((PsiVariable)parent).getTypeElement(); // Inferred type may change if boxing is removed; if it's possible if (typeElement != null && typeElement.isInferredType()) return true; } else if (parent instanceof PsiTypeCastExpression castExpression) { return TypeUtils.isTypeParameter(castExpression.getType()); } else if (parent instanceof PsiConditionalExpression conditionalExpression) { final PsiExpression thenExpression = conditionalExpression.getThenExpression(); final PsiExpression elseExpression = conditionalExpression.getElseExpression(); if (elseExpression == null || thenExpression == null) { return true; } if (PsiTreeUtil.isAncestor(thenExpression, boxingExpression, false)) { final PsiType type = elseExpression.getType(); return !(type instanceof PsiPrimitiveType); } else if (PsiTreeUtil.isAncestor(elseExpression, boxingExpression, false)) { final PsiType type = thenExpression.getType(); return !(type instanceof PsiPrimitiveType); } else { return false; } } else if (parent instanceof PsiPolyadicExpression polyadicExpression) { return isPossibleObjectComparison(boxingExpression, polyadicExpression); } return MethodCallUtils.isNecessaryForSurroundingMethodCall(boxingExpression, boxedExpression) || !LambdaUtil.isSafeLambdaReturnValueReplacement(boxingExpression, boxedExpression); }