3 #![allow(unused_variables, clippy::many_single_char_names, clippy::clone_double_ref)]
4 #![warn(clippy::explicit_deref_methods)]
6 use std::ops::{Deref, DerefMut};
8 fn concat(deref_str: &str) -> String {
9 format!("{}bar", deref_str)
12 fn just_return(deref_str: &str) -> &str {
16 struct CustomVec(Vec<u8>);
17 impl Deref for CustomVec {
18 type Target = Vec<u8>;
20 fn deref(&self) -> &Vec<u8> {
26 let a: &mut String = &mut String::from("foo");
28 // these should require linting
32 let b: &mut str = &mut **a;
34 // both derefs should get linted here
35 let b: String = format!("{}, {}", &*a, &*a);
39 #[allow(clippy::match_single_binding)]
44 let b: String = concat(&*a);
46 let b = just_return(a);
48 let b: String = concat(just_return(a));
52 let opt_a = Some(a.clone());
53 let b = &*opt_a.unwrap();
55 // following should not require linting
57 let cv = CustomVec(vec![0, 42]);
58 let c = cv.deref()[0];
60 let b: &str = &*a.deref();
62 let b: String = a.deref().clone();
64 let b: usize = a.deref_mut().len();
66 let b: &usize = &a.deref().len();
70 let b: &mut str = &mut *a;
72 macro_rules! expr_deref {
77 let b: &str = expr_deref!(a);
79 let b: &str = expr_deref!(&*a);
81 // The struct does not implement Deref trait
82 #[derive(Copy, Clone)]
85 pub fn deref(self) -> u32 {
88 pub fn deref_mut(self) -> u32 {
92 let no_lint = NoLint(42);
93 let b = no_lint.deref();
94 let b = no_lint.deref_mut();