1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#![warn(clippy::explicit_deref_methods)]
#![allow(unused_variables, unused_must_use)]
#![allow(
clippy::borrow_deref_ref,
suspicious_double_ref_op,
noop_method_call,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::no_effect,
clippy::uninlined_format_args,
clippy::unnecessary_literal_unwrap,
clippy::deref_addrof
)]
use std::ops::{Deref, DerefMut};
fn concat(deref_str: &str) -> String {
format!("{}bar", deref_str)
}
fn just_return(deref_str: &str) -> &str {
deref_str
}
struct CustomVec(Vec<u8>);
impl Deref for CustomVec {
type Target = Vec<u8>;
fn deref(&self) -> &Vec<u8> {
&self.0
}
}
struct Aaa;
impl Deref for Aaa {
type Target = ();
fn deref(&self) -> &Self::Target {
todo!();
}
}
impl DerefMut for Aaa {
fn deref_mut(&mut self) -> &mut Self::Target {
todo!();
}
}
fn main() {
let a: &mut String = &mut String::from("foo");
// these should require linting
let b: &str = a.deref();
//~^ explicit_deref_methods
let b: &mut str = a.deref_mut();
//~^ explicit_deref_methods
// both derefs should get linted here
let b: String = format!("{}, {}", a.deref(), a.deref());
//~^ explicit_deref_methods
//~| explicit_deref_methods
println!("{}", a.deref());
//~^ explicit_deref_methods
#[allow(clippy::match_single_binding)]
match a.deref() {
//~^ explicit_deref_methods
_ => (),
}
let b: String = concat(a.deref());
//~^ explicit_deref_methods
let b = just_return(a).deref();
//~^ explicit_deref_methods
let b: String = concat(just_return(a).deref());
//~^ explicit_deref_methods
let b: &str = a.deref().deref();
let opt_a = Some(a.clone());
let b = opt_a.unwrap().deref();
Aaa::deref(&Aaa);
Aaa::deref_mut(&mut Aaa);
<Aaa as Deref>::deref(&Aaa);
<Aaa as DerefMut>::deref_mut(&mut Aaa);
let mut aaa = Aaa;
Aaa::deref(&aaa);
Aaa::deref_mut(&mut aaa);
// following should not require linting
let cv = CustomVec(vec![0, 42]);
let c = cv.deref()[0];
let b: &str = &*a.deref();
let b: String = a.deref().clone();
let b: usize = a.deref_mut().len();
let b: &usize = &a.deref().len();
let b: &str = &*a;
let b: &mut str = &mut *a;
macro_rules! expr_deref {
($body:expr) => {
$body.deref()
};
}
let b: &str = expr_deref!(a);
let b: &str = expr_deref!(a.deref());
//~^ explicit_deref_methods
// The struct does not implement Deref trait
#[derive(Copy, Clone)]
struct NoLint(u32);
impl NoLint {
pub fn deref(self) -> u32 {
self.0
}
pub fn deref_mut(self) -> u32 {
self.0
}
}
let no_lint = NoLint(42);
let b = no_lint.deref();
let b = no_lint.deref_mut();
let _ = &Deref::deref(&"foo"); //~ explicit_deref_methods
let mut x = String::new();
let _ = &DerefMut::deref_mut(&mut x); //~ explicit_deref_methods
let _ = &DerefMut::deref_mut((&mut &mut x).deref_mut()); //~ explicit_deref_methods
}
|