Search in sources :

Example 1 with TypeTag

use of com.sun.tools.javac.code.TypeTag in project error-prone by google.

the class BadComparable method matches.

/**
   * Matches if this is a narrowing integral cast between signed types where the expression is a
   * subtract.
   */
private boolean matches(TypeCastTree tree, VisitorState state) {
    Type treeType = ASTHelpers.getType(tree.getType());
    // If the cast isn't narrowing to an int then don't implicate it in the bug pattern.
    if (treeType.getTag() != TypeTag.INT) {
        return false;
    }
    // The expression should be a subtract but remove parentheses.
    ExpressionTree expression = TreeInfo.skipParens((JCExpression) tree.getExpression());
    if (expression.getKind() != Kind.MINUS) {
        return false;
    }
    // Ensure the expression type is wider and signed (ie a long) than the cast type ignoring
    // boxing.
    Type expressionType = getTypeOfSubtract((BinaryTree) expression);
    TypeTag expressionTypeTag = state.getTypes().unboxedTypeOrType(expressionType).getTag();
    return (expressionTypeTag == TypeTag.LONG);
}
Also used : Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree) TypeTag(com.sun.tools.javac.code.TypeTag)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)1 Type (com.sun.tools.javac.code.Type)1 TypeTag (com.sun.tools.javac.code.TypeTag)1