From 7ee554fa6480d90e650cc234d0a4a63dbce1dbf2 Mon Sep 17 00:00:00 2001 From: ghm Date: Tue, 26 Nov 2024 09:57:09 -0800 Subject: [PATCH] Use a single-letter name for primitives so there's a hope of it compiling. PiperOrigin-RevId: 700375854 --- .../PatternMatchingInstanceof.java | 13 +++++++- .../PatternMatchingInstanceofTest.java | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java b/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java index f58f3ea7bbb..63fddff29ae 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java @@ -23,6 +23,7 @@ import static com.google.errorprone.util.ASTHelpers.getType; import static com.google.errorprone.util.SourceVersion.supportsPatternMatchingInstanceof; +import com.google.common.base.Ascii; import com.google.common.collect.ImmutableSet; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; @@ -41,6 +42,7 @@ import com.sun.source.util.TreePathScanner; import com.sun.tools.javac.code.Symbol.VarSymbol; import com.sun.tools.javac.code.Type; +import com.sun.tools.javac.code.TypeTag; import org.jspecify.annotations.Nullable; /** A BugPattern; see the summary. */ @@ -66,7 +68,16 @@ public Description matchInstanceOf(InstanceOfTree instanceOfTree, VisitorState s if (!allCasts.isEmpty()) { // This is a gamble as to an appropriate name. We could make sure it doesn't clash with // anything in scope, but that's effort. - var name = lowerFirstLetter(targetType.tsym.getSimpleName().toString()); + String name; + var unboxed = state.getTypes().unboxedType(targetType); + if (targetType.isPrimitive() || (unboxed != null && unboxed.getTag() != TypeTag.NONE)) { + name = + String.valueOf( + Ascii.toLowerCase(targetType.tsym.getSimpleName().toString().charAt(0))); + } else { + name = lowerFirstLetter(targetType.tsym.getSimpleName().toString()); + } + return describeMatch( instanceOfTree, SuggestedFix.builder() diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java index 422d7e30f34..63068b1b7e4 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java @@ -212,6 +212,36 @@ void test(Object o) { .doTest(); } + @Test + public void primitiveType_shortNameChosen() { + helper + .addInputLines( + "Test.java", + """ + class Test { + void test(Object o) { + if (o instanceof Long) { + test((Long) o); + test(((Long) o).hashCode()); + } + } + } + """) + .addOutputLines( + "Test.java", + """ + class Test { + void test(Object o) { + if (o instanceof Long l) { + test(l); + test(l.hashCode()); + } + } + } + """) + .doTest(); + } + @Test public void genericWithUpperBoundedWildcard() { helper