Skip to content

Commit e3e7e77

Browse files
authored
Introduce StringFormatted Refaster rule (#1961)
1 parent e78f5bc commit e3e7e77

File tree

33 files changed

+94
-75
lines changed

33 files changed

+94
-75
lines changed

documentation-support/src/main/java/tech/picnic/errorprone/documentation/DocumentationGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static Path getOutputPath(String pathArg) {
5050
try {
5151
return Path.of(path);
5252
} catch (InvalidPathException e) {
53-
throw new IllegalArgumentException(String.format("Invalid path '%s'", path), e);
53+
throw new IllegalArgumentException("Invalid path '%s'".formatted(path), e);
5454
}
5555
}
5656
}

documentation-support/src/main/java/tech/picnic/errorprone/documentation/DocumentationGeneratorTaskListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ private void createDocsDirectory() {
8181
Files.createDirectories(docsPath);
8282
} catch (IOException e) {
8383
throw new IllegalStateException(
84-
String.format("Error while creating directory with path '%s'", docsPath), e);
84+
"Error while creating directory with path '%s'".formatted(docsPath), e);
8585
}
8686
}
8787

8888
private <T> void writeToFile(String identifier, String className, T data) {
89-
Json.write(docsPath.resolve(String.format("%s-%s.json", identifier, className)), data);
89+
Json.write(docsPath.resolve("%s-%s.json".formatted(identifier, className)), data);
9090
}
9191

9292
private static String getSimpleClassName(URI path) {

documentation-support/src/main/java/tech/picnic/errorprone/documentation/Json.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ static <T> void write(Path path, T object) {
4040

4141
@FormatMethod
4242
private static UncheckedIOException failure(IOException cause, String format, Object... args) {
43-
return new UncheckedIOException(String.format(format, args), cause);
43+
return new UncheckedIOException(format.formatted(args), cause);
4444
}
4545
}

documentation-support/src/main/java/tech/picnic/errorprone/documentation/RefasterRuleCollectionTestExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static Optional<String> tryExtractPatternGroup(String input, Pattern pat
136136

137137
@FormatMethod
138138
private static Supplier<VerifyException> violation(String format, Object... args) {
139-
return () -> new VerifyException(String.format(format, args));
139+
return () -> new VerifyException(format.formatted(args));
140140
}
141141

142142
record RefasterTestCases(

documentation-support/src/test/java/tech/picnic/errorprone/documentation/BugPatternExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void undocumentedSuppressionBugPattern(@TempDir Path outputDirectory) {
135135

136136
private static void verifyGeneratedFileContent(
137137
Path outputDirectory, String testClass, BugPatternDocumentation expected) {
138-
assertThat(outputDirectory.resolve(String.format("bugpattern-%s.json", testClass)))
138+
assertThat(outputDirectory.resolve("bugpattern-%s.json".formatted(testClass)))
139139
.exists()
140140
.returns(expected, path -> Json.read(path, BugPatternDocumentation.class));
141141
}

documentation-support/src/test/java/tech/picnic/errorprone/documentation/BugPatternTestExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void compilationAndBugCheckerRefactoringTestHelpersWithCustomCheckerPackageAndNa
551551

552552
private static void verifyGeneratedFileContent(
553553
Path outputDirectory, String testClass, BugPatternTestCases expected) {
554-
assertThat(outputDirectory.resolve(String.format("bugpattern-test-%s.json", testClass)))
554+
assertThat(outputDirectory.resolve("bugpattern-test-%s.json".formatted(testClass)))
555555
.exists()
556556
.returns(expected, path -> Json.read(path, BugPatternTestCases.class));
557557
}

documentation-support/src/test/java/tech/picnic/errorprone/documentation/RefasterRuleCollectionTestExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private static void verifyGeneratedFileContent(
159159
Path outputDirectory, String testIdentifier, RefasterTestCases expected) {
160160
assertThat(
161161
outputDirectory.resolve(
162-
String.format("refaster-rule-collection-test-%s.json", testIdentifier)))
162+
"refaster-rule-collection-test-%s.json".formatted(testIdentifier)))
163163
.exists()
164164
.returns(expected, path -> Json.read(path, RefasterTestCases.class));
165165
}

error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutability.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private Description suggestToCollectionAlternatives(
101101
.addFix(replaceMethodInvocation(tree, immutableReplacement, state))
102102
.addFix(
103103
mutableFix
104-
.replace(tree, String.format("%s(%s::new)", toCollectionSelect, mutableCollection))
104+
.replace(tree, "%s(%s::new)".formatted(toCollectionSelect, mutableCollection))
105105
.build())
106106
.build();
107107
}
@@ -125,7 +125,7 @@ private Description suggestToMapAlternatives(MethodInvocationTree tree, VisitorS
125125
.postfixWith(
126126
tree.getArguments().get(argCount - 1),
127127
(argCount == 2 ? ", (a, b) -> { throw new IllegalStateException(); }" : "")
128-
+ String.format(", %s::new", hashMap))
128+
+ ", %s::new".formatted(hashMap))
129129
.build())
130130
.build();
131131
}

error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/DirectReturn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public Description matchBlock(BlockTree tree, VisitorState state) {
9898
SuggestedFix.builder()
9999
.replace(
100100
precedingStatement,
101-
String.format("return %s;", SourceCode.treeToString(resultExpr, state)))
101+
"return %s;".formatted(SourceCode.treeToString(resultExpr, state)))
102102
.delete(finalStatement)
103103
.build()))
104104
.orElse(Description.NO_MATCH);

error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EagerStringFormatting.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,9 @@ private SuggestedFix suggestFlattening(String newPlaceholder, VisitorState state
230230

231231
private String deriveFormatStringExpression(String newPlaceholder, VisitorState state) {
232232
String derivative =
233-
String.format(
234-
simplifiableFormatString()
235-
.orElseThrow(() -> new VerifyException("Format string cannot be simplified")),
236-
Collections.nCopies(arguments().size(), newPlaceholder).toArray());
233+
simplifiableFormatString()
234+
.orElseThrow(() -> new VerifyException("Format string cannot be simplified"))
235+
.formatted(Collections.nCopies(arguments().size(), newPlaceholder).toArray());
237236

238237
/*
239238
* If the suggested replacement format string is the same as the original, then use the

0 commit comments

Comments
 (0)