summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/rc_clone_in_vec_init.txt
blob: 6fc08aaf9ab57141dd81585151c2e6eedea87271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
### What it does
Checks for reference-counted pointers (`Arc`, `Rc`, `rc::Weak`, and `sync::Weak`)
in `vec![elem; len]`

### Why is this bad?
This will create `elem` once and clone it `len` times - doing so with `Arc`/`Rc`/`Weak`
is a bit misleading, as it will create references to the same pointer, rather
than different instances.

### Example
```
let v = vec![std::sync::Arc::new("some data".to_string()); 100];
// or
let v = vec![std::rc::Rc::new("some data".to_string()); 100];
```
Use instead:
```
// Initialize each value separately:
let mut data = Vec::with_capacity(100);
for _ in 0..100 {
    data.push(std::rc::Rc::new("some data".to_string()));
}

// Or if you want clones of the same reference,
// Create the reference beforehand to clarify that
// it should be cloned for each value
let data = std::rc::Rc::new("some data".to_string());
let v = vec![data; 100];
```