Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · XO0536

Docs

XO0536: a collection copied out of a list element, changed, and stored back

note · reported by the vet

Reported by xo vet, not xo check. var x = xs[i] shares the element with xs, so the first change to x (a push, an element write, a var argument) copies all of it, and xs[i] = x stores the copy back. In a loop that copies the element on every pass: the PE linker copied a whole section at each relocation that way and linked xo.exe in quadratic time (2026-10-20). Change the element in place: xs[i].push(v), xs[i][j] = v, or pass xs[i] to a var parameter (decision 0169); the store back is then not needed. Taking the element first (xs[i] = [] right after the read) also avoids the copy and is not reported. See core 5.1 and decision 0294.

Example

fn fill(var bufs: List[List[Int]], k: Int, n: Int) {
  for i in 0..n {
    var b = bufs[k]
    b.push(i)
    bufs[k] = b
  }
}

Fix

fn fill(var bufs: List[List[Int]], k: Int, n: Int) {
  for i in 0..n {
    bufs[k].push(i)
  }
}

Run xo explain XO0536 for this text in a terminal, or see section 10.6 of the specification.