void (PsiElement element) { final PsiElement parent = element.getParent(); if (!(parent instanceof PsiVariable variable)) { if (parent instanceof PsiNewExpression newExpression) { final PsiExpression stringBuilderExpression = getCompleteExpression(newExpression); if (stringBuilderExpression != null) { final CommentTracker tracker = new CommentTracker(); final StringBuilder stringExpression = buildStringExpression(stringBuilderExpression, tracker, new StringBuilder()); if (stringExpression != null) { String replacement = addConversionToStringIfNecessary(stringBuilderExpression, stringExpression.toString()); tracker.replaceExpressionAndRestoreComments(stringBuilderExpression, replacement); } } } return; } final String variableName = variable.getName(); if (variableName == null) { return; } final PsiTypeElement originalTypeElement = variable.getTypeElement(); if (originalTypeElement == null) { return; } final PsiExpression initializer = PsiUtil.skipParenthesizedExprDown(variable.getInitializer()); if (initializer == null) { return; } final CommentTracker tracker = new CommentTracker(); final StringBuilder builder; if (isAppendCall(initializer)) { builder = buildStringExpression(initializer, tracker, new StringBuilder()); if (builder == null) { return; } } else if (initializer instanceof PsiNewExpression newExpression) { final PsiExpressionList argumentList = newExpression.getArgumentList(); if (argumentList == null) { return; } final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 0 || TypeUtils.typeEquals(STRING_JOINER, newExpression.getType())) { builder = new StringBuilder(); } else { final PsiExpression argument = arguments[0]; if (PsiTypes.intType().equals(argument.getType())) { builder = new StringBuilder(); } else if (ParenthesesUtils.getPrecedence(argument) > ParenthesesUtils.ADDITIVE_PRECEDENCE) { builder = new StringBuilder("(").append(tracker.text(argument)).append(')'); } else { builder = new StringBuilder(tracker.text(argument)); } } } else { return; } final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); if (codeBlock == null) { return; } final StringExpressionCollector visitor = new StringExpressionCollector(variable); codeBlock.accept(visitor); final List<PsiExpression> expressions = visitor.getExpressions(); for (PsiExpression expression : expressions) { if (expression == null || buildStringExpression(expression, tracker, builder) == null) { return; } } final String expressionText = addConversionToStringIfNecessary(element, builder.toString()); final List<PsiElement> toDelete = new SmartList<>(); toDelete.add(variable); for (int i = 0, size = expressions.size() - 1; i < size; i++) { toDelete.add(expressions.get(i).getParent()); } final PsiExpression lastExpression = expressions.get(expressions.size() - 1); final boolean useVariable = myPossibleSideEffect || expressionText.contains("\n") && !isVariableInitializer(lastExpression); if (useVariable) { final PsiElement last = toDelete.remove(toDelete.size() - 1); final PsiStatement statement = PsiTreeUtil.getNonStrictParentOfType(last, PsiStatement.class); if (statement == null) { return; } final @NonNls String modifier = JavaCodeStyleSettings.getInstance(lastExpression.getContainingFile()).GENERATE_FINAL_LOCALS ? "final " : ""; toDelete.forEach(tracker::delete); final String statementText = modifier + CommonClassNames.JAVA_LANG_STRING + ' ' + variableName + "=" + expressionText + ';'; tracker.replaceAndRestoreComments(statement, statementText); PsiReplacementUtil.replaceExpression(lastExpression, variableName); } else { tracker.replaceExpressionAndRestoreComments(lastExpression, expressionText, toDelete); } }