|
| 1 | +/** |
| 2 | + * @name Use of returnless function |
| 3 | + * @description Using the return value of a function that does not return an expression is indicative of a mistake. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id js/use-of-returnless-function |
| 7 | + * @tags maintainability, |
| 8 | + * correctness |
| 9 | + * @precision high |
| 10 | + */ |
| 11 | + |
| 12 | +import javascript |
| 13 | +import Declarations.UnusedVariable |
| 14 | +import Expressions.ExprHasNoEffect |
| 15 | +import Statements.UselessConditional |
| 16 | + |
| 17 | +predicate returnsVoid(Function f) { |
| 18 | + not f.isGenerator() and |
| 19 | + not f.isAsync() and |
| 20 | + not exists(f.getAReturnedExpr()) |
| 21 | +} |
| 22 | + |
| 23 | +predicate isStub(Function f) { |
| 24 | + f.getBody().(BlockStmt).getNumChild() = 0 |
| 25 | + or |
| 26 | + f instanceof ExternalDecl |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Holds if `e` is in a syntactic context where it likely is fine that the value of `e` comes from a call to a returnless function. |
| 31 | + */ |
| 32 | +predicate benignContext(Expr e) { |
| 33 | + inVoidContext(e) or |
| 34 | + |
| 35 | + // A return statement is often used to just end the function. |
| 36 | + e = any(Function f).getAReturnedExpr() |
| 37 | + or |
| 38 | + exists(ConditionalExpr cond | cond.getABranch() = e and benignContext(cond)) |
| 39 | + or |
| 40 | + exists(LogicalBinaryExpr bin | bin.getAnOperand() = e and benignContext(bin)) |
| 41 | + or |
| 42 | + exists(Expr parent | parent.getUnderlyingValue() = e and benignContext(parent)) |
| 43 | + or |
| 44 | + any(VoidExpr voidExpr).getOperand() = e |
| 45 | + |
| 46 | + or |
| 47 | + // weeds out calls inside HTML-attributes. |
| 48 | + e.getParent() instanceof CodeInAttribute or |
| 49 | + // and JSX-attributes. |
| 50 | + e = any(JSXAttribute attr).getValue() or |
| 51 | + |
| 52 | + exists(AwaitExpr await | await.getOperand() = e and benignContext(await)) |
| 53 | + or |
| 54 | + // Avoid double reporting with js/trivial-conditional |
| 55 | + isExplicitConditional(_, e) |
| 56 | + or |
| 57 | + // Avoid double reporting with js/comparison-between-incompatible-types |
| 58 | + any(Comparison binOp).getAnOperand() = e |
| 59 | + or |
| 60 | + // Avoid double reporting with js/property-access-on-non-object |
| 61 | + any(PropAccess ac).getBase() = e |
| 62 | + or |
| 63 | + // Avoid double-reporting with js/unused-local-variable |
| 64 | + exists(VariableDeclarator v | v.getInit() = e and v.getBindingPattern().getVariable() instanceof UnusedLocal) |
| 65 | + or |
| 66 | + // Avoid double reporting with js/call-to-non-callable |
| 67 | + any(InvokeExpr invoke).getCallee() = e |
| 68 | + or |
| 69 | + // arguments to Promise.resolve (and promise library variants) are benign. |
| 70 | + e = any(ResolvedPromiseDefinition promise).getValue().asExpr() |
| 71 | +} |
| 72 | + |
| 73 | +predicate oneshotClosure(InvokeExpr call) { |
| 74 | + call.getCallee().getUnderlyingValue() instanceof ImmediatelyInvokedFunctionExpr |
| 75 | +} |
| 76 | + |
| 77 | +predicate alwaysThrows(Function f) { |
| 78 | + exists(ReachableBasicBlock entry, DataFlow::Node throwNode | |
| 79 | + entry = f.getEntryBB() and |
| 80 | + throwNode.asExpr() = any(ThrowStmt t).getExpr() and |
| 81 | + entry.dominates(throwNode.getBasicBlock()) |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +from DataFlow::CallNode call |
| 86 | +where |
| 87 | + not call.isIndefinite(_) and |
| 88 | + forex(Function f | f = call.getACallee() | |
| 89 | + returnsVoid(f) and not isStub(f) and not alwaysThrows(f) |
| 90 | + ) and |
| 91 | + |
| 92 | + not benignContext(call.asExpr()) and |
| 93 | + |
| 94 | + // anonymous one-shot closure. Those are used in weird ways and we ignore them. |
| 95 | + not oneshotClosure(call.asExpr()) |
| 96 | +select |
| 97 | + call, "the function $@ does not return anything, yet the return value is used.", call.getACallee(), call.getCalleeName() |
| 98 | + |
0 commit comments