[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 23:06:56 +03:00
committed by intellij-monorepo-bot
parent e84c4191d6
commit 0218cec2c9
7 changed files with 42 additions and 46 deletions

View File

@@ -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 */

View File

@@ -1,108 +0,0 @@
// "Fix all 'Redundant String operation' problems in file" "true"
class StringBuilderToStringArgs {
private static final String CONST_STRING_VAL = "Hello";
static String str() { return ""; }
static <T> T gen(T a) { return a; }
static void stringBuilderToStringArgs(StringBuilder sb) {
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb/* 5 */ + "Hello");
/* 2 */
/* 3 */
/* 4 */
System.out.println("Hello" + /* 1 */sb/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
/* 2 */
/* 3 */
/* 4 */
System.out.println("Hello" + /* 1 */sb/* 5 */ + 42);
final String finalVal = "Hello";
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb/* 5 */ + finalVal);
/* 2 */
/* 3 */
/* 4 */
System.out.println(finalVal + /* 1 */sb/* 5 */);
/* 2 */
/* 3 */
/* 4 */
System.out.println(finalVal + /* 1 */sb/* 5 */ + 42);
String stringVal = "Hello";
/* 2 */
/* 3 */
/* 4 */
System.out.println(stringVal + /* 1 */sb/* 5 */ + 42);
int intVal = 42;
System.out.println(intVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
final int constIntVal = 42;
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + constIntVal);
System.out.println(constIntVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(constIntVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
/* 2 */
/* 3 */
/* 4 */
System.out.println(CONST_STRING_VAL + /* 1 */sb/* 5 */);
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb/* 5 */ + CONST_STRING_VAL);
/* 2 */
/* 3 */
/* 4 */
System.out.println(CONST_STRING_VAL + /* 1 */sb/* 5 */ + 42);
/* 2 */
/* 3 */
/* 4 */
System.out.println(str() + /* 1 */sb/* 5 */);
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb/* 5 */ + str());
/* 2 */
/* 3 */
/* 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 */);
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 + ((sb) + "Hello"))));
System.out.println(("Hello" + sb + (sb + ((sb.toString()) + 42))));
System.out.println((("Hello")) + sb + (sb + ((sb.toString()) + 42)));
/* 2 */
/* 3 */
/* 4 */
System.out.println(gen("Hello") + /* 1 */sb/* 5 */);
/* 2 */
/* 3 */
/* 4 */
System.out.println(/* 1 */sb/* 5 */ + gen("Hello"));
/* 2 */
/* 3 */
/* 4 */
System.out.println(gen("Hello") + /* 1 */sb/* 5 */ + gen("Hello"));
System.out.println(gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42));
System.out.println(gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42));
}
}

View File

@@ -1,107 +0,0 @@
// "Fix all 'Redundant String operation' problems in file" "true"
class StringBuilderToStringAssign {
public static final String CONST_STRING_VAL = "Hello";
static String str() { return ""; }
static <T> T gen(T a) { return a; }
static void stringBuilderToStringAssign(StringBuilder sb) {
String s1 = sb + "Hello";
/* 2 */
/* 3 */
/* 4 */
String s2 = "Hello" + /* 1 */sb/* 5 */;
String s3 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
/* 2 */
/* 3 */
/* 4 */
String s4 = "Hello" + /* 1 */sb/* 5 */ + 42;
final String finalVal = "Hello";
/* 2 */
/* 3 */
/* 4 */
String s5 = finalVal + /* 1 */sb/* 5 */ + 42;
String stringVal = "Hello";
/* 2 */
/* 3 */
/* 4 */
String s611 = /* 1 */sb/* 5 */ + stringVal;
/* 2 */
/* 3 */
/* 4 */
String s621 = stringVal + /* 1 */sb/* 5 */;
/* 2 */
/* 3 */
/* 4 */
String s631 = stringVal + /* 1 */sb/* 5 */ + 42;
int intVal = 42;
String s612 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + intVal;
String s622 = intVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s632 = intVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
final int constIntVal = 42;
String s61 = constIntVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s62 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + constIntVal;
String s63 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + constIntVal + 42;
/* 2 */
/* 3 */
/* 4 */
String s71 = CONST_STRING_VAL + /* 1 */sb/* 5 */;
/* 2 */
/* 3 */
/* 4 */
String s72 = /* 1 */sb/* 5 */ + CONST_STRING_VAL;
/* 2 */
/* 3 */
/* 4 */
String s73 = CONST_STRING_VAL + /* 1 */sb/* 5 */ + 42;
/* 2 */
/* 3 */
/* 4 */
String s81 = str() + /* 1 */sb/* 5 */;
/* 2 */
/* 3 */
/* 4 */
String s82 = /* 1 */sb/* 5 */ + str();
/* 2 */
/* 3 */
/* 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 */;
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 s105 = ("Hello" + sb + (sb + ((sb) + "Hello")));
String s106 = ("Hello" + sb + (sb + ((sb.toString()) + 42)));
String s107 = ((("Hello")) + sb + (sb + ((sb.toString()) + 42)));
/* 2 */
/* 3 */
/* 4 */
String s181 = gen("Hello") + /* 1 */sb/* 5 */;
/* 2 */
/* 3 */
/* 4 */
String s182 = /* 1 */sb/* 5 */ + gen("Hello");
/* 2 */
/* 3 */
/* 4 */
String s183 = gen("Hello") + /* 1 */sb/* 5 */ + gen("Hello");
String s191 = gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s192 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42);
String s193 = gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42);
}
}

View File

@@ -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) {

View File

@@ -1,60 +0,0 @@
// "Fix all 'Redundant String operation' problems in file" "true"
class StringBuilderToStringArgs {
private static final String CONST_STRING_VAL = "Hello";
static String str() { return ""; }
static <T> T gen(T a) { return a; }
static void stringBuilderToStringArgs(StringBuilder sb) {
System.out.println(/* 1 */sb./* 2 */to<caret>String/* 3 */(/* 4 */)/* 5 */ + "Hello");
System.out.println("Hello" + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
System.out.println("Hello" + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
final String finalVal = "Hello";
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + finalVal);
System.out.println(finalVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(finalVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
String stringVal = "Hello";
System.out.println(stringVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
int intVal = 42;
System.out.println(intVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
final int constIntVal = 42;
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + constIntVal);
System.out.println(constIntVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(constIntVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
System.out.println(CONST_STRING_VAL + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + CONST_STRING_VAL);
System.out.println(CONST_STRING_VAL + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42);
System.out.println(str() + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + str());
System.out.println(str() + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 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 */);
System.out.println(("Hello" + sb.toString()) + sb + sb.toString());
System.out.println(("Hello" + sb.toString()) + sb.toString() + sb.toString());
System.out.println((("Hello" + sb.toString()) + sb.toString()) + sb.toString());
System.out.println(("Hello" + sb.toString() + (sb.toString() + ((sb.toString())))));
System.out.println(("Hello" + sb.toString() + (sb.toString() + ((sb.toString()) + "Hello"))));
System.out.println(("Hello" + sb.toString() + (sb.toString() + ((sb.toString()) + 42))));
System.out.println((("Hello")) + sb.toString() + (sb.toString() + ((sb.toString()) + 42)));
System.out.println(gen("Hello") + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen("Hello"));
System.out.println(gen("Hello") + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen("Hello"));
System.out.println(gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */);
System.out.println(/* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42));
System.out.println(gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42));
}
}

View File

@@ -1,62 +0,0 @@
// "Fix all 'Redundant String operation' problems in file" "true"
class StringBuilderToStringAssign {
public static final String CONST_STRING_VAL = "Hello";
static String str() { return ""; }
static <T> T gen(T a) { return a; }
static void stringBuilderToStringAssign(StringBuilder sb) {
String s1 = sb.<caret>toString() + "Hello";
String s2 = "Hello" + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s3 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
String s4 = "Hello" + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
final String finalVal = "Hello";
String s5 = finalVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
String stringVal = "Hello";
String s611 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + stringVal;
String s621 = stringVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s631 = stringVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
int intVal = 42;
String s612 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + intVal;
String s622 = intVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s632 = intVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
final int constIntVal = 42;
String s61 = constIntVal + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s62 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + constIntVal;
String s63 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + constIntVal + 42;
String s71 = CONST_STRING_VAL + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s72 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + CONST_STRING_VAL;
String s73 = CONST_STRING_VAL + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + 42;
String s81 = str() + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s82 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + str();
String s83 = str() + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 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 */;
String s101 = ("Hello" + sb.toString()) + sb + sb.toString();
String s102 = ("Hello" + sb.toString()) + sb.toString() + sb.toString();
String s103 = (("Hello" + sb.toString()) + sb.toString()) + sb.toString();
String s104 = ("Hello" + sb.toString() + (sb.toString() + ((sb.toString()))));
String s105 = ("Hello" + sb.toString() + (sb.toString() + ((sb.toString()) + "Hello")));
String s106 = ("Hello" + sb.toString() + (sb.toString() + ((sb.toString()) + 42)));
String s107 = ((("Hello")) + sb.toString() + (sb.toString() + ((sb.toString()) + 42)));
String s181 = gen("Hello") + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s182 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen("Hello");
String s183 = gen("Hello") + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen("Hello");
String s191 = gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */;
String s192 = /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42);
String s193 = gen(42) + /* 1 */sb./* 2 */toString/* 3 */(/* 4 */)/* 5 */ + gen(42);
}
}