Merge pull request #2058 from rust-lang-nursery/ptr_arg-vs-clone

add suggestions for .clone() in ptr_arg fns
This commit is contained in:
Oliver Schneider 2017-09-17 15:37:21 +02:00 committed by GitHub
commit 2bb8efdb4d
6 changed files with 189 additions and 45 deletions

View file

@ -1,6 +1,6 @@
#![feature(plugin)]
#![plugin(clippy)]
#![allow(unused)]
#![allow(unused, many_single_char_names)]
#![warn(ptr_arg)]
fn do_vec(x: &Vec<i64>) {
@ -34,5 +34,24 @@ struct Bar;
impl Foo for Bar {
type Item = Vec<u8>;
fn do_vec(x: &Vec<i64>) {}
fn do_item(x: &Vec<u8>) {}
fn do_item(x: &Vec<u8>) {}
}
fn cloned(x: &Vec<u8>) -> Vec<u8> {
let e = x.clone();
let f = e.clone(); // OK
let g = x;
let h = g.clone(); // Alas, we cannot reliably detect this without following data.
let i = (e).clone();
x.clone()
}
fn str_cloned(x: &String) -> String {
let a = x.clone();
let b = x.clone();
let c = b.clone();
let d = a.clone()
.clone()
.clone();
x.clone()
}

View file

@ -18,5 +18,47 @@ error: writing `&Vec<_>` instead of `&[_]` involves one more reference and canno
27 | fn do_vec(x: &Vec<i64>);
| ^^^^^^^^^ help: change this to: `&[i64]`
error: aborting due to 3 previous errors
error: writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used with non-Vec-based slices.
--> $DIR/ptr_arg.rs:40:14
|
40 | fn cloned(x: &Vec<u8>) -> Vec<u8> {
| ^^^^^^^^
|
help: change this to
|
40 | fn cloned(x: &[u8]) -> Vec<u8> {
| ^^^^^
help: change the `.clone()` to
|
41 | let e = x.to_owned();
| ^^^^^^^^^^^^
help: change the `.clone()` to
|
46 | x.to_owned()
| ^^^^^^^^^^^^
error: writing `&String` instead of `&str` involves a new object where a slice will do.
--> $DIR/ptr_arg.rs:49:18
|
49 | fn str_cloned(x: &String) -> String {
| ^^^^^^^
|
help: change this to
|
49 | fn str_cloned(x: &str) -> String {
| ^^^^
help: change the `.clone` to
|
50 | let a = x.to_string();
| ^^^^^^^^^^^^^
help: change the `.clone` to
|
51 | let b = x.to_string();
| ^^^^^^^^^^^^^
help: change the `.clone` to
|
56 | x.to_string()
| ^^^^^^^^^^^^^
error: aborting due to 5 previous errors