use of org.sonar.plugins.java.api.tree.InstanceOfTree in project sonar-java by SonarSource.
the class InstanceOfAlwaysTrueCheck method visitNode.
@Override
public void visitNode(Tree tree) {
InstanceOfTree instanceOfTree = (InstanceOfTree) tree;
Type expressionType = instanceOfTree.expression().symbolType();
Type instanceOf = instanceOfTree.type().symbolType();
if (expressionType.isSubtypeOf(instanceOf) && !instanceOfTree.expression().is(Tree.Kind.NULL_LITERAL)) {
reportIssue(instanceOfTree.instanceofKeyword(), "Remove this useless \"instanceof\" operator; it will always return \"true\". ");
}
}
use of org.sonar.plugins.java.api.tree.InstanceOfTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method instanceof_expression.
/**
* 15.20.2. Type Comparison Operator instanceof
*/
@Test
public void instanceof_expression() {
InstanceOfTree tree = (InstanceOfTree) expressionOfReturnStatement("class T { boolean m() { return null instanceof Object; } }");
assertThat(tree.is(Tree.Kind.INSTANCE_OF)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.instanceofKeyword().text()).isEqualTo("instanceof");
assertThat(tree.type()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 3);
}
use of org.sonar.plugins.java.api.tree.InstanceOfTree in project sonar-java by SonarSource.
the class InstanceofUsedOnExceptionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.CATCH)) {
CatchTree catchTree = (CatchTree) tree;
caughtVariables.add(catchTree.parameter().simpleName().name());
} else if (isLeftOperandAnException((InstanceOfTree) tree)) {
reportIssue(((InstanceOfTree) tree).instanceofKeyword(), "Replace the usage of the \"instanceof\" operator by a catch block.");
}
}
Aggregations