fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![warn(clippy::implicit_clone)] 2#![allow(clippy::clone_on_copy, clippy::redundant_clone)] 3use std::borrow::Borrow; 4use std::ffi::{OsStr, OsString}; 5use std::path::PathBuf; 6 7fn return_owned_from_slice(slice: &[u32]) -> Vec<u32> { 8 slice.to_owned() 9} 10 11pub fn own_same<T>(v: T) -> T 12where 13 T: ToOwned<Owned = T>, 14{ 15 v.to_owned() 16} 17 18pub fn own_same_from_ref<T>(v: &T) -> T 19where 20 T: ToOwned<Owned = T>, 21{ 22 v.to_owned() 23} 24 25pub fn own_different<T, U>(v: T) -> U 26where 27 T: ToOwned<Owned = U>, 28{ 29 v.to_owned() 30} 31 32#[derive(Copy, Clone)] 33struct Kitten; 34impl Kitten { 35 // badly named method 36 fn to_vec(self) -> Kitten { 37 Kitten {} 38 } 39} 40impl Borrow<BorrowedKitten> for Kitten { 41 fn borrow(&self) -> &BorrowedKitten { 42 static VALUE: BorrowedKitten = BorrowedKitten {}; 43 &VALUE 44 } 45} 46 47struct BorrowedKitten; 48impl ToOwned for BorrowedKitten { 49 type Owned = Kitten; 50 fn to_owned(&self) -> Kitten { 51 Kitten {} 52 } 53} 54 55mod weird { 56 #[allow(clippy::ptr_arg)] 57 pub fn to_vec(v: &Vec<u32>) -> Vec<u32> { 58 v.clone() 59 } 60} 61 62fn main() { 63 let vec = vec![5]; 64 let _ = return_owned_from_slice(&vec); 65 let _ = vec.clone(); 66 let _ = vec.clone(); 67 68 let vec_ref = &vec; 69 let _ = return_owned_from_slice(vec_ref); 70 let _ = vec_ref.to_owned(); 71 let _ = vec_ref.clone(); 72 73 // we expect no lint for this 74 let _ = weird::to_vec(&vec); 75 76 // we expect no lints for this 77 let slice: &[u32] = &[1, 2, 3, 4, 5]; 78 let _ = return_owned_from_slice(slice); 79 let _ = slice.to_owned(); 80 let _ = slice.to_vec(); 81 82 let str = "hello world".to_string(); 83 let _ = str.clone(); 84 85 // testing w/ an arbitrary type 86 let kitten = Kitten {}; 87 let _ = kitten.clone(); 88 let _ = own_same_from_ref(&kitten); 89 // this shouldn't lint 90 let _ = kitten.to_vec(); 91 92 // we expect no lints for this 93 let borrowed = BorrowedKitten {}; 94 let _ = borrowed.to_owned(); 95 96 let pathbuf = PathBuf::new(); 97 let _ = pathbuf.clone(); 98 let _ = pathbuf.clone(); 99 100 let os_string = OsString::from("foo"); 101 let _ = os_string.clone(); 102 let _ = os_string.clone(); 103 104 // we expect no lints for this 105 let os_str = OsStr::new("foo"); 106 let _ = os_str.to_owned(); 107 let _ = os_str.to_os_string(); 108 109 // issue #8227 110 let pathbuf_ref = &pathbuf; 111 let pathbuf_ref = &pathbuf_ref; 112 let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&PathBuf` 113 let _ = (*pathbuf_ref).clone(); 114 let pathbuf_ref = &pathbuf_ref; 115 let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf` 116 let _ = (**pathbuf_ref).clone(); 117 118 struct NoClone; 119 impl ToOwned for NoClone { 120 type Owned = Self; 121 fn to_owned(&self) -> Self { 122 NoClone 123 } 124 } 125 let no_clone = &NoClone; 126 let _ = no_clone.to_owned(); 127}