Skip to content

Commit f635aa8

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Improve handling of raw types
* Include the enclosing `TreePath` in the paths returned by `findAllCasts`, `targetType` walks up to find the enclosing method for `return`s * Check if the instanceof type or the cast type is already non-raw, to avoid adding an extra `<?>` PiperOrigin-RevId: 737027442
1 parent 52a3318 commit f635aa8

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.sun.source.tree.ConditionalExpressionTree;
4242
import com.sun.source.tree.IfTree;
4343
import com.sun.source.tree.InstanceOfTree;
44+
import com.sun.source.tree.ParameterizedTypeTree;
4445
import com.sun.source.tree.ParenthesizedTree;
4546
import com.sun.source.tree.Tree;
4647
import com.sun.source.tree.TypeCastTree;
@@ -112,7 +113,7 @@ public Description matchInstanceOf(InstanceOfTree instanceOfTree, VisitorState s
112113
// anything in scope, but that's effort.
113114
name = generateVariableName(targetType, state);
114115
}
115-
if (typeArgCount != 0) {
116+
if (typeArgCount != 0 && !(instanceOfTree.getType() instanceof ParameterizedTypeTree)) {
116117
fix.postfixWith(
117118
instanceOfTree.getType(),
118119
nCopies(typeArgCount, "?").stream().collect(joining(",", "<", ">")));
@@ -244,7 +245,7 @@ public Void visitTypeCast(TypeCastTree node, Void unused) {
244245
if (v.equals(symbol)
245246
&& state.getTypes().isSubtype(targetType, getType(node.getType()))) {
246247
usages.add(
247-
getCurrentPath().getParentPath().getLeaf() instanceof ParenthesizedTree p
248+
getCurrentPath().getParentPath().getLeaf() instanceof ParenthesizedTree
248249
? getCurrentPath().getParentPath()
249250
: getCurrentPath());
250251
}
@@ -253,7 +254,7 @@ public Void visitTypeCast(TypeCastTree node, Void unused) {
253254
}
254255
};
255256
for (Tree tree : trees) {
256-
scanner.scan(new TreePath(new TreePath(state.getPath().getCompilationUnit()), tree), null);
257+
scanner.scan(new TreePath(state.getPath(), tree), null);
257258
}
258259
return usages.build();
259260
}

core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -664,4 +664,38 @@ public String stringify(Object o) {
664664
""")
665665
.doTest();
666666
}
667+
668+
@Test
669+
public void returnTarget() {
670+
helper
671+
.addInputLines(
672+
"Test.java",
673+
"""
674+
class Test<T> {
675+
private String val;
676+
677+
public Class stringify(Object o) {
678+
if (o instanceof Class<?>) {
679+
return (Class) o;
680+
}
681+
return null;
682+
}
683+
}
684+
""")
685+
.addOutputLines(
686+
"Test.java",
687+
"""
688+
class Test<T> {
689+
private String val;
690+
691+
public Class stringify(Object o) {
692+
if (o instanceof Class<?> c) {
693+
return c;
694+
}
695+
return null;
696+
}
697+
}
698+
""")
699+
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
700+
}
667701
}

0 commit comments

Comments
 (0)