summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/rc_clone_in_vec_init
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/rc_clone_in_vec_init
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/rc_clone_in_vec_init')
-rw-r--r--src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.rs68
-rw-r--r--src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.stderr109
-rw-r--r--src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.rs69
-rw-r--r--src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.stderr109
-rw-r--r--src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.rs83
-rw-r--r--src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.stderr201
6 files changed, 639 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.rs b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.rs
new file mode 100644
index 000000000..384060e6e
--- /dev/null
+++ b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.rs
@@ -0,0 +1,68 @@
+#![warn(clippy::rc_clone_in_vec_init)]
+use std::sync::{Arc, Mutex};
+
+fn main() {}
+
+fn should_warn_simple_case() {
+ let v = vec![Arc::new("x".to_string()); 2];
+}
+
+fn should_warn_simple_case_with_big_indentation() {
+ if true {
+ let k = 1;
+ dbg!(k);
+ if true {
+ let v = vec![Arc::new("x".to_string()); 2];
+ }
+ }
+}
+
+fn should_warn_complex_case() {
+ let v = vec![
+ std::sync::Arc::new(Mutex::new({
+ let x = 1;
+ dbg!(x);
+ x
+ }));
+ 2
+ ];
+
+ let v1 = vec![
+ Arc::new(Mutex::new({
+ let x = 1;
+ dbg!(x);
+ x
+ }));
+ 2
+ ];
+}
+
+fn should_not_warn_custom_arc() {
+ #[derive(Clone)]
+ struct Arc;
+
+ impl Arc {
+ fn new() -> Self {
+ Arc
+ }
+ }
+
+ let v = vec![Arc::new(); 2];
+}
+
+fn should_not_warn_vec_from_elem_but_not_arc() {
+ let v = vec![String::new(); 2];
+ let v1 = vec![1; 2];
+ let v2 = vec![
+ Box::new(std::sync::Arc::new({
+ let y = 3;
+ dbg!(y);
+ y
+ }));
+ 2
+ ];
+}
+
+fn should_not_warn_vec_macro_but_not_from_elem() {
+ let v = vec![Arc::new("x".to_string())];
+}
diff --git a/src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.stderr b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.stderr
new file mode 100644
index 000000000..cd7d91e12
--- /dev/null
+++ b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/arc.stderr
@@ -0,0 +1,109 @@
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/arc.rs:7:13
+ |
+LL | let v = vec![Arc::new("x".to_string()); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: `-D clippy::rc-clone-in-vec-init` implied by `-D warnings`
+ = note: each element will point to the same `Arc` instance
+help: consider initializing each `Arc` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Arc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Arc` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Arc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/arc.rs:15:21
+ |
+LL | let v = vec![Arc::new("x".to_string()); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Arc` instance
+help: consider initializing each `Arc` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Arc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Arc` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Arc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/arc.rs:21:13
+ |
+LL | let v = vec![
+ | _____________^
+LL | | std::sync::Arc::new(Mutex::new({
+LL | | let x = 1;
+LL | | dbg!(x);
+... |
+LL | | 2
+LL | | ];
+ | |_____^
+ |
+ = note: each element will point to the same `Arc` instance
+help: consider initializing each `Arc` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(std::sync::Arc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Arc` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = std::sync::Arc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/arc.rs:30:14
+ |
+LL | let v1 = vec![
+ | ______________^
+LL | | Arc::new(Mutex::new({
+LL | | let x = 1;
+LL | | dbg!(x);
+... |
+LL | | 2
+LL | | ];
+ | |_____^
+ |
+ = note: each element will point to the same `Arc` instance
+help: consider initializing each `Arc` element individually
+ |
+LL ~ let v1 = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Arc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Arc` initialization to a variable
+ |
+LL ~ let v1 = {
+LL + let data = Arc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: aborting due to 4 previous errors
+
diff --git a/src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.rs b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.rs
new file mode 100644
index 000000000..0394457fe
--- /dev/null
+++ b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.rs
@@ -0,0 +1,69 @@
+#![warn(clippy::rc_clone_in_vec_init)]
+use std::rc::Rc;
+use std::sync::Mutex;
+
+fn main() {}
+
+fn should_warn_simple_case() {
+ let v = vec![Rc::new("x".to_string()); 2];
+}
+
+fn should_warn_simple_case_with_big_indentation() {
+ if true {
+ let k = 1;
+ dbg!(k);
+ if true {
+ let v = vec![Rc::new("x".to_string()); 2];
+ }
+ }
+}
+
+fn should_warn_complex_case() {
+ let v = vec![
+ std::rc::Rc::new(Mutex::new({
+ let x = 1;
+ dbg!(x);
+ x
+ }));
+ 2
+ ];
+
+ let v1 = vec![
+ Rc::new(Mutex::new({
+ let x = 1;
+ dbg!(x);
+ x
+ }));
+ 2
+ ];
+}
+
+fn should_not_warn_custom_arc() {
+ #[derive(Clone)]
+ struct Rc;
+
+ impl Rc {
+ fn new() -> Self {
+ Rc
+ }
+ }
+
+ let v = vec![Rc::new(); 2];
+}
+
+fn should_not_warn_vec_from_elem_but_not_rc() {
+ let v = vec![String::new(); 2];
+ let v1 = vec![1; 2];
+ let v2 = vec![
+ Box::new(std::rc::Rc::new({
+ let y = 3;
+ dbg!(y);
+ y
+ }));
+ 2
+ ];
+}
+
+fn should_not_warn_vec_macro_but_not_from_elem() {
+ let v = vec![Rc::new("x".to_string())];
+}
diff --git a/src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.stderr b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.stderr
new file mode 100644
index 000000000..fe861afe0
--- /dev/null
+++ b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/rc.stderr
@@ -0,0 +1,109 @@
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/rc.rs:8:13
+ |
+LL | let v = vec![Rc::new("x".to_string()); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: `-D clippy::rc-clone-in-vec-init` implied by `-D warnings`
+ = note: each element will point to the same `Rc` instance
+help: consider initializing each `Rc` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Rc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Rc` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Rc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/rc.rs:16:21
+ |
+LL | let v = vec![Rc::new("x".to_string()); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Rc` instance
+help: consider initializing each `Rc` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Rc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Rc` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Rc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/rc.rs:22:13
+ |
+LL | let v = vec![
+ | _____________^
+LL | | std::rc::Rc::new(Mutex::new({
+LL | | let x = 1;
+LL | | dbg!(x);
+... |
+LL | | 2
+LL | | ];
+ | |_____^
+ |
+ = note: each element will point to the same `Rc` instance
+help: consider initializing each `Rc` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(std::rc::Rc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Rc` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = std::rc::Rc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/rc.rs:31:14
+ |
+LL | let v1 = vec![
+ | ______________^
+LL | | Rc::new(Mutex::new({
+LL | | let x = 1;
+LL | | dbg!(x);
+... |
+LL | | 2
+LL | | ];
+ | |_____^
+ |
+ = note: each element will point to the same `Rc` instance
+help: consider initializing each `Rc` element individually
+ |
+LL ~ let v1 = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Rc::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Rc` initialization to a variable
+ |
+LL ~ let v1 = {
+LL + let data = Rc::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: aborting due to 4 previous errors
+
diff --git a/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.rs b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.rs
new file mode 100644
index 000000000..693c9b553
--- /dev/null
+++ b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.rs
@@ -0,0 +1,83 @@
+#![warn(clippy::rc_clone_in_vec_init)]
+use std::rc::{Rc, Weak as UnSyncWeak};
+use std::sync::{Arc, Mutex, Weak as SyncWeak};
+
+fn main() {}
+
+fn should_warn_simple_case() {
+ let v = vec![SyncWeak::<u32>::new(); 2];
+ let v2 = vec![UnSyncWeak::<u32>::new(); 2];
+
+ let v = vec![Rc::downgrade(&Rc::new("x".to_string())); 2];
+ let v = vec![Arc::downgrade(&Arc::new("x".to_string())); 2];
+}
+
+fn should_warn_simple_case_with_big_indentation() {
+ if true {
+ let k = 1;
+ dbg!(k);
+ if true {
+ let v = vec![Arc::downgrade(&Arc::new("x".to_string())); 2];
+ let v2 = vec![Rc::downgrade(&Rc::new("x".to_string())); 2];
+ }
+ }
+}
+
+fn should_warn_complex_case() {
+ let v = vec![
+ Arc::downgrade(&Arc::new(Mutex::new({
+ let x = 1;
+ dbg!(x);
+ x
+ })));
+ 2
+ ];
+
+ let v1 = vec![
+ Rc::downgrade(&Rc::new(Mutex::new({
+ let x = 1;
+ dbg!(x);
+ x
+ })));
+ 2
+ ];
+}
+
+fn should_not_warn_custom_weak() {
+ #[derive(Clone)]
+ struct Weak;
+
+ impl Weak {
+ fn new() -> Self {
+ Weak
+ }
+ }
+
+ let v = vec![Weak::new(); 2];
+}
+
+fn should_not_warn_vec_from_elem_but_not_weak() {
+ let v = vec![String::new(); 2];
+ let v1 = vec![1; 2];
+ let v2 = vec![
+ Box::new(Arc::downgrade(&Arc::new({
+ let y = 3;
+ dbg!(y);
+ y
+ })));
+ 2
+ ];
+ let v3 = vec![
+ Box::new(Rc::downgrade(&Rc::new({
+ let y = 3;
+ dbg!(y);
+ y
+ })));
+ 2
+ ];
+}
+
+fn should_not_warn_vec_macro_but_not_from_elem() {
+ let v = vec![Arc::downgrade(&Arc::new("x".to_string()))];
+ let v = vec![Rc::downgrade(&Rc::new("x".to_string()))];
+}
diff --git a/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.stderr b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.stderr
new file mode 100644
index 000000000..4a21946cc
--- /dev/null
+++ b/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.stderr
@@ -0,0 +1,201 @@
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:8:13
+ |
+LL | let v = vec![SyncWeak::<u32>::new(); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: `-D clippy::rc-clone-in-vec-init` implied by `-D warnings`
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(SyncWeak::<u32>::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = SyncWeak::<u32>::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:9:14
+ |
+LL | let v2 = vec![UnSyncWeak::<u32>::new(); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v2 = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(UnSyncWeak::<u32>::new(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v2 = {
+LL + let data = UnSyncWeak::<u32>::new(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:11:13
+ |
+LL | let v = vec![Rc::downgrade(&Rc::new("x".to_string())); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Rc::downgrade(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Rc::downgrade(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:12:13
+ |
+LL | let v = vec![Arc::downgrade(&Arc::new("x".to_string())); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Arc::downgrade(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Arc::downgrade(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:20:21
+ |
+LL | let v = vec![Arc::downgrade(&Arc::new("x".to_string())); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Arc::downgrade(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Arc::downgrade(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:21:22
+ |
+LL | let v2 = vec![Rc::downgrade(&Rc::new("x".to_string())); 2];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v2 = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Rc::downgrade(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v2 = {
+LL + let data = Rc::downgrade(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:27:13
+ |
+LL | let v = vec![
+ | _____________^
+LL | | Arc::downgrade(&Arc::new(Mutex::new({
+LL | | let x = 1;
+LL | | dbg!(x);
+... |
+LL | | 2
+LL | | ];
+ | |_____^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Arc::downgrade(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v = {
+LL + let data = Arc::downgrade(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: initializing a reference-counted pointer in `vec![elem; len]`
+ --> $DIR/weak.rs:36:14
+ |
+LL | let v1 = vec![
+ | ______________^
+LL | | Rc::downgrade(&Rc::new(Mutex::new({
+LL | | let x = 1;
+LL | | dbg!(x);
+... |
+LL | | 2
+LL | | ];
+ | |_____^
+ |
+ = note: each element will point to the same `Weak` instance
+help: consider initializing each `Weak` element individually
+ |
+LL ~ let v1 = {
+LL + let mut v = Vec::with_capacity(2);
+LL + (0..2).for_each(|_| v.push(Rc::downgrade(..)));
+LL + v
+LL ~ };
+ |
+help: or if this is intentional, consider extracting the `Weak` initialization to a variable
+ |
+LL ~ let v1 = {
+LL + let data = Rc::downgrade(..);
+LL + vec![data; 2]
+LL ~ };
+ |
+
+error: aborting due to 8 previous errors
+