|
| 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.CapturedNodes |
| 13 | +import UnusedVariable |
| 14 | +import UnusedParameter |
| 15 | +import Expressions.ExprHasNoEffect |
| 16 | + |
| 17 | +predicate hasUnknownPropertyRead(CapturedSource 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 | +predicate flowsToTypeRestrictedExpression(CapturedSource n) { |
| 34 | + exists (Expr restricted, TypeExpr type | |
| 35 | + n.flowsToExpr(restricted) and |
| 36 | + not type.isAny() | |
| 37 | + exists (TypeAssertion assertion | |
| 38 | + type = assertion.getTypeAnnotation() and |
| 39 | + restricted = assertion.getExpression() |
| 40 | + ) |
| 41 | + or |
| 42 | + exists (BindingPattern v | |
| 43 | + type = v.getTypeAnnotation() and |
| 44 | + restricted = v.getAVariable().getAnAssignedExpr() |
| 45 | + ) |
| 46 | + // no need to reason about writes to typed fields, captured nodes do not reach them |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +from DataFlow::PropWrite w, CapturedSource n, string name |
| 51 | +where |
| 52 | + w = n.getAPropertyWrite(name) and |
| 53 | + not exists(n.getAPropertyRead(name)) and |
| 54 | + not w.getBase().analyze().getAValue() != n.analyze().getAValue() and |
| 55 | + not hasUnknownPropertyRead(n) and |
| 56 | + // avoid reporting if the definition is unreachable |
| 57 | + w.getAstNode().getFirstControlFlowNode().getBasicBlock() instanceof ReachableBasicBlock and |
| 58 | + // avoid implicitly read properties |
| 59 | + not ( |
| 60 | + name = "toString" or |
| 61 | + name = "valueOf" or |
| 62 | + name.matches("@@%") // @@iterator, for example |
| 63 | + ) and |
| 64 | + // avoid flagging properties that a type system requires |
| 65 | + not flowsToTypeRestrictedExpression(n) and |
| 66 | + // flagged by js/unused-local-variable |
| 67 | + not exists(UnusedLocal l | l.getAnAssignedExpr().getUnderlyingValue().flow() = n) and |
| 68 | + // flagged by js/unused-parameter |
| 69 | + not exists(Parameter p | isAnAccidentallyUnusedParameter(p) | |
| 70 | + p.getDefault().getUnderlyingValue().flow() = n |
| 71 | + ) and |
| 72 | + // flagged by js/useless-expression |
| 73 | + not inVoidContext(n.asExpr()) |
| 74 | +select w, "Unused property " + name + "." |
0 commit comments