CodeQL queries 1.25
Skip to end of metadata
Go to start of metadata

Name: Ignoring result from pure array method

Description: Ignoring the result of an array method that does not modify its receiver is generally an error.

ID: js/ignore-array-result

Kind: problem

Severity: warning

Precision: high

Query: IgnoreArrayResult.ql
 * @name Ignoring result from pure array method
 * @description Ignoring the result of an array method that does not modify its receiver is generally an error.
 * @kind problem
 * @problem.severity warning
 * @id js/ignore-array-result
 * @tags maintainability
 *       correctness
 * @precision high

import javascript
import Expressions.ExprHasNoEffect

DataFlow::SourceNode callsArray(DataFlow::TypeBackTracker t, DataFlow::MethodCallNode call) {
  isIgnoredPureArrayCall(call) and
  t.start() and
  result = call.getReceiver().getALocalSource()
  exists(DataFlow::TypeBackTracker t2 | result = callsArray(t2, call).backtrack(t2, t))

DataFlow::SourceNode callsArray(DataFlow::MethodCallNode call) {
  result = callsArray(DataFlow::TypeBackTracker::end(), call)

predicate isIgnoredPureArrayCall(DataFlow::MethodCallNode call) {
  inVoidContext(call.asExpr()) and
    call.getMethodName() = "concat" and
    call.getNumArgument() = 1
    call.getMethodName() = "join" and
    call.getNumArgument() < 2
    call.getMethodName() = "slice" and
    call.getNumArgument() < 3

from DataFlow::MethodCallNode call
  callsArray(call) instanceof DataFlow::ArrayCreationNode and
  not call.getReceiver().asExpr().(ArrayExpr).getSize() = 0
select call, "Result from call to " + call.getMethodName() + " ignored."

The concat, join and slice methods are pure and do not modify any of the inputs or the array the method is called on. It is therefore generally an error to ignore the return value from a call to one of these methods.


Use the returned value from the calls to concat, join or slice.


A function extend is defined in the following example. The function uses the concat method to add elements to the arr array. However, the extend function has no effect as the return value from concat is ignored:

var arr = [1,2,3];

function extend(others) {

Assigning the returned value from the call to concat to the arr variable fixes the error:

var arr = [1,2,3];

function extend(others) {
	arr = arr.concat(others);