menu

iter-overeager-cloned

Delay iterator cloning until after operations that discard elements

Cloning iterator values before filtering or selection may clone values that the operation discards. Instead, you SHOULD apply cloned after filtering or limiting adapters and clone only values returned by terminal selectors.

Reported

1import { Iterator } from "destack:iter";2 3struct Label {4    values: ^int32[];5}6 7function prefix(values: Iterator<&readonly Label>, count: isize): Iterator<Label> {8    return values.cloned().take(count);9}

Accepted

1import { Iterator } from "destack:iter";2 3struct Label {4    values: ^int32[];5}6 7function prefix(values: Iterator<&readonly Label>, count: isize): Iterator<Label> {8    return values.take(count).cloned();9}