Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a single-letter name for primitives so there's a hope of it compiling. #4704

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading