diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java index c45c0550f5..9c1d4a97f6 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java @@ -162,6 +162,22 @@ String after(Object object) { } } + /** + * Prefer direct invocation of {@link String#copyValueOf(char[])} over the indirection introduced + * by {@link String#copyValueOf(char[])}. + */ + static final class StringCopyValueOf { + @BeforeTemplate + String before(char[] data, int offset, int count) { + return String.copyValueOf(data, offset, count); + } + + @AfterTemplate + String after(char[] data, int offset, int count) { + return new String(data, offset, count); + } + } + /** * Prefer direct delegation to {@link String#valueOf(Object)} over the indirection introduced by * {@link Objects#toString(Object)}. diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java index e547a75974..ad52da52db 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java @@ -84,4 +84,8 @@ String testSubstringRemainder() { int testUtf8EncodedLength() { return "foo".getBytes(UTF_8).length; } + + String testStringCopyValueOf() { + return String.copyValueOf(new char[] {'f', 'o', 'o'}, 0, 3); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java index 8cb371ccc6..d9d6d90720 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java @@ -86,4 +86,8 @@ String testSubstringRemainder() { int testUtf8EncodedLength() { return Utf8.encodedLength("foo"); } + + String testStringCopyValueOf() { + return new String(new char[] {'f', 'o', 'o'}, 0, 3); + } }