# prefer-clone-from

Prefer Clone.cloneFrom when replacing a cloneable value

Assigning a newly cloned value discards storage that the destination may be able to reuse.
Instead, you SHOULD call `cloneFrom` so the destination can reuse its existing allocation.

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

## Reported

```ds title="main.ds"
import { rc } from "destack:memory";

function replace(target: &exclusive rc.Rc<int32>, source: &readonly rc.Rc<int32>): void {
    *target = source.clone();
}
```

## Accepted

```ds title="main.ds"
import { rc } from "destack:memory";

function replace(target: &exclusive rc.Rc<int32>, source: &readonly rc.Rc<int32>): void {
    target.cloneFrom(source);
}
```

## Prior art

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

[language/linter/src/rules/performance/prefer_clone_from.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/prefer_clone_from.rs#L8)
