[java-inspections] StringConcatenationInLoops: the quick-fix produces incorrect result

IDEA-306289

GitOrigin-RevId: 89cc6c4a394107e2c9eb8995915f28583a607231
This commit is contained in:
Andrey Cherkasov
2022-11-28 16:40:39 +04:00
committed by intellij-monorepo-bot
parent a1c599bdce
commit 7e0d6a7f1a
5 changed files with 90 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.util;
import com.intellij.psi.*;
@@ -29,34 +29,35 @@ public final class ChangeToAppendUtil {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)concatenation;
final PsiExpression[] operands = polyadicExpression.getOperands();
boolean isConstant = true;
boolean isPrimitiveOrBoxed = true;
boolean isString = false;
final StringBuilder builder = new StringBuilder();
for (PsiExpression operand : operands) {
if (isConstant && PsiUtil.isConstantExpression(operand)) {
if (builder.length() != 0) {
final PsiType operandType = operand.getType();
isConstant &= PsiUtil.isConstantExpression(operand);
isPrimitiveOrBoxed &= operandType instanceof PsiPrimitiveType || PsiPrimitiveType.getUnboxedType(operandType) != null;
if (isConstant || !isString && isPrimitiveOrBoxed) {
if (!builder.isEmpty()) {
builder.append('+');
}
final PsiType operandType = operand.getType();
if (operandType != null && operandType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
isString = true;
}
builder.append(CommentTracker.textWithSurroundingComments(operand));
}
else if (!operand.textMatches("\"\"")) {
isConstant = false;
if (builder.length() != 0) {
if (!builder.isEmpty()) {
append(builder, useStringValueOf && !isString, out);
builder.setLength(0);
}
buildAppendExpression(operand, useStringValueOf, out);
}
}
if (builder.length() != 0) {
if (!builder.isEmpty()) {
append(builder, false, out);
}
}
else if (concatenation instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)concatenation;
else if (concatenation instanceof PsiParenthesizedExpression parenthesizedExpression) {
final PsiExpression expression = parenthesizedExpression.getExpression();
if (expression != null) {
return buildAppendExpression(expression, useStringValueOf, out);

View File

@@ -0,0 +1,20 @@
// "Fix all 'String concatenation in loop' problems in file" "true"
public class Main {
void test(String[] strings) {
StringBuilder result1 = new StringBuilder();
StringBuilder result2 = new StringBuilder();
StringBuilder result3 = new StringBuilder();
StringBuilder result4 = new StringBuilder();
StringBuilder result5 = new StringBuilder();
StringBuilder result6 = new StringBuilder();
for (Integer i = 0; i < strings.length; i++) {
result1.append(i + 1).append(" item: ").append(strings[i]).append(" ");
result2.append(1 + i).append(" item: ").append(strings[i]).append(" ");
result3.append(i).append(" item: ").append(1).append(strings[i]).append(" ");
result4.append(" item: ").append(i).append(1).append(strings[i]).append(" ");
result5.append(" item: " + 1).append(i).append(strings[i]).append(" ");
result6.append(1 + " item: ").append(i).append(strings[i]).append(" ");
}
}
}

View File

@@ -0,0 +1,20 @@
// "Fix all 'String concatenation in loop' problems in file" "true"
public class Main {
void test(String[] strings) {
StringBuilder result1 = new StringBuilder();
StringBuilder result2 = new StringBuilder();
StringBuilder result3 = new StringBuilder();
StringBuilder result4 = new StringBuilder();
StringBuilder result5 = new StringBuilder();
StringBuilder result6 = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
result1.append(i + 1).append(" item: ").append(strings[i]).append(" ");
result2.append(1 + i).append(" item: ").append(strings[i]).append(" ");
result3.append(i).append(" item: ").append(1).append(strings[i]).append(" ");
result4.append(" item: ").append(i).append(1).append(strings[i]).append(" ");
result5.append(" item: " + 1).append(i).append(strings[i]).append(" ");
result6.append(1 + " item: ").append(i).append(strings[i]).append(" ");
}
}
}

View File

@@ -0,0 +1,20 @@
// "Fix all 'String concatenation in loop' problems in file" "true"
public class Main {
void test(String[] strings) {
String result1 = "";
String result2 = "";
String result3 = "";
String result4 = "";
String result5 = "";
String result6 = "";
for (Integer i = 0; i < strings.length; i++) {
result1 +=<caret> i + 1 + " item: " + strings[i] + " ";
result2 += 1 + i + " item: " + strings[i] + " ";
result3 += i + " item: " + 1 + strings[i] + " ";
result4 += " item: " + i + 1 + strings[i] + " ";
result5 += " item: " + 1 + i + strings[i] + " ";
result6 += 1 + " item: " + i + strings[i] + " ";
}
}
}

View File

@@ -0,0 +1,20 @@
// "Fix all 'String concatenation in loop' problems in file" "true"
public class Main {
void test(String[] strings) {
String result1 = "";
String result2 = "";
String result3 = "";
String result4 = "";
String result5 = "";
String result6 = "";
for (int i = 0; i < strings.length; i++) {
result1 +=<caret> i + 1 + " item: " + strings[i] + " ";
result2 += 1 + i + " item: " + strings[i] + " ";
result3 += i + " item: " + 1 + strings[i] + " ";
result4 += " item: " + i + 1 + strings[i] + " ";
result5 += " item: " + 1 + i + strings[i] + " ";
result6 += 1 + " item: " + i + strings[i] + " ";
}
}
}