Skip to content

Commit dbde150

Browse files
SONARJAVA-5765: Recognize PatternInstanceOf trees as type checks for S2097
1 parent d92b96b commit dbde150

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

java-checks/src/main/java/org/sonar/java/checks/EqualsArgumentTypeCheck.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
3434
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
3535
import org.sonar.plugins.java.api.tree.MethodTree;
36+
import org.sonar.plugins.java.api.tree.PatternInstanceOfTree;
3637
import org.sonar.plugins.java.api.tree.Tree;
3738
import org.sonar.plugins.java.api.tree.TypeCastTree;
3839

@@ -146,7 +147,14 @@ private static class ExpressionVisitor extends BaseTreeVisitor {
146147

147148
@Override
148149
public void visitInstanceOf(InstanceOfTree tree) {
149-
if (isArgument(tree.expression(), parameterSymbol)) {
150+
if (isParameter(tree.expression())) {
151+
typeChecked = true;
152+
}
153+
}
154+
155+
@Override
156+
public void visitPatternInstanceOf(PatternInstanceOfTree tree) {
157+
if (isParameter(tree.expression())) {
150158
typeChecked = true;
151159
}
152160
}
@@ -171,7 +179,7 @@ public void visitMethodInvocation(MethodInvocationTree tree) {
171179
}
172180
}
173181
for (ExpressionTree argument : tree.arguments()) {
174-
if (isArgument(argument, parameterSymbol)) {
182+
if (isParameter(argument)) {
175183
typeChecked = true;
176184
return;
177185
}
@@ -189,6 +197,10 @@ private boolean isGetClassOnArgument(ExpressionTree tree) {
189197
private boolean isInvocationOnArgument(MethodInvocationTree tree) {
190198
return tree.methodSelect().is(Tree.Kind.MEMBER_SELECT) && isArgument(((MemberSelectExpressionTree) tree.methodSelect()).expression(), parameterSymbol);
191199
}
200+
201+
private boolean isParameter(ExpressionTree expressionTree) {
202+
return isArgument(expressionTree, parameterSymbol);
203+
}
192204
}
193205

194206
}

0 commit comments

Comments
 (0)