|
| 1 | +/** |
| 2 | + * @name Unused property |
| 3 | + * @description Unused properties may be a symptom of a bug and should be examined carefully. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity recommendation |
| 6 | + * @id js/unused-property |
| 7 | + * @tags maintainability |
| 8 | + * @precision high |
| 9 | + */ |
| 10 | + |
| 11 | +import javascript |
| 12 | +import semmle.javascript.dataflow.LocalObjects |
| 13 | +import UnusedVariable |
| 14 | +import UnusedParameter |
| 15 | +import Expressions.ExprHasNoEffect |
| 16 | + |
| 17 | +predicate hasUnknownPropertyRead(LocalObject obj) { |
| 18 | + // dynamic reads |
| 19 | + exists(DataFlow::PropRead r | obj.getAPropertyRead() = r | not exists(r.getPropertyName())) |
| 20 | + or |
| 21 | + // reflective reads |
| 22 | + obj.flowsToExpr(any(EnhancedForLoop l).getIterationDomain()) |
| 23 | + or |
| 24 | + obj.flowsToExpr(any(InExpr l).getRightOperand()) |
| 25 | + or |
| 26 | + obj.flowsToExpr(any(SpreadElement e).getOperand()) |
| 27 | + or |
| 28 | + exists(obj.getAPropertyRead("hasOwnProperty")) |
| 29 | + or |
| 30 | + exists(obj.getAPropertyRead("propertyIsEnumerable")) |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Holds if `obj` flows to an expression that must have a specific type. |
| 35 | + */ |
| 36 | +predicate flowsToTypeRestrictedExpression(LocalObject obj) { |
| 37 | + exists (Expr restricted, TypeExpr type | |
| 38 | + obj.flowsToExpr(restricted) and |
| 39 | + not type.isAny() | |
| 40 | + exists (TypeAssertion assertion | |
| 41 | + type = assertion.getTypeAnnotation() and |
| 42 | + restricted = assertion.getExpression() |
| 43 | + ) |
| 44 | + or |
| 45 | + exists (BindingPattern v | |
| 46 | + type = v.getTypeAnnotation() and |
| 47 | + restricted = v.getAVariable().getAnAssignedExpr() |
| 48 | + ) |
| 49 | + // no need to reason about writes to typed fields, captured nodes do not reach them |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +from DataFlow::PropWrite write, LocalObject obj, string name |
| 54 | +where |
| 55 | + write = obj.getAPropertyWrite(name) and |
| 56 | + not exists(obj.getAPropertyRead(name)) and |
| 57 | + // `obj` is the only base object for the write: it is not spurious |
| 58 | + not write.getBase().analyze().getAValue() != obj.analyze().getAValue() and |
| 59 | + not hasUnknownPropertyRead(obj) and |
| 60 | + // avoid reporting if the definition is unreachable |
| 61 | + write.getAstNode().getFirstControlFlowNode().getBasicBlock() instanceof ReachableBasicBlock and |
| 62 | + // avoid implicitly read properties |
| 63 | + not ( |
| 64 | + name = "toString" or |
| 65 | + name = "valueOf" or |
| 66 | + name.matches("@@%") // @@iterator, for example |
| 67 | + ) and |
| 68 | + // avoid flagging properties that a type system requires |
| 69 | + not flowsToTypeRestrictedExpression(obj) and |
| 70 | + // flagged by js/unused-local-variable |
| 71 | + not exists(UnusedLocal l | l.getAnAssignedExpr().getUnderlyingValue().flow() = obj) and |
| 72 | + // flagged by js/unused-parameter |
| 73 | + not exists(Parameter p | isAnAccidentallyUnusedParameter(p) | |
| 74 | + p.getDefault().getUnderlyingValue().flow() = obj |
| 75 | + ) and |
| 76 | + // flagged by js/useless-expression |
| 77 | + not inVoidContext(obj.asExpr()) |
| 78 | +select write, "Unused property " + name + "." |
0 commit comments