# redundant-iter-cloned

Disallow cloning iterator elements that are only borrowed afterward

Cloning iterator elements is unnecessary when the following operation only observes them through readonly borrows.
Instead, you SHOULD operate on the borrowed elements directly.

- Category: performance
- Level: warning
- Fix: automatic
- Scope: module

## Reported

```ds title="main.ds"
import { Iterator } from "destack:iter";

struct Label {
    values: ^int32[];
}

function lengths(values: Iterator<&readonly Label>): Iterator<isize> {
    return values.cloned().map((value) => value.values.length);
}
```

## Accepted

```ds title="main.ds"
import { Iterator } from "destack:iter";

struct Label {
    values: ^int32[];
}

function lengths(values: Iterator<&readonly Label>): Iterator<isize> {
    return values.map((value) => value.values.length);
}
```

## Prior art

- [Clippy · redundant_iter_cloned](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_iter_cloned)

[language/linter/src/rules/performance/redundant_iter_cloned.rs:20](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/redundant_iter_cloned.rs#L20)
