Skip to content

Commit 2bc0ecd

Browse files
committed
should_implement_trait - add test cases for every checked trait method
1 parent a77e881 commit 2bc0ecd

File tree

3 files changed

+386
-25
lines changed

3 files changed

+386
-25
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,6 +3424,7 @@ const TRAIT_METHODS: [(&str, usize, &hir::FnHeader, SelfKind, OutType, &str); 30
34243424
("borrow_mut", 1, &FN_HEADER, SelfKind::RefMut, OutType::Ref, "std::borrow::BorrowMut"),
34253425
("clone", 1, &FN_HEADER, SelfKind::Ref, OutType::Any, "std::clone::Clone"),
34263426
("cmp", 2, &FN_HEADER, SelfKind::Ref, OutType::Any, "std::cmp::Ord"),
3427+
// FIXME: default doesn't work
34273428
("default", 0, &FN_HEADER, SelfKind::No, OutType::Any, "std::default::Default"),
34283429
("deref", 1, &FN_HEADER, SelfKind::Ref, OutType::Ref, "std::ops::Deref"),
34293430
("deref_mut", 1, &FN_HEADER, SelfKind::RefMut, OutType::Ref, "std::ops::DerefMut"),

tests/ui/methods.rs

Lines changed: 136 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,136 @@ use option_helpers::IteratorFalsePositives;
3737
pub struct T;
3838

3939
impl T {
40+
// *******************************************
41+
// complete trait method list, should lint all
42+
// *******************************************
4043
pub fn add(self, other: T) -> T {
41-
self
44+
unimplemented!()
45+
}
46+
47+
pub fn as_mut(&mut self) -> &mut T {
48+
unimplemented!()
49+
}
50+
51+
pub fn as_ref(&self) -> &T {
52+
unimplemented!()
53+
}
54+
55+
pub fn bitand(self, rhs: T) -> T {
56+
unimplemented!()
57+
}
58+
59+
pub fn bitor(self, rhs: Self) -> Self {
60+
unimplemented!()
61+
}
62+
63+
pub fn bitxor(self, rhs: Self) -> Self {
64+
unimplemented!()
65+
}
66+
67+
pub fn borrow(&self) -> &str {
68+
unimplemented!()
69+
}
70+
71+
pub fn borrow_mut(&mut self) -> &mut str {
72+
unimplemented!()
73+
}
74+
75+
pub fn clone(&self) -> Self {
76+
unimplemented!()
77+
}
78+
79+
pub fn cmp(&self, other: &Self) -> Self {
80+
unimplemented!()
81+
}
82+
83+
pub fn default() -> Self {
84+
unimplemented!()
85+
}
86+
87+
pub fn deref(&self) -> &Self {
88+
unimplemented!()
89+
}
90+
91+
pub fn deref_mut(&mut self) -> &mut Self {
92+
unimplemented!()
93+
}
94+
95+
pub fn div(self, rhs: Self) -> Self {
96+
unimplemented!()
97+
}
98+
99+
pub fn drop(&mut self) {
100+
unimplemented!()
101+
}
102+
103+
pub fn eq(&self, other: &Self) -> bool {
104+
unimplemented!()
105+
}
106+
107+
pub fn from_iter<T>(iter: T) -> Self {
108+
unimplemented!()
109+
}
110+
111+
pub fn from_str(s: &str) -> Result<Self, Self> {
112+
unimplemented!()
113+
}
114+
115+
pub fn hash(&self, state: &mut T) {
116+
unimplemented!()
117+
}
118+
119+
pub fn index(&self, index: usize) -> &Self {
120+
unimplemented!()
121+
}
122+
123+
pub fn index_mut(&mut self, index: usize) -> &mut Self {
124+
unimplemented!()
125+
}
126+
127+
pub fn into_iter(self) -> Self {
128+
unimplemented!()
129+
}
130+
131+
pub fn mul(self, rhs: Self) -> Self {
132+
unimplemented!()
133+
}
134+
135+
pub fn neg(self) -> Self {
136+
unimplemented!()
137+
}
138+
139+
pub fn next(&mut self) -> Option<Self> {
140+
unimplemented!()
141+
}
142+
143+
pub fn not(self) -> Self {
144+
unimplemented!()
145+
}
146+
147+
pub fn rem(self, rhs: Self) -> Self {
148+
unimplemented!()
149+
}
150+
151+
pub fn shl(self, rhs: Self) -> Self {
152+
unimplemented!()
153+
}
154+
155+
pub fn shr(self, rhs: Self) -> Self {
156+
unimplemented!()
157+
}
158+
159+
pub fn sub(self, rhs: Self) -> Self {
160+
unimplemented!()
42161
}
162+
// *****************
163+
// complete list end
164+
// *****************
165+
}
166+
167+
pub struct T1;
168+
impl T1 {
169+
// corner cases: should not lint
43170

44171
// no error, not public interface
45172
pub(crate) fn drop(&mut self) {}
@@ -50,22 +177,22 @@ impl T {
50177
}
51178

52179
// no error, private function
53-
fn eq(&self, other: T) -> bool {
180+
fn eq(&self, other: Self) -> bool {
54181
true
55182
}
56183

57184
// No error; self is a ref.
58-
fn sub(&self, other: T) -> &T {
185+
fn sub(&self, other: Self) -> &Self {
59186
self
60187
}
61188

62189
// No error; different number of arguments.
63-
fn div(self) -> T {
190+
fn div(self) -> Self {
64191
self
65192
}
66193

67194
// No error; wrong return type.
68-
fn rem(self, other: T) {}
195+
fn rem(self, other: Self) {}
69196

70197
// Fine
71198
fn into_u32(self) -> u32 {
@@ -89,16 +216,15 @@ impl T {
89216
}
90217
}
91218

92-
pub struct T1;
93-
94-
impl T1 {
219+
pub struct T2;
220+
impl T2 {
95221
// Shouldn't trigger lint as it is unsafe.
96-
pub unsafe fn add(self, rhs: T1) -> T1 {
222+
pub unsafe fn add(self, rhs: Self) -> Self {
97223
self
98224
}
99225

100226
// Should not trigger lint since this is an async function.
101-
pub async fn next(&mut self) -> Option<T1> {
227+
pub async fn next(&mut self) -> Option<Self> {
102228
None
103229
}
104230
}

0 commit comments

Comments
 (0)