fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![allow(clippy::needless_borrow, clippy::needless_borrows_for_generic_args, clippy::ptr_arg)] 2#![warn(clippy::unnecessary_to_owned, clippy::redundant_clone)] 3 4use std::borrow::Cow; 5use std::ffi::{CStr, CString, OsStr, OsString}; 6use std::ops::Deref; 7 8#[derive(Clone)] 9struct X(String); 10 11impl Deref for X { 12 type Target = [u8]; 13 fn deref(&self) -> &[u8] { 14 self.0.as_bytes() 15 } 16} 17 18impl AsRef<str> for X { 19 fn as_ref(&self) -> &str { 20 self.0.as_str() 21 } 22} 23 24impl ToString for X { 25 fn to_string(&self) -> String { 26 self.0.to_string() 27 } 28} 29 30impl X { 31 fn join(&self, other: impl AsRef<str>) -> Self { 32 let mut s = self.0.clone(); 33 s.push_str(other.as_ref()); 34 Self(s) 35 } 36} 37 38#[allow(dead_code)] 39#[derive(Clone)] 40enum FileType { 41 Account, 42 PrivateKey, 43 Certificate, 44} 45 46fn main() { 47 let c_str = CStr::from_bytes_with_nul(&[0]).unwrap(); 48 let os_str = OsStr::new("x"); 49 let path = std::path::Path::new("x"); 50 let s = "x"; 51 let array = ["x"]; 52 let array_ref = &["x"]; 53 let slice = &["x"][..]; 54 let x = X(String::from("x")); 55 let x_ref = &x; 56 57 require_c_str(&Cow::from(c_str)); 58 require_c_str(c_str); 59 60 require_os_str(os_str); 61 require_os_str(&Cow::from(os_str)); 62 require_os_str(os_str); 63 64 require_path(path); 65 require_path(&Cow::from(path)); 66 require_path(path); 67 68 require_str(s); 69 require_str(&Cow::from(s)); 70 require_str(s); 71 require_str(x_ref.as_ref()); 72 73 require_slice(slice); 74 require_slice(&Cow::from(slice)); 75 require_slice(array.as_ref()); 76 require_slice(array_ref.as_ref()); 77 require_slice(slice); 78 require_slice(&x_ref.to_owned()); // No longer flagged because of #8759. 79 80 require_x(&Cow::<X>::Owned(x.clone())); 81 require_x(&x_ref.to_owned()); // No longer flagged because of #8759. 82 83 require_deref_c_str(c_str); 84 require_deref_os_str(os_str); 85 require_deref_path(path); 86 require_deref_str(s); 87 require_deref_slice(slice); 88 89 require_impl_deref_c_str(c_str); 90 require_impl_deref_os_str(os_str); 91 require_impl_deref_path(path); 92 require_impl_deref_str(s); 93 require_impl_deref_slice(slice); 94 95 require_deref_str_slice(s, slice); 96 require_deref_slice_str(slice, s); 97 98 require_as_ref_c_str(c_str); 99 require_as_ref_os_str(os_str); 100 require_as_ref_path(path); 101 require_as_ref_str(s); 102 require_as_ref_str(&x); 103 require_as_ref_slice(array); 104 require_as_ref_slice(array_ref); 105 require_as_ref_slice(slice); 106 107 require_impl_as_ref_c_str(c_str); 108 require_impl_as_ref_os_str(os_str); 109 require_impl_as_ref_path(path); 110 require_impl_as_ref_str(s); 111 require_impl_as_ref_str(&x); 112 require_impl_as_ref_slice(array); 113 require_impl_as_ref_slice(array_ref); 114 require_impl_as_ref_slice(slice); 115 116 require_as_ref_str_slice(s, array); 117 require_as_ref_str_slice(s, array_ref); 118 require_as_ref_str_slice(s, slice); 119 require_as_ref_slice_str(array, s); 120 require_as_ref_slice_str(array_ref, s); 121 require_as_ref_slice_str(slice, s); 122 123 let _ = x.join(x_ref); 124 125 let _ = slice.iter().copied(); 126 let _ = slice.iter().copied(); 127 let _ = [std::path::PathBuf::new()][..].iter().cloned(); 128 let _ = [std::path::PathBuf::new()][..].iter().cloned(); 129 130 let _ = slice.iter().copied(); 131 let _ = slice.iter().copied(); 132 let _ = [std::path::PathBuf::new()][..].iter().cloned(); 133 let _ = [std::path::PathBuf::new()][..].iter().cloned(); 134 135 let _ = check_files(&[FileType::Account]); 136 137 // negative tests 138 require_string(&s.to_string()); 139 require_string(&Cow::from(s).into_owned()); 140 require_string(&s.to_owned()); 141 require_string(&x_ref.to_string()); 142 143 // `X` isn't copy. 144 require_slice(&x.to_owned()); 145 require_deref_slice(x.to_owned()); 146 147 // The following should be flagged by `redundant_clone`, but not by this lint. 148 require_c_str(&CString::from_vec_with_nul(vec![0]).unwrap()); 149 require_os_str(&OsString::from("x")); 150 require_path(&std::path::PathBuf::from("x")); 151 require_str(&String::from("x")); 152 require_slice(&[String::from("x")]); 153} 154 155fn require_c_str(_: &CStr) {} 156fn require_os_str(_: &OsStr) {} 157fn require_path(_: &std::path::Path) {} 158fn require_str(_: &str) {} 159fn require_slice<T>(_: &[T]) {} 160fn require_x(_: &X) {} 161 162fn require_deref_c_str<T: Deref<Target = CStr>>(_: T) {} 163fn require_deref_os_str<T: Deref<Target = OsStr>>(_: T) {} 164fn require_deref_path<T: Deref<Target = std::path::Path>>(_: T) {} 165fn require_deref_str<T: Deref<Target = str>>(_: T) {} 166fn require_deref_slice<T, U: Deref<Target = [T]>>(_: U) {} 167 168fn require_impl_deref_c_str(_: impl Deref<Target = CStr>) {} 169fn require_impl_deref_os_str(_: impl Deref<Target = OsStr>) {} 170fn require_impl_deref_path(_: impl Deref<Target = std::path::Path>) {} 171fn require_impl_deref_str(_: impl Deref<Target = str>) {} 172fn require_impl_deref_slice<T>(_: impl Deref<Target = [T]>) {} 173 174fn require_deref_str_slice<T: Deref<Target = str>, U, V: Deref<Target = [U]>>(_: T, _: V) {} 175fn require_deref_slice_str<T, U: Deref<Target = [T]>, V: Deref<Target = str>>(_: U, _: V) {} 176 177fn require_as_ref_c_str<T: AsRef<CStr>>(_: T) {} 178fn require_as_ref_os_str<T: AsRef<OsStr>>(_: T) {} 179fn require_as_ref_path<T: AsRef<std::path::Path>>(_: T) {} 180fn require_as_ref_str<T: AsRef<str>>(_: T) {} 181fn require_as_ref_slice<T, U: AsRef<[T]>>(_: U) {} 182 183fn require_impl_as_ref_c_str(_: impl AsRef<CStr>) {} 184fn require_impl_as_ref_os_str(_: impl AsRef<OsStr>) {} 185fn require_impl_as_ref_path(_: impl AsRef<std::path::Path>) {} 186fn require_impl_as_ref_str(_: impl AsRef<str>) {} 187fn require_impl_as_ref_slice<T>(_: impl AsRef<[T]>) {} 188 189fn require_as_ref_str_slice<T: AsRef<str>, U, V: AsRef<[U]>>(_: T, _: V) {} 190fn require_as_ref_slice_str<T, U: AsRef<[T]>, V: AsRef<str>>(_: U, _: V) {} 191 192// `check_files` is based on: 193// https://github.com/breard-r/acmed/blob/1f0dcc32aadbc5e52de6d23b9703554c0f925113/acmed/src/storage.rs#L262 194fn check_files(file_types: &[FileType]) -> bool { 195 for t in file_types { 196 let path = match get_file_path(t) { 197 Ok(p) => p, 198 Err(_) => { 199 return false; 200 }, 201 }; 202 if !path.is_file() { 203 return false; 204 } 205 } 206 true 207} 208 209fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> { 210 Ok(std::path::PathBuf::new()) 211} 212 213fn require_string(_: &String) {} 214 215#[clippy::msrv = "1.35"] 216fn _msrv_1_35() { 217 // `copied` was stabilized in 1.36, so clippy should use `cloned`. 218 let _ = &["x"][..].iter().cloned(); 219} 220 221#[clippy::msrv = "1.36"] 222fn _msrv_1_36() { 223 let _ = &["x"][..].iter().copied(); 224} 225 226// https://github.com/rust-lang/rust-clippy/issues/8507 227mod issue_8507 { 228 #![allow(dead_code)] 229 230 struct Opaque<P>(P); 231 232 pub trait Abstracted {} 233 234 impl<P> Abstracted for Opaque<P> {} 235 236 fn build<P>(p: P) -> Opaque<P> 237 where 238 P: AsRef<str>, 239 { 240 Opaque(p) 241 } 242 243 // Should not lint. 244 fn test_str(s: &str) -> Box<dyn Abstracted> { 245 Box::new(build(s.to_string())) 246 } 247 248 // Should not lint. 249 fn test_x(x: super::X) -> Box<dyn Abstracted> { 250 Box::new(build(x)) 251 } 252 253 #[derive(Clone, Copy)] 254 struct Y(&'static str); 255 256 impl AsRef<str> for Y { 257 fn as_ref(&self) -> &str { 258 self.0 259 } 260 } 261 262 impl ToString for Y { 263 fn to_string(&self) -> String { 264 self.0.to_string() 265 } 266 } 267 268 // Should lint because Y is copy. 269 fn test_y(y: Y) -> Box<dyn Abstracted> { 270 Box::new(build(y)) 271 } 272} 273 274// https://github.com/rust-lang/rust-clippy/issues/8759 275mod issue_8759 { 276 #![allow(dead_code)] 277 278 #[derive(Default)] 279 struct View {} 280 281 impl std::borrow::ToOwned for View { 282 type Owned = View; 283 fn to_owned(&self) -> Self::Owned { 284 View {} 285 } 286 } 287 288 #[derive(Default)] 289 struct RenderWindow { 290 default_view: View, 291 } 292 293 impl RenderWindow { 294 fn default_view(&self) -> &View { 295 &self.default_view 296 } 297 fn set_view(&mut self, _view: &View) {} 298 } 299 300 fn main() { 301 let mut rw = RenderWindow::default(); 302 rw.set_view(&rw.default_view().to_owned()); 303 } 304} 305 306mod issue_8759_variant { 307 #![allow(dead_code)] 308 309 #[derive(Clone, Default)] 310 struct View {} 311 312 #[derive(Default)] 313 struct RenderWindow { 314 default_view: View, 315 } 316 317 impl RenderWindow { 318 fn default_view(&self) -> &View { 319 &self.default_view 320 } 321 fn set_view(&mut self, _view: &View) {} 322 } 323 324 fn main() { 325 let mut rw = RenderWindow::default(); 326 rw.set_view(&rw.default_view().to_owned()); 327 } 328} 329 330mod issue_9317 { 331 #![allow(dead_code)] 332 333 struct Bytes {} 334 335 impl ToString for Bytes { 336 fn to_string(&self) -> String { 337 "123".to_string() 338 } 339 } 340 341 impl AsRef<[u8]> for Bytes { 342 fn as_ref(&self) -> &[u8] { 343 &[1, 2, 3] 344 } 345 } 346 347 fn consume<C: AsRef<[u8]>>(c: C) { 348 let _ = c; 349 } 350 351 pub fn main() { 352 let b = Bytes {}; 353 // Should not lint. 354 consume(b.to_string()); 355 } 356} 357 358mod issue_9351 { 359 #![allow(dead_code)] 360 361 use std::ops::Deref; 362 use std::path::{Path, PathBuf}; 363 364 fn require_deref_path<T: Deref<Target = std::path::Path>>(x: T) -> T { 365 x 366 } 367 368 fn generic_arg_used_elsewhere<T: AsRef<Path>>(_x: T, _y: T) {} 369 370 fn id<T: AsRef<str>>(x: T) -> T { 371 x 372 } 373 374 fn predicates_are_satisfied(_x: impl std::fmt::Write) {} 375 376 // Should lint 377 fn single_return() -> impl AsRef<str> { 378 id("abc") 379 } 380 381 // Should not lint 382 fn multiple_returns(b: bool) -> impl AsRef<str> { 383 if b { 384 return String::new(); 385 } 386 387 id("abc".to_string()) 388 } 389 390 struct S1(String); 391 392 // Should not lint 393 fn fields1() -> S1 { 394 S1(id("abc".to_string())) 395 } 396 397 struct S2 { 398 s: String, 399 } 400 401 // Should not lint 402 fn fields2() { 403 let mut s = S2 { s: "abc".into() }; 404 s.s = id("abc".to_string()); 405 } 406 407 pub fn main() { 408 let path = std::path::Path::new("x"); 409 let path_buf = path.to_owned(); 410 411 // Should not lint. 412 let _x: PathBuf = require_deref_path(path.to_owned()); 413 generic_arg_used_elsewhere(path.to_owned(), path_buf); 414 predicates_are_satisfied(id("abc".to_string())); 415 } 416} 417 418mod issue_9504 { 419 #![allow(dead_code)] 420 421 async fn foo<S: AsRef<str>>(_: S) {} 422 async fn bar() { 423 foo(std::path::PathBuf::new().to_string_lossy().to_string()).await; 424 } 425} 426 427mod issue_9771a { 428 #![allow(dead_code)] 429 430 use std::marker::PhantomData; 431 432 pub struct Key<K: AsRef<[u8]>, V: ?Sized>(K, PhantomData<V>); 433 434 impl<K: AsRef<[u8]>, V: ?Sized> Key<K, V> { 435 pub fn new(key: K) -> Key<K, V> { 436 Key(key, PhantomData) 437 } 438 } 439 440 pub fn pkh(pkh: &[u8]) -> Key<Vec<u8>, String> { 441 Key::new([b"pkh-", pkh].concat().to_vec()) 442 } 443} 444 445mod issue_9771b { 446 #![allow(dead_code)] 447 448 pub struct Key<K: AsRef<[u8]>>(K); 449 450 pub fn from(c: &[u8]) -> Key<Vec<u8>> { 451 let v = [c].concat(); 452 Key(v.to_vec()) 453 } 454} 455 456// This is a watered down version of the code in: https://github.com/oxigraph/rio 457// The ICE is triggered by the call to `to_owned` on this line: 458// https://github.com/oxigraph/rio/blob/66635b9ff8e5423e58932353fa40d6e64e4820f7/testsuite/src/parser_evaluator.rs#L116 459mod issue_10021 { 460 #![allow(unused)] 461 462 pub struct Iri<T>(T); 463 464 impl<T: AsRef<str>> Iri<T> { 465 pub fn parse(iri: T) -> Result<Self, ()> { 466 unimplemented!() 467 } 468 } 469 470 pub fn parse_w3c_rdf_test_file(url: &str) -> Result<(), ()> { 471 let base_iri = Iri::parse(url.to_owned())?; 472 Ok(()) 473 } 474} 475 476mod issue_10033 { 477 #![allow(dead_code)] 478 use std::fmt::Display; 479 use std::ops::Deref; 480 481 fn _main() { 482 let f = Foo; 483 484 // Not actually unnecessary - this calls `Foo`'s `Display` impl, not `str`'s (even though `Foo` does 485 // deref to `str`) 486 foo(&f.to_string()); 487 } 488 489 fn foo(s: &str) { 490 println!("{}", s); 491 } 492 493 struct Foo; 494 495 impl Deref for Foo { 496 type Target = str; 497 498 fn deref(&self) -> &Self::Target { 499 "str" 500 } 501 } 502 503 impl Display for Foo { 504 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 505 write!(f, "Foo") 506 } 507 } 508}