From e36f366aebc2b25a4f8a40d8c29e29c573792ce2 Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 3 Feb 2024 16:02:23 +0530 Subject: [PATCH] fixes: https://github.com/PicnicSupermarket/error-prone-support/issues/1001 --- .../errorprone/refasterrules/StringRules.java | 16 ++++++++++++++++ .../refasterrules/StringRulesTestInput.java | 4 ++++ .../refasterrules/StringRulesTestOutput.java | 4 ++++ 3 files changed, 24 insertions(+) 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); + } }