public static boolean isSafeToNarrowType(@NotNull PsiVariable variable, @NotNull PsiTypeCastExpression cast, @NotNull PsiType type) {
    PsiType castType = GenericsUtil.getVariableTypeByExpressionType(type);
    PsiTypeElement varTypeElement = variable.getTypeElement();
    if (varTypeElement == null || varTypeElement.isInferredType() || varTypeElement.getAnnotations().length > 0) return false;
    PsiType variableType = varTypeElement.getType();
    if (!(variableType instanceof PsiClassType classType) || classType.isRaw()) return false;
    if (variableType.equals(castType) || !variableType.isAssignableFrom(castType)) return false;
    for (PsiReferenceExpression reference : VariableAccessUtils.getVariableReferences(variable)) {
      if (PsiTreeUtil.isAncestor(cast, reference, true)) continue;
      if (!VariableAccessUtils.isVariableTypeChangeSafeForReference(castType, reference)) return false;
    }
    return true;
  }
public static boolean isSafeToNarrowType(@NotNull PsiVariable variable, @NotNull PsiTypeCastExpression cast, @NotNull PsiType type)