[codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()

This patch removes the detection of redundant calls to `StringBuilder#toString` from the `RedundantStringOperationInspection` since there is a more general inspection called `UnnecessaryToStringCallInspection` which handles all the redundant `Object#toString` calls. All the related tests were moved to the test data of `UnnecessaryToStringCallInspectionTest`

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 1fe7723fa4369b797120c5bbc6a6b24947c84a94
This commit is contained in:
Nikita Eshkeev
2020-05-14 22:04:54 +00:00
committed by intellij-monorepo-bot
parent e84c4191d6
commit 0218cec2c9
7 changed files with 42 additions and 46 deletions
@@ -16,8 +16,7 @@ class StringBuilderToString {
/* 1 */
int s5 = new StringBuilder()/* 2 */.substring(1, 4).length();
/* 1 */
System.out.println(new StringBuilder()/* 2 */);
System.out.println(new StringBuilder()./* 1 */toString()/* 2 */);
/* 1 */
System.out.println(new StringBuilder()/* 2 */.substring(1));
/* 1 */
@@ -26,6 +25,8 @@ class StringBuilderToString {
System.out.println(new StringBuilder()/* 2 */.substring(1, 3).length());
System.out.println(new StringBuilder().substring(1, 3));
System.out.println(new StringBuilder().substring(1, 3).length());
"hello".substring(sb.toString());
}
void builder(StringBuilder sb) {
@@ -39,8 +40,7 @@ class StringBuilderToString {
/* 1 */
int s5 = sb/* 2 */.substring(1, 4).length();
/* 1 */
System.out.println(sb/* 2 */);
System.out.println(sb./* 1 */toString()/* 2 */);
/* 1 */
System.out.println(sb/* 2 */.substring(1));
/* 1 */
@@ -18,6 +18,8 @@ class StringBuilderToString {
System.out.println(new StringBuilder()./* 1 */toString()/* 2 */.substring(1, 3).length());
System.out.println(new StringBuilder().substring(1, 3));
System.out.println(new StringBuilder().substring(1, 3).length());
"hello".substring(sb.toString());
}
void builder(StringBuilder sb) {
@@ -1,4 +1,4 @@
// "Fix all 'Redundant String operation' problems in file" "true"
// "Fix all 'Unnecessary call to 'toString()'' problems in file" "true"
class StringBuilderToStringArgs {
@@ -76,13 +76,22 @@ class StringBuilderToStringArgs {
/* 4 */
System.out.println(str() + /* 1 */sb/* 5 */ + str());
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb/* 5 */);
/* 2 */
/* 3 */
/* 4 */
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb/* 5 */ + /* 1 */sb/* 5 */);
System.out.println(("Hello" + sb) + sb + sb);
System.out.println(("Hello" + sb) + sb + sb);
System.out.println((("Hello" + sb) + sb) + sb);
System.out.println(("Hello" + sb + (sb.toString() + ((sb.toString())))));
System.out.println(("Hello" + sb + (sb.toString() + ((sb)))));
System.out.println(("Hello" + sb + (sb + ((sb) + "Hello"))));
System.out.println(("Hello" + sb + (sb + ((sb.toString()) + 42))));
@@ -1,4 +1,4 @@
// "Fix all 'Redundant String operation' problems in file" "true"
// "Fix all 'Unnecessary call to 'toString()'' problems in file" "true"
class StringBuilderToStringAssign {
@@ -75,13 +75,22 @@ class StringBuilderToStringAssign {
/* 4 */
String s83 = str() + /* 1 */sb/* 5 */ + str();
String s91 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s92 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
/* 2 */
/* 3 */
/* 4 */
String s91 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb/* 5 */;
/* 2 */
/* 3 */
/* 4 */
/* 2 */
/* 3 */
/* 4 */
String s92 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + /* 1 */sb/* 5 */ + /* 1 */sb/* 5 */;
String s101 = ("Hello" + sb) + sb + sb;
String s102 = ("Hello" + sb) + sb + sb;
String s103 = (("Hello" + sb) + sb) + sb;
String s104 = ("Hello" + sb + (sb.toString() + ((sb.toString()))));
String s104 = ("Hello" + sb + (sb.toString() + ((sb))));
String s105 = ("Hello" + sb + (sb + ((sb) + "Hello")));
String s106 = ("Hello" + sb + (sb + ((sb.toString()) + 42)));
@@ -1,4 +1,4 @@
// "Fix all 'Redundant String operation' problems in file" "true"
// "Fix all 'Unnecessary call to 'toString()'' problems in file" "true"
class StringBuilderToStringArgs {
@@ -1,4 +1,4 @@
// "Fix all 'Redundant String operation' problems in file" "true"
// "Fix all 'Unnecessary call to 'toString()'' problems in file" "true"
class StringBuilderToStringAssign {
@@ -20,10 +20,12 @@ import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.callMatcher.CallMapper;
import com.siyeh.ig.callMatcher.CallMatcher;
import com.siyeh.ig.psiutils.*;
import org.jetbrains.annotations.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;
import javax.swing.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -277,41 +279,15 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
@Nullable
private ProblemDescriptor getRedundantStringBuilderToStringProblem(@NotNull final PsiMethodCallExpression call) {
if (!ExpressionUtils.isConversionToStringNecessary(call, false)) {
// report naked `new StringBuilder().toString()`
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(call.getParent());
if (parent instanceof PsiPolyadicExpression) {
if (((PsiPolyadicExpression)parent).getOperationTokenType() != JavaTokenType.PLUS) return null;
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
final PsiExpression @NotNull[] operands = polyadicExpression.getOperands();
final boolean hasStringOperand = Arrays.stream(operands)
.filter(operand -> operand != call)
.anyMatch(RedundantStringOperationVisitor::isOperandOfStringType);
if (!hasStringOperand) return null;
}
return getProblem(call, "inspection.redundant.string.call.message");
}
final PsiMethodCallExpression substringCall = PsiTreeUtil.getParentOfType(call, PsiMethodCallExpression.class);
if (!STRING_SUBSTRING.test(substringCall)) return null;
if (substringCall == null) return null;
final PsiExpression qualifier = ExpressionUtils.getEffectiveQualifier(substringCall.getMethodExpression());
if (qualifier != call || !STRING_SUBSTRING.test(substringCall)) return null;
return getProblem(call, "inspection.redundant.string.call.message");
}
private static boolean isOperandOfStringType(@NotNull final PsiExpression operand) {
if (STRING_BUILDER_TO_STRING.matches(operand)) return false;
if (operand.getType() != null && operand.getType().equalsToText(String.class.getName())) {
return true;
}
final String value = tryCast(ExpressionUtils.computeConstantExpression(operand), String.class);
return value != null;
}
@Nullable
private ProblemDescriptor getInternProblem(PsiMethodCallExpression call) {
return PsiUtil.isConstantExpression(call.getMethodExpression().getQualifierExpression())