void (@NotNull PsiTypeCastExpression cast) { PsiTypeElement castTypeElement = cast.getCastType(); if (castTypeElement == null || castTypeElement.getAnnotations().length > 0) return; PsiType castType = GenericsUtil.getVariableTypeByExpressionType(cast.getType()); if (!(castType instanceof PsiClassType) || ((PsiClassType)castType).isRaw()) return; PsiReferenceExpression ref = tryCast(PsiUtil.skipParenthesizedExprDown(cast.getOperand()), PsiReferenceExpression.class); if (ref == null) return; PsiVariable variable = tryCast(ref.resolve(), PsiVariable.class); if (!PsiUtil.isJvmLocalVariable(variable)) return; PsiForeachStatement forEach = null; if (variable instanceof PsiParameter) { forEach = tryCast(((PsiParameter)variable).getDeclarationScope(), PsiForeachStatement.class); if (forEach == null) return; } PsiTypeElement variableTypeElement = variable.getTypeElement(); if (variableTypeElement == null || variableTypeElement.isInferredType() || variableTypeElement.getAnnotations().length > 0) return; PsiType variableType = variable.getType(); if (!(variableType instanceof PsiClassType) || ((PsiClassType)variableType).isRaw()) return; if (variableType.equals(castType) || !variableType.isAssignableFrom(castType)) return; if (forEach != null) { PsiExpression collection = forEach.getIteratedValue(); if (collection == null) return; PsiType elementType = JavaGenericsUtil.getCollectionItemType(collection); if (elementType == null || !castType.isAssignableFrom(elementType)) return; PsiType elementVarType = GenericsUtil.getVariableTypeByExpressionType(elementType); if (elementVarType instanceof PsiClassType && ((PsiClassType)elementVarType).isRaw()) return; } else { PsiExpression variableInitializer = variable.getInitializer(); if (variableInitializer != null) { PsiType initializerType = GenericsUtil.getVariableTypeByExpressionType(variableInitializer.getType()); if (initializerType == null || !castType.isAssignableFrom(initializerType)) return; } } PsiElement block = PsiUtil.getVariableCodeBlock(variable, null); if (block == null) return; for (PsiReferenceExpression reference : VariableAccessUtils.getVariableReferences(variable, block)) { if (!isVariableTypeChangeSafeForReference(cast, castType, reference)) return; } String message = JavaBundle .message("inspection.cast.can.be.removed.narrowing.variable.type.message", variable.getName(), castType.getPresentableText()); holder.registerProblem(castTypeElement, message, new CastCanBeRemovedNarrowingVariableTypeFix(variable, castType, isOnTheFly)); }