unnecessary_clone: split rustfixable lint out into separate test

This commit is contained in:
Manish Goregaokar 2019-09-25 10:46:14 -07:00
parent 363e382f5b
commit 04dd580f37
5 changed files with 77 additions and 47 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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

View File

@ -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};
@ -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;

View File

@ -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