Skip to content

Commit

Permalink
unnecessary_clone: split rustfixable lint out into separate test
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Sep 25, 2019
1 parent 362d0c8 commit 218f8bf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 47 deletions.
22 changes: 22 additions & 0 deletions tests/ui/iter_cloned_collect.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// run-rustfix

#![allow(unused)]

use std::collections::HashSet;
use std::collections::VecDeque;

fn main() {
let v = [1, 2, 3, 4, 5];
let v2: Vec<isize> = v.to_vec();
let v3: HashSet<isize> = v.iter().cloned().collect();
let v4: VecDeque<isize> = v.iter().cloned().collect();

// Handle macro expansion in suggestion
let _: Vec<isize> = vec![1, 2, 3].to_vec();

// Issue #3704
unsafe {
let _: Vec<u8> = std::ffi::CStr::from_ptr(std::ptr::null())
.to_bytes().to_vec();
}
}
25 changes: 25 additions & 0 deletions tests/ui/iter_cloned_collect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// run-rustfix

#![allow(unused)]

use std::collections::HashSet;
use std::collections::VecDeque;

fn main() {
let v = [1, 2, 3, 4, 5];
let v2: Vec<isize> = v.iter().cloned().collect();
let v3: HashSet<isize> = v.iter().cloned().collect();
let v4: VecDeque<isize> = v.iter().cloned().collect();

// Handle macro expansion in suggestion
let _: Vec<isize> = vec![1, 2, 3].iter().cloned().collect();

// Issue #3704
unsafe {
let _: Vec<u8> = std::ffi::CStr::from_ptr(std::ptr::null())
.to_bytes()
.iter()
.cloned()
.collect();
}
}
26 changes: 26 additions & 0 deletions tests/ui/iter_cloned_collect.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
--> $DIR/iter_cloned_collect.rs:10:27
|
LL | let v2: Vec<isize> = v.iter().cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`
|
= note: `-D clippy::iter-cloned-collect` implied by `-D warnings`

error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
--> $DIR/iter_cloned_collect.rs:15:38
|
LL | let _: Vec<isize> = vec![1, 2, 3].iter().cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`

error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
--> $DIR/iter_cloned_collect.rs:20:24
|
LL | .to_bytes()
| ________________________^
LL | | .iter()
LL | | .cloned()
LL | | .collect();
| |______________________^ help: try: `.to_vec()`

error: aborting due to 3 previous errors

23 changes: 2 additions & 21 deletions tests/ui/unnecessary_clone.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// does not test any rustfixable lints

#![warn(clippy::clone_on_ref_ptr)]
#![allow(unused)]

use std::cell::RefCell;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

Expand Down Expand Up @@ -66,25 +66,6 @@ fn clone_on_double_ref() {
println!("{:p} {:p}", *y, z);
}

fn iter_clone_collect() {
let v = [1, 2, 3, 4, 5];
let v2: Vec<isize> = v.iter().cloned().collect();
let v3: HashSet<isize> = v.iter().cloned().collect();
let v4: VecDeque<isize> = v.iter().cloned().collect();

// Handle macro expansion in suggestion
let _: Vec<isize> = vec![1, 2, 3].iter().cloned().collect();

// Issue #3704
unsafe {
let _: Vec<u8> = std::ffi::CStr::from_ptr(std::ptr::null())
.to_bytes()
.iter()
.cloned()
.collect();
}
}

mod many_derefs {
struct A;
struct B;
Expand Down
28 changes: 2 additions & 26 deletions tests/ui/unnecessary_clone.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,11 @@ help: or try being explicit about what type to clone
LL | let z: &Vec<_> = &std::vec::Vec<i32>::clone(y);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
--> $DIR/unnecessary_clone.rs:71:27
|
LL | let v2: Vec<isize> = v.iter().cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`
|
= note: `-D clippy::iter-cloned-collect` implied by `-D warnings`

error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
--> $DIR/unnecessary_clone.rs:76:38
|
LL | let _: Vec<isize> = vec![1, 2, 3].iter().cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`

error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
--> $DIR/unnecessary_clone.rs:81:24
|
LL | .to_bytes()
| ________________________^
LL | | .iter()
LL | | .cloned()
LL | | .collect();
| |______________________^ help: try: `.to_vec()`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:119:20
--> $DIR/unnecessary_clone.rs:100:20
|
LL | let _: E = a.clone();
| ^^^^^^^^^ help: try dereferencing it: `*****a`

error: aborting due to 15 previous errors
error: aborting due to 12 previous errors

0 comments on commit 218f8bf

Please sign in to comment.