summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/rc_clone_in_vec_init.txt
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tools/clippy/src/docs/rc_clone_in_vec_init.txt29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/tools/clippy/src/docs/rc_clone_in_vec_init.txt b/src/tools/clippy/src/docs/rc_clone_in_vec_init.txt
new file mode 100644
index 000000000..6fc08aaf9
--- /dev/null
+++ b/src/tools/clippy/src/docs/rc_clone_in_vec_init.txt
@@ -0,0 +1,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];
+``` \ No newline at end of file