5 clippy::clone_double_ref,
6 clippy::needless_borrow,
7 clippy::borrow_deref_ref
9 #![warn(clippy::explicit_deref_methods)]
11 use std::ops::{Deref, DerefMut};
13 fn concat(deref_str: &str) -> String {
14 format!("{}bar", deref_str)
17 fn just_return(deref_str: &str) -> &str {
21 struct CustomVec(Vec<u8>);
22 impl Deref for CustomVec {
23 type Target = Vec<u8>;
25 fn deref(&self) -> &Vec<u8> {
31 let a: &mut String = &mut String::from("foo");
33 // these should require linting
35 let b: &str = a.deref();
37 let b: &mut str = a.deref_mut();
39 // both derefs should get linted here
40 let b: String = format!("{}, {}", a.deref(), a.deref());
42 println!("{}", a.deref());
44 #[allow(clippy::match_single_binding)]
49 let b: String = concat(a.deref());
51 let b = just_return(a).deref();
53 let b: String = concat(just_return(a).deref());
55 let b: &str = a.deref().deref();
57 let opt_a = Some(a.clone());
58 let b = opt_a.unwrap().deref();
60 // following should not require linting
62 let cv = CustomVec(vec![0, 42]);
63 let c = cv.deref()[0];
65 let b: &str = &*a.deref();
67 let b: String = a.deref().clone();
69 let b: usize = a.deref_mut().len();
71 let b: &usize = &a.deref().len();
75 let b: &mut str = &mut *a;
77 macro_rules! expr_deref {
82 let b: &str = expr_deref!(a);
84 let b: &str = expr_deref!(a.deref());
86 // The struct does not implement Deref trait
87 #[derive(Copy, Clone)]
90 pub fn deref(self) -> u32 {
93 pub fn deref_mut(self) -> u32 {
97 let no_lint = NoLint(42);
98 let b = no_lint.deref();
99 let b = no_lint.deref_mut();