IDEA-203697 Class.getClass() false-negative on Java 12 switch expression

This commit is contained in:
Tagir Valeev
2018-12-06 18:12:23 +07:00
parent e2f7b5986b
commit 0c7e86cf4c
3 changed files with 18 additions and 1 deletions
@@ -24,7 +24,8 @@ public class ClassGetClassInspection extends AbstractBaseJavaLocalInspectionTool
if (!OBJECT_GET_CLASS.test(call)) return;
// Sometimes people use xyz.getClass() for implicit NPE check. While it's a questionable code style
// do not warn about such case
if (call.getParent() instanceof PsiExpressionStatement) return;
if (call.getParent() instanceof PsiExpressionStatement &&
!(call.getParent().getParent() instanceof PsiSwitchLabeledRuleStatement)) return;
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (qualifier == null) return;
PsiType type = qualifier.getType();
@@ -0,0 +1,8 @@
// "Remove 'getClass()' call" "true"
public class Main {
Class<?> test(Class<?> obj) {
return switch(obj.hashCode()) {
default -> obj;
};
}
}
@@ -0,0 +1,8 @@
// "Remove 'getClass()' call" "true"
public class Main {
Class<?> test(Class<?> obj) {
return switch(obj.hashCode()) {
default -> obj.<caret>getClass();
};
}
}