void (@NotNull PsiLocalVariable variable) { PsiIdentifier identifier = variable.getNameIdentifier(); if (identifier == null) return; PsiTypeCastExpression cast = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(variable.getInitializer()), PsiTypeCastExpression.class); if (cast == null || cast.getCastType() == null) return; PsiExpression operand = cast.getOperand(); if (operand == null) return; PsiType castType = cast.getCastType().getType(); if (castType instanceof PsiPrimitiveType) return; if (!variable.getType().equals(castType)) return; PsiType operandType = operand.getType(); if (operandType == null || castType.isAssignableFrom(operandType)) return; PsiElement scope = PsiUtil.getVariableCodeBlock(variable, null); if (scope == null) return; PsiDeclarationStatement declaration = ObjectUtils.tryCast(variable.getParent(), PsiDeclarationStatement.class); if (declaration == null) return; PsiInstanceOfExpression instanceOf = InstanceOfUtils.findPatternCandidate(cast); if (instanceOf != null) { PsiPattern pattern = instanceOf.getPattern(); PsiPatternVariable existingPatternVariable = JavaPsiPatternUtil.getPatternVariable(pattern); //it is a deconstruction pattern and we can't add new variable here if (pattern != null && existingPatternVariable == null) { return; } String name = identifier.getText(); if (existingPatternVariable != null) { if (!canReplaceLocalVariableWithPatternVariable(variable, existingPatternVariable) || !isFinalOrEffectivelyFinal(existingPatternVariable)) { return; } holder.registerProblem(identifier, InspectionGadgetsBundle.message("inspection.pattern.variable.can.be.used.existing.message", existingPatternVariable.getName(), name), new ExistingPatternVariableCanBeUsedFix(name, existingPatternVariable)); } else { if (!isOnTheFly && InstanceOfUtils.hasConflictingDeclaredNames(variable, instanceOf)) { return; } holder.registerProblem(identifier, InspectionGadgetsBundle.message("inspection.pattern.variable.can.be.used.message", name), new PatternVariableCanBeUsedFix(name, instanceOf)); } } }