[codeInsight] IDEA-113640 Provide intention to combine System.out.println(String.format(...)) into System.out.printf

The RedundantStringFormatCallInspection inspection used to be able to
detect excessive String.format calls and get rid of them not changing
the callsite at all. This patch enhances the inspection's capabilities
with changing the callsite if it is either PrintStream#print or
PrintStream#println and converting it to PrintStream#printf adding "%n"
if necessary.

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

GitOrigin-RevId: 7edc5b0a84fb6c7b9caf504b1afb8905c5684985
This commit is contained in:
Nikita Eshkeev
2020-05-01 02:19:06 +03:00
committed by intellij-monorepo-bot
parent a2b594c60d
commit d605adb8d1
13 changed files with 655 additions and 160 deletions

View File

@@ -0,0 +1,52 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
import java.util.Locale;
class Main {
static {
System.out.printf("%s, %s!", "Hello", "World");
System.out.printf(
/* condition start */ true /* condition end */
? /* first leg start */ "%s, %s!" /* first leg end */
: /* second leg start */ "%s: %s" /* second leg end */,
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */);
System.out.printf(/* one */ Locale.US, /* two */ "%s, %s!" /* three */, /* four */ "Hello" /* five */, /* six */ "World" /* seven */);
}
Main() {
System.out.printf("%s, World!", "Hello");
System.out.printf("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.print("========");
}
void f() {
System.out.printf("%s, World!", "Hello");
System.out.printf("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.print("========");
}
void out(PrintStream printer) {
printer.printf("%s, World!", "Hello");
printer.printf("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ Locale.US, /* two */ "%s, %s!" /* three */, /* four */ "Hello" /* five */, /* six */ "World" /* seven */);
printer.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.print("========");
}
void caller() {
print(String.format("%s, %s!", "Hello", "World"));
}
static void print(String value) {}
}

View File

@@ -0,0 +1,28 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
class Main {
static {
System.out.print(/* begin */ "Hello, World!"/* end */);
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
Main() {
System.out.print(/* begin */ "Hello, World!"/* end */);
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
void f() {
System.out.print(/* begin */ "Hello, World!"/* end */);
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
void out(PrintStream printer) {
printer.print(/* begin */ "Hello, World!"/* end */);
printer.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
void caller() {
print(/* begin */ "Hello, World!"/* end */);
print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
static void print(String value) {}
}

View File

@@ -0,0 +1,38 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
import java.util.Locale;
import static java.lang.String.format;
class Main {
static {
String s1 = "test";
String s1n = format("test%n");
String s2 = "test";
String s2n = format(Locale.US, "test%n");
String s3 = "test";
String s3l = "test";
String s3n = String.format(Locale.US, "test%n");
System.out.println(/* one */ /* two */ /* three */ "hello, " /* four */);
}
Main() {
String s1 = "test";
String s1n = format("test%n");
String s2 = "test";
String s2n = format(Locale.US, "test%n");
String s3 = "test";
String s3l = "test";
String s3n = String.format(Locale.US, "test%n");
}
void f() {
String s1 = "test";
String s1n = format("test%n");
String s2 = "test";
String s2n = format(Locale.US, "test%n");
String s3 = "test";
String s3l = "test";
String s3n = String.format(Locale.US, "test%n");
}
}

View File

@@ -0,0 +1,52 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
class Main {
static {
System.out.printf("%s, %s!%n", "Hello", "World");
System.out.printf(
/* condition start */ (false /* condition end */
? /* first leg start */ "%s, %s!" /* first leg end */
: /* second leg start */ "%s: %s") + "%n" /* second leg end */,
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */);
}
Main() {
System.out.printf("Hello, World!%n%n");
System.out.printf("%s, World!%n", "Hello");
System.out.printf("%s, %s!%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + "5%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7) + "%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.println("========");
}
void f() {
System.out.printf("Hello, World!%n%n");
System.out.printf("%s, World!%n", "Hello");
System.out.printf("%s, %s!%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + "5%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7) + "%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.println("========");
}
void out(PrintStream printer) {
printer.printf("Hello, World!%n%n");
printer.printf("%s, World!%n", "Hello");
printer.printf("%s, %s!%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ "%s," + /* two */ " %s!%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ "%s," + /* two */ " %s!" + "5%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7) + "%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.println("========");
}
void caller() {
println(String.format("%s, %s!", "Hello", "World"));
}
static void println(String value) {}
}

View File

@@ -0,0 +1,52 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
import java.util.Locale;
class Main {
static {
System.out.print(String.format("%s, %s!", "Hello", "World"));
System.out.print(String.format(
/* condition start */ true /* condition end */
? /* first leg start */ "%s, %s!" /* first leg end */
: /* second leg start */ "%s: %s" /* second leg end */,
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */));
System.out.print(String.format(/* one */ Locale.US, /* two */ "%s, %s!" /* three */, /* four */ "Hello" /* five */, /* six */ "World" /* seven */));
}
Main() {
System.out.print(String.format("%s, World!", "Hello"));
System.out.print(String.format("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.print(String.format(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.print(String.format(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.print(String.format(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.print("========");
}
void f() {
System.out.print(String.format("%s, World!", "Hello"));
System.out.print(String.format("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.print(String.format(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.print(String.format(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.print(String.format(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.print("========");
}
void out(PrintStream printer) {
printer.print(String.<caret>format("%s, World!", "Hello"));
printer.print(String.format("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.print(String.format(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.print(String.format(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.print(String.format(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.print(String.format(/* one */ Locale.US, /* two */ "%s, %s!" /* three */, /* four */ "Hello" /* five */, /* six */ "World" /* seven */));
printer.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.print("========");
}
void caller() {
print(String.format("%s, %s!", "Hello", "World"));
}
static void print(String value) {}
}

View File

@@ -0,0 +1,28 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
class Main {
static {
System.out.print(String.<caret>format(/* begin */ "Hello, World!"/* end */));
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
Main() {
System.out.print(String.format(/* begin */ "Hello, World!"/* end */));
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
void f() {
System.out.print(String.format(/* begin */ "Hello, World!"/* end */));
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
void out(PrintStream printer) {
printer.print(String.format(/* begin */ "Hello, World!"/* end */));
printer.print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
void caller() {
print(String.format(/* begin */ "Hello, World!"/* end */));
print(String.format(/* begin */ "Hello, World!%n"/* end */));
}
static void print(String value) {}
}

View File

@@ -0,0 +1,38 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
import java.util.Locale;
import static java.lang.String.format;
class Main {
static {
String s1 = f<caret>ormat("test");
String s1n = format("test%n");
String s2 = format(Locale.US, "test");
String s2n = format(Locale.US, "test%n");
String s3 = String.format("test");
String s3l = String.format(Locale.US, "test");
String s3n = String.format(Locale.US, "test%n");
System.out.println(String.format(/* one */ Locale.CANADA /* two */, /* three */ "hello, " /* four */));
}
Main() {
String s1 = format("test");
String s1n = format("test%n");
String s2 = format(Locale.US, "test");
String s2n = format(Locale.US, "test%n");
String s3 = String.format("test");
String s3l = String.format(Locale.US, "test");
String s3n = String.format(Locale.US, "test%n");
}
void f() {
String s1 = format("test");
String s1n = format("test%n");
String s2 = format(Locale.US, "test");
String s2n = format(Locale.US, "test%n");
String s3 = String.format("test");
String s3l = String.format(Locale.US, "test");
String s3n = String.format(Locale.US, "test%n");
}
}

View File

@@ -0,0 +1,52 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
class Main {
static {
System.out.println(String.<caret>format("%s, %s!", "Hello", "World"));
System.out.println(String.format(
/* condition start */ false /* condition end */
? /* first leg start */ "%s, %s!" /* first leg end */
: /* second leg start */ "%s: %s" /* second leg end */,
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */));
}
Main() {
System.out.println(String.format("Hello, World!%n"));
System.out.println(String.format("%s, World!", "Hello"));
System.out.println(String.format("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.println(String.format(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.println(String.format(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.println(String.format(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.println("========");
}
void f() {
System.out.println(String.format("Hello, World!%n"));
System.out.println(String.format("%s, World!", "Hello"));
System.out.println(String.format("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.println(String.format(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.println(String.format(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.println(String.format(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
System.out.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
System.out.println("========");
}
void out(PrintStream printer) {
printer.println(String.format("Hello, World!%n"));
printer.println(String.format("%s, World!", "Hello"));
printer.println(String.format("%s, %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.println(String.format(/* one */ "%s," + /* two */ " %s!", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.println(String.format(/* one */ "%s," + /* two */ " %s!" + 5, /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.println(String.format(/* one */ "%s," + /* two */ " %s!" + (5 /* four */ + /* five */ 7), /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */));
printer.printf("%s, %s", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.printf("%s, %s%n", /* param1 start */ "Hello" /* param1 end */, /* param2 start */ "World" /* param2 end */);
printer.println("========");
}
void caller() {
println(String.format("%s, %s!", "Hello", "World"));
}
static void println(String value) {}
}