|
| 1 | +import ql |
| 2 | + |
| 3 | +class RedundantInlineCast extends AstNode instanceof InlineCast { |
| 4 | + Type t; |
| 5 | + |
| 6 | + RedundantInlineCast() { |
| 7 | + t = unique( | | super.getType()) and |
| 8 | + ( |
| 9 | + // The cast is to the type the base expression already has |
| 10 | + t = unique( | | super.getBase().getType()) |
| 11 | + or |
| 12 | + // The cast is to the same type as the other expression in an equality comparison |
| 13 | + exists(ComparisonFormula comp, Expr other | comp.getOperator() = "=" | |
| 14 | + this = comp.getAnOperand() and |
| 15 | + other = comp.getAnOperand() and |
| 16 | + this != other and |
| 17 | + t = unique( | | other.getType()) and |
| 18 | + not other instanceof InlineCast // we don't want to risk both sides being "redundant" |
| 19 | + ) |
| 20 | + or |
| 21 | + exists(Call call, int i, Predicate target | |
| 22 | + this = call.getArgument(i) and |
| 23 | + target = unique( | | call.getTarget()) and |
| 24 | + t = unique( | | target.getParameterType(i)) |
| 25 | + ) |
| 26 | + ) and |
| 27 | + // noopt can require explicit casts |
| 28 | + not this.getEnclosingPredicate().getAnAnnotation() instanceof NoOpt |
| 29 | + } |
| 30 | + |
| 31 | + TypeExpr getTypeExpr() { result = super.getTypeExpr() } |
| 32 | +} |
| 33 | + |
| 34 | +// `any(Bar b)`. |
| 35 | +private class AnyCast extends AstNode instanceof FullAggregate { |
| 36 | + TypeExpr type; |
| 37 | + |
| 38 | + AnyCast() { |
| 39 | + super.getKind() = "any" and |
| 40 | + not exists(super.getRange()) and |
| 41 | + not exists(super.getExpr(_)) and |
| 42 | + count(super.getArgument(_)) = 1 and |
| 43 | + type = super.getArgument(0).getTypeExpr() |
| 44 | + } |
| 45 | + |
| 46 | + TypeExpr getTypeExpr() { result = type } |
| 47 | +} |
| 48 | + |
| 49 | +// `foo = any(Bar b)` is effectively a cast to `Bar`. |
| 50 | +class RedundantAnyCast extends AstNode instanceof ComparisonFormula { |
| 51 | + AnyCast cast; |
| 52 | + Expr operand; |
| 53 | + |
| 54 | + RedundantAnyCast() { |
| 55 | + super.getOperator() = "=" and |
| 56 | + super.getAnOperand() = cast and |
| 57 | + super.getAnOperand() = operand and |
| 58 | + cast != operand and |
| 59 | + unique( | | operand.getType()).getASuperType*() = |
| 60 | + unique( | | cast.getTypeExpr().getResolvedType()) and |
| 61 | + not this.getEnclosingPredicate().getAnAnnotation() instanceof NoOpt |
| 62 | + } |
| 63 | + |
| 64 | + TypeExpr getTypeExpr() { result = cast.getTypeExpr() } |
| 65 | +} |
| 66 | + |
| 67 | +// foo instanceof Bar |
| 68 | +class RedundantInstanceof extends AstNode instanceof InstanceOf { |
| 69 | + RedundantInstanceof() { |
| 70 | + unique( | | super.getExpr().getType()).getASuperType*() = |
| 71 | + unique( | | super.getType().getResolvedType()) and |
| 72 | + not this.getEnclosingPredicate().getAnAnnotation() instanceof NoOpt |
| 73 | + } |
| 74 | + |
| 75 | + TypeExpr getTypeExpr() { result = super.getType() } |
| 76 | +} |
| 77 | + |
| 78 | +predicate redundantCast(AstNode n, TypeExpr type) { |
| 79 | + n.(RedundantInlineCast).getTypeExpr() = type |
| 80 | + or |
| 81 | + n.(RedundantAnyCast).getTypeExpr() = type |
| 82 | + or |
| 83 | + n.(RedundantInstanceof).getTypeExpr() = type |
| 84 | +} |
0 commit comments