From fd718537d505bf302c77c16487f6f892e5af87c2 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 3 Feb 2024 18:35:41 +0100 Subject: [PATCH] Suggestions --- .../errorprone/refasterrules/StringRules.java | 25 ++++++++++++++++--- .../refasterrules/StringRulesTestInput.java | 16 +++++++++--- .../refasterrules/StringRulesTestOutput.java | 14 ++++++++--- 3 files changed, 43 insertions(+), 12 deletions(-) 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 9c1d4a97f68..d4c2cdf0f74 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 @@ -163,13 +163,14 @@ String after(Object object) { } /** - * Prefer direct invocation of {@link String#copyValueOf(char[])} over the indirection introduced - * by {@link String#copyValueOf(char[])}. + * Prefer direct invocation of {@link String#String(char[], int, int)} over the indirection + * introduced by alternatives. */ - static final class StringCopyValueOf { + static final class NewStringFromCharArraySubSequence { @BeforeTemplate String before(char[] data, int offset, int count) { - return String.copyValueOf(data, offset, count); + return Refaster.anyOf( + String.valueOf(data, offset, count), String.copyValueOf(data, offset, count)); } @AfterTemplate @@ -178,6 +179,22 @@ String after(char[] data, int offset, int count) { } } + /** + * Prefer direct invocation of {@link String#String(char[])} over the indirection introduced by + * alternatives. + */ + static final class NewStringFromCharArray { + @BeforeTemplate + String before(char[] data) { + return Refaster.anyOf(String.valueOf(data), new String(data, 0, data.length)); + } + + @AfterTemplate + String after(char[] data) { + return new String(data); + } + } + /** * 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 ad52da52dbb..a6b2f1cb33d 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 @@ -73,6 +73,18 @@ String testStringValueOf() { return Objects.toString("foo"); } + ImmutableSet testNewStringFromCharArraySubSequence() { + return ImmutableSet.of( + String.valueOf(new char[] {'f', 'o', 'o'}, 0, 1), + String.copyValueOf(new char[] {'b', 'a', 'r'}, 2, 3)); + } + + ImmutableSet testNewStringFromCharArray() { + return ImmutableSet.of( + String.valueOf(new char[] {'f', 'o', 'o'}), + new String(new char[] {'b', 'a', 'r'}, 0, new char[] {'b', 'a', 'r'}.length)); + } + Function testStringValueOfMethodReference() { return Objects::toString; } @@ -84,8 +96,4 @@ 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 d9d6d90720e..d5e1241aaad 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 @@ -75,6 +75,16 @@ String testStringValueOf() { return String.valueOf("foo"); } + ImmutableSet testNewStringFromCharArraySubSequence() { + return ImmutableSet.of( + new String(new char[] {'f', 'o', 'o'}, 0, 1), new String(new char[] {'b', 'a', 'r'}, 2, 3)); + } + + ImmutableSet testNewStringFromCharArray() { + return ImmutableSet.of( + new String(new char[] {'f', 'o', 'o'}), new String(new char[] {'b', 'a', 'r'})); + } + Function testStringValueOfMethodReference() { return String::valueOf; } @@ -86,8 +96,4 @@ String testSubstringRemainder() { int testUtf8EncodedLength() { return Utf8.encodedLength("foo"); } - - String testStringCopyValueOf() { - return new String(new char[] {'f', 'o', 'o'}, 0, 3); - } }