5 clippy::clone_double_ref,
6 clippy::needless_borrow,
7 clippy::borrow_deref_ref,
8 clippy::explicit_auto_deref
10 #![warn(clippy::explicit_deref_methods)]
12 use std::ops::{Deref, DerefMut};
14 fn concat(deref_str: &str) -> String {
15 format!("{}bar", deref_str)
18 fn just_return(deref_str: &str) -> &str {
22 struct CustomVec(Vec<u8>);
23 impl Deref for CustomVec {
24 type Target = Vec<u8>;
26 fn deref(&self) -> &Vec<u8> {
32 let a: &mut String = &mut String::from("foo");
34 // these should require linting
36 let b: &str = a.deref();
38 let b: &mut str = a.deref_mut();
40 // both derefs should get linted here
41 let b: String = format!("{}, {}", a.deref(), a.deref());
43 println!("{}", a.deref());
45 #[allow(clippy::match_single_binding)]
50 let b: String = concat(a.deref());
52 let b = just_return(a).deref();
54 let b: String = concat(just_return(a).deref());
56 let b: &str = a.deref().deref();
58 let opt_a = Some(a.clone());
59 let b = opt_a.unwrap().deref();
61 // following should not require linting
63 let cv = CustomVec(vec![0, 42]);
64 let c = cv.deref()[0];
66 let b: &str = &*a.deref();
68 let b: String = a.deref().clone();
70 let b: usize = a.deref_mut().len();
72 let b: &usize = &a.deref().len();
76 let b: &mut str = &mut *a;
78 macro_rules! expr_deref {
83 let b: &str = expr_deref!(a);
85 let b: &str = expr_deref!(a.deref());
87 // The struct does not implement Deref trait
88 #[derive(Copy, Clone)]
91 pub fn deref(self) -> u32 {
94 pub fn deref_mut(self) -> u32 {
98 let no_lint = NoLint(42);
99 let b = no_lint.deref();
100 let b = no_lint.deref_mut();