diff --git a/tests/ui/methods.rs b/tests/ui/methods.rs index 4769906e2ff..4eb7846d3cc 100644 --- a/tests/ui/methods.rs +++ b/tests/ui/methods.rs @@ -454,60 +454,9 @@ fn str_extend_chars() { s.extend(f.chars()); } -fn clone_on_copy() { - 42.clone(); - - vec![1].clone(); // ok, not a Copy type - Some(vec![1]).clone(); // ok, not a Copy type - (&42).clone(); -} - -fn clone_on_ref_ptr() { - let rc = Rc::new(true); - let arc = Arc::new(true); - - let rcweak = Rc::downgrade(&rc); - let arc_weak = Arc::downgrade(&arc); - - rc.clone(); - Rc::clone(&rc); - - arc.clone(); - Arc::clone(&arc); - - rcweak.clone(); - rc::Weak::clone(&rcweak); - - arc_weak.clone(); - sync::Weak::clone(&arc_weak); - - -} - -fn clone_on_copy_generic(t: T) { - t.clone(); - - Some(t).clone(); -} - -fn clone_on_double_ref() { - let x = vec![1]; - let y = &&x; - let z: &Vec<_> = y.clone(); - - println!("{:p} {:p}",*y, z); -} - #[allow(result_unwrap_used)] fn temporary_cstring() { use std::ffi::CString; CString::new("foo").unwrap().as_ptr(); } - -fn iter_clone_collect() { - let v = [1,2,3,4,5]; - let v2 : Vec = v.iter().cloned().collect(); - let v3 : HashSet = v.iter().cloned().collect(); - let v4 : VecDeque = v.iter().cloned().collect(); -} diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index 2500c30b402..0bf7dc321c9 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -567,85 +567,17 @@ error: calling `.extend(_.chars())` 447 | s.extend(def.chars()); | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)` -error: using `clone` on a `Copy` type - --> $DIR/methods.rs:458:5 - | -458 | 42.clone(); - | ^^^^^^^^^^ help: try removing the `clone` call: `42` - | - = note: `-D clone-on-copy` implied by `-D warnings` - -error: using `clone` on a `Copy` type - --> $DIR/methods.rs:462:5 - | -462 | (&42).clone(); - | ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)` - -error: using '.clone()' on a ref-counted pointer - --> $DIR/methods.rs:472:5 - | -472 | rc.clone(); - | ^^^^^^^^^^ help: try this: `Rc::clone(&rc)` - | - = note: `-D clone-on-ref-ptr` implied by `-D warnings` - -error: using '.clone()' on a ref-counted pointer - --> $DIR/methods.rs:475:5 - | -475 | arc.clone(); - | ^^^^^^^^^^^ help: try this: `Arc::clone(&arc)` - -error: using '.clone()' on a ref-counted pointer - --> $DIR/methods.rs:478:5 - | -478 | rcweak.clone(); - | ^^^^^^^^^^^^^^ help: try this: `Weak::clone(&rcweak)` - -error: using '.clone()' on a ref-counted pointer - --> $DIR/methods.rs:481:5 - | -481 | arc_weak.clone(); - | ^^^^^^^^^^^^^^^^ help: try this: `Weak::clone(&arc_weak)` - -error: using `clone` on a `Copy` type - --> $DIR/methods.rs:488:5 - | -488 | t.clone(); - | ^^^^^^^^^ help: try removing the `clone` call: `t` - -error: using `clone` on a `Copy` type - --> $DIR/methods.rs:490:5 - | -490 | Some(t).clone(); - | ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)` - -error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type - --> $DIR/methods.rs:496:22 - | -496 | let z: &Vec<_> = y.clone(); - | ^^^^^^^^^ help: try dereferencing it: `(*y).clone()` - | - = note: `-D clone-double-ref` implied by `-D warnings` - error: you are getting the inner pointer of a temporary `CString` - --> $DIR/methods.rs:505:5 + --> $DIR/methods.rs:461:5 | -505 | CString::new("foo").unwrap().as_ptr(); +461 | CString::new("foo").unwrap().as_ptr(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D temporary-cstring-as-ptr` implied by `-D warnings` = note: that pointer will be invalid outside this expression help: assign the `CString` to a variable to extend its lifetime - --> $DIR/methods.rs:505:5 + --> $DIR/methods.rs:461:5 | -505 | CString::new("foo").unwrap().as_ptr(); +461 | CString::new("foo").unwrap().as_ptr(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: called `cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable - --> $DIR/methods.rs:510:27 - | -510 | let v2 : Vec = v.iter().cloned().collect(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `-D iter-cloned-collect` implied by `-D warnings` - diff --git a/tests/ui/unnecessary_clone.rs b/tests/ui/unnecessary_clone.rs new file mode 100644 index 00000000000..f33def9eb4e --- /dev/null +++ b/tests/ui/unnecessary_clone.rs @@ -0,0 +1,59 @@ +#![allow(unused)] + +use std::collections::HashSet; +use std::collections::VecDeque; +use std::rc::{self, Rc}; +use std::sync::{self, Arc}; + +fn main() {} + +fn clone_on_copy() { + 42.clone(); + + vec![1].clone(); // ok, not a Copy type + Some(vec![1]).clone(); // ok, not a Copy type + (&42).clone(); +} + +fn clone_on_ref_ptr() { + let rc = Rc::new(true); + let arc = Arc::new(true); + + let rcweak = Rc::downgrade(&rc); + let arc_weak = Arc::downgrade(&arc); + + rc.clone(); + Rc::clone(&rc); + + arc.clone(); + Arc::clone(&arc); + + rcweak.clone(); + rc::Weak::clone(&rcweak); + + arc_weak.clone(); + sync::Weak::clone(&arc_weak); + + +} + +fn clone_on_copy_generic(t: T) { + t.clone(); + + Some(t).clone(); +} + +fn clone_on_double_ref() { + let x = vec![1]; + let y = &&x; + let z: &Vec<_> = y.clone(); + + println!("{:p} {:p}",*y, z); +} + +fn iter_clone_collect() { + let v = [1,2,3,4,5]; + let v2 : Vec = v.iter().cloned().collect(); + let v3 : HashSet = v.iter().cloned().collect(); + let v4 : VecDeque = v.iter().cloned().collect(); +} diff --git a/tests/ui/unnecessary_clone.stderr b/tests/ui/unnecessary_clone.stderr new file mode 100644 index 00000000000..17263756980 --- /dev/null +++ b/tests/ui/unnecessary_clone.stderr @@ -0,0 +1,68 @@ +error: using `clone` on a `Copy` type + --> $DIR/unnecessary_clone.rs:11:5 + | +11 | 42.clone(); + | ^^^^^^^^^^ help: try removing the `clone` call: `42` + | + = note: `-D clone-on-copy` implied by `-D warnings` + +error: using `clone` on a `Copy` type + --> $DIR/unnecessary_clone.rs:15:5 + | +15 | (&42).clone(); + | ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)` + +error: using '.clone()' on a ref-counted pointer + --> $DIR/unnecessary_clone.rs:25:5 + | +25 | rc.clone(); + | ^^^^^^^^^^ help: try this: `Rc::clone(&rc)` + | + = note: `-D clone-on-ref-ptr` implied by `-D warnings` + +error: using '.clone()' on a ref-counted pointer + --> $DIR/unnecessary_clone.rs:28:5 + | +28 | arc.clone(); + | ^^^^^^^^^^^ help: try this: `Arc::clone(&arc)` + +error: using '.clone()' on a ref-counted pointer + --> $DIR/unnecessary_clone.rs:31:5 + | +31 | rcweak.clone(); + | ^^^^^^^^^^^^^^ help: try this: `Weak::clone(&rcweak)` + +error: using '.clone()' on a ref-counted pointer + --> $DIR/unnecessary_clone.rs:34:5 + | +34 | arc_weak.clone(); + | ^^^^^^^^^^^^^^^^ help: try this: `Weak::clone(&arc_weak)` + +error: using `clone` on a `Copy` type + --> $DIR/unnecessary_clone.rs:41:5 + | +41 | t.clone(); + | ^^^^^^^^^ help: try removing the `clone` call: `t` + +error: using `clone` on a `Copy` type + --> $DIR/unnecessary_clone.rs:43:5 + | +43 | Some(t).clone(); + | ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)` + +error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type + --> $DIR/unnecessary_clone.rs:49:22 + | +49 | let z: &Vec<_> = y.clone(); + | ^^^^^^^^^ help: try dereferencing it: `(*y).clone()` + | + = note: `-D clone-double-ref` implied by `-D warnings` + +error: called `cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable + --> $DIR/unnecessary_clone.rs:56:27 + | +56 | let v2 : Vec = v.iter().cloned().collect(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D iter-cloned-collect` implied by `-D warnings` +