|
| 1 | +#![warn(clippy::implicit_clone)] |
| 2 | +#![allow(clippy::redundant_clone)] |
| 3 | +use std::borrow::Borrow; |
| 4 | +use std::ffi::{OsStr, OsString}; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +fn return_owned_from_slice(slice: &[u32]) -> Vec<u32> { |
| 8 | + slice.to_owned() |
| 9 | +} |
| 10 | + |
| 11 | +pub fn own_same<T>(v: T) -> T |
| 12 | +where |
| 13 | + T: ToOwned<Owned = T>, |
| 14 | +{ |
| 15 | + v.to_owned() |
| 16 | +} |
| 17 | + |
| 18 | +pub fn own_same_from_ref<T>(v: &T) -> T |
| 19 | +where |
| 20 | + T: ToOwned<Owned = T>, |
| 21 | +{ |
| 22 | + v.to_owned() |
| 23 | +} |
| 24 | + |
| 25 | +pub fn own_different<T, U>(v: T) -> U |
| 26 | +where |
| 27 | + T: ToOwned<Owned = U>, |
| 28 | +{ |
| 29 | + v.to_owned() |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Copy, Clone)] |
| 33 | +struct Kitten {} |
| 34 | +impl Kitten { |
| 35 | + // badly named method |
| 36 | + fn to_vec(self) -> Kitten { |
| 37 | + Kitten {} |
| 38 | + } |
| 39 | +} |
| 40 | +impl Borrow<BorrowedKitten> for Kitten { |
| 41 | + fn borrow(&self) -> &BorrowedKitten { |
| 42 | + static VALUE: BorrowedKitten = BorrowedKitten {}; |
| 43 | + &VALUE |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +struct BorrowedKitten {} |
| 48 | +impl ToOwned for BorrowedKitten { |
| 49 | + type Owned = Kitten; |
| 50 | + fn to_owned(&self) -> Kitten { |
| 51 | + Kitten {} |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +mod weird { |
| 56 | + #[allow(clippy::ptr_arg)] |
| 57 | + pub fn to_vec(v: &Vec<u32>) -> Vec<u32> { |
| 58 | + v.clone() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn main() { |
| 63 | + let vec = vec![5]; |
| 64 | + let _ = return_owned_from_slice(&vec); |
| 65 | + let _ = vec.to_owned(); |
| 66 | + let _ = vec.to_vec(); |
| 67 | + |
| 68 | + let vec_ref = &vec; |
| 69 | + let _ = return_owned_from_slice(&vec_ref); |
| 70 | + let _ = vec_ref.to_owned(); |
| 71 | + let _ = vec_ref.to_vec(); |
| 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.to_owned(); |
| 84 | + |
| 85 | + // testing w/ an arbitrary type |
| 86 | + let kitten = Kitten {}; |
| 87 | + let _ = kitten.to_owned(); |
| 88 | + let _ = own_same_from_ref(&kitten); |
| 89 | + // this shouln'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.to_owned(); |
| 98 | + let _ = pathbuf.to_path_buf(); |
| 99 | + |
| 100 | + let os_string = OsString::from("foo"); |
| 101 | + let _ = os_string.to_owned(); |
| 102 | + let _ = os_string.to_os_string(); |
| 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 | +} |
0 commit comments