Files
openide/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantStringOperation/beforeStringBuilderToStringArgs.java
Nikita Eshkeev cb47b00f60 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch alters the `getRedundantStringBuilderToStringProblem` method with more sophisticated algorithm to detect unnecessary freestanding `StringBuilder#toString` calls. If the call is a part of a polyadic expression it checks if there is at least one string operand (either a constant literal or a local variable or a constant variable or a method call) and based on that information it detects redundant `StringBuilder#toString` calls.

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

GitOrigin-RevId: e09ea8b8829ad9e28e8ff1bcfad34cf6b191aa6b
2020-05-12 00:59:21 +00:00

50 lines
2.7 KiB
Java

// "Fix all 'Redundant String operation' problems in file" "true"
class StringBuilderToStringArgs {
private static final String CONST_STRING_VAL = "Hello";
static String str() { return ""; }
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))));
}
}