X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=tests%2Fui%2Fptr_arg.rs;h=00b99da2631c630aa1ef6e227e50c4c7f678f027;hb=611d0398143acef24bc6029810828791a2475b9e;hp=127ae7037021d741feb8f4677d9027e31afe51f7;hpb=bc76f397c6464d5946deaa8f658605a1c2f72ac9;p=rust.git diff --git a/tests/ui/ptr_arg.rs b/tests/ui/ptr_arg.rs index 127ae703702..00b99da2631 100644 --- a/tests/ui/ptr_arg.rs +++ b/tests/ui/ptr_arg.rs @@ -1,13 +1,14 @@ -#![feature(plugin)] -#![plugin(clippy)] -#![allow(unused, many_single_char_names)] -#![warn(ptr_arg)] +#![allow(unused, clippy::many_single_char_names, clippy::redundant_clone)] +#![warn(clippy::ptr_arg)] + +use std::borrow::Cow; +use std::path::PathBuf; fn do_vec(x: &Vec) { //Nothing here } -fn do_vec_mut(x: &mut Vec) { // no error here +fn do_vec_mut(x: &mut Vec) { //Nothing here } @@ -15,13 +16,20 @@ fn do_str(x: &String) { //Nothing here either } -fn do_str_mut(x: &mut String) { // no error here +fn do_str_mut(x: &mut String) { + //Nothing here either +} + +fn do_path(x: &PathBuf) { //Nothing here either } -fn main() { +fn do_path_mut(x: &mut PathBuf) { + //Nothing here either } +fn main() {} + trait Foo { type Item; fn do_vec(x: &Vec); @@ -41,7 +49,7 @@ fn cloned(x: &Vec) -> Vec { 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 h = g.clone(); let i = (e).clone(); x.clone() } @@ -50,9 +58,15 @@ fn str_cloned(x: &String) -> String { let a = x.clone(); let b = x.clone(); let c = b.clone(); - let d = a.clone() - .clone() - .clone(); + let d = a.clone().clone().clone(); + x.clone() +} + +fn path_cloned(x: &PathBuf) -> PathBuf { + let a = x.clone(); + let b = x.clone(); + let c = b.clone(); + let d = a.clone().clone().clone(); x.clone() } @@ -63,7 +77,112 @@ fn false_positive_capacity(x: &Vec, y: &String) { } fn false_positive_capacity_too(x: &String) -> String { - if x.capacity() > 1024 { panic!("Too large!"); } + if x.capacity() > 1024 { + panic!("Too large!"); + } x.clone() } +#[allow(dead_code)] +fn test_cow_with_ref(c: &Cow<[i32]>) {} + +fn test_cow(c: Cow<[i32]>) { + let _c = c; +} + +trait Foo2 { + fn do_string(&self); +} + +// no error for &self references where self is of type String (#2293) +impl Foo2 for String { + fn do_string(&self) {} +} + +// Check that the allow attribute on parameters is honored +mod issue_5644 { + use std::borrow::Cow; + use std::path::PathBuf; + + fn allowed( + #[allow(clippy::ptr_arg)] _v: &Vec, + #[allow(clippy::ptr_arg)] _s: &String, + #[allow(clippy::ptr_arg)] _p: &PathBuf, + #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>, + ) { + } + + struct S {} + impl S { + fn allowed( + #[allow(clippy::ptr_arg)] _v: &Vec, + #[allow(clippy::ptr_arg)] _s: &String, + #[allow(clippy::ptr_arg)] _p: &PathBuf, + #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>, + ) { + } + } + + trait T { + fn allowed( + #[allow(clippy::ptr_arg)] _v: &Vec, + #[allow(clippy::ptr_arg)] _s: &String, + #[allow(clippy::ptr_arg)] _p: &PathBuf, + #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>, + ) { + } + } +} + +mod issue6509 { + use std::path::PathBuf; + + fn foo_vec(vec: &Vec) { + let _ = vec.clone().pop(); + let _ = vec.clone().clone(); + } + + fn foo_path(path: &PathBuf) { + let _ = path.clone().pop(); + let _ = path.clone().clone(); + } + + fn foo_str(str: &PathBuf) { + let _ = str.clone().pop(); + let _ = str.clone().clone(); + } +} + +fn mut_vec_slice_methods(v: &mut Vec) { + v.copy_within(1..5, 10); +} + +fn mut_vec_vec_methods(v: &mut Vec) { + v.clear(); +} + +fn vec_contains(v: &Vec) -> bool { + [vec![], vec![0]].as_slice().contains(v) +} + +fn fn_requires_vec(v: &Vec) -> bool { + vec_contains(v) +} + +fn impl_fn_requires_vec(v: &Vec, f: impl Fn(&Vec)) { + f(v); +} + +fn dyn_fn_requires_vec(v: &Vec, f: &dyn Fn(&Vec)) { + f(v); +} + +// No error for types behind an alias (#7699) +type A = Vec; +fn aliased(a: &A) {} + +// Issue #8366 +pub trait Trait { + fn f(v: &mut Vec); + fn f2(v: &mut Vec) {} +}