private static void tryReplaceWithTarget(@NotNull PsiLocalVariable variable,
                                           @NotNull PsiReferenceExpression usage,
                                           PsiElement context,
                                           List<? extends PsiReferenceExpression> references) {
    PsiLocalVariable target = tryCast(PsiUtil.skipParenthesizedExprUp(usage.getParent()), PsiLocalVariable.class);
    if (target == null) return;
    String name = target.getName();
    if (!target.getType().equals(variable.getType())) return;
    PsiDeclarationStatement declaration = tryCast(target.getParent(), PsiDeclarationStatement.class);
    if (declaration == null || declaration.getDeclaredElements().length != 1) return;
    PsiModifierList modifiers = target.getModifierList();
    if (modifiers != null && modifiers.getAnnotations().length != 0) return;
    boolean effectivelyFinal = HighlightControlFlowUtil.isEffectivelyFinal(variable, context, null);
    if (!effectivelyFinal && !VariableAccessUtils.canUseAsNonFinal(target)) return;

    for (PsiReferenceExpression reference : references) {
      ExpressionUtils.bindReferenceTo(reference, name);
    }
    if (effectivelyFinal && target.hasModifierProperty(PsiModifier.FINAL)) {
      PsiModifierList modifierList = variable.getModifierList();
      if (modifierList != null) {
        modifierList.setModifierProperty(PsiModifier.FINAL, true);
      }
    }
    variable.setName(name);
    new CommentTracker().deleteAndRestoreComments(declaration);
  }
private static void tryReplaceWithTarget(@NotNull PsiLocalVariable variable,
                                           @NotNull PsiReferenceExpression usage,
                                           PsiElement context,
                                           List<? extends PsiReferenceExpression> references)