]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
should_implement_trait - add test cases for every checked trait method
[rust.git] / tests / ui / methods.rs
1 // aux-build:option_helpers.rs
2 // edition:2018
3
4 #![warn(clippy::all, clippy::pedantic)]
5 #![allow(
6     clippy::blacklisted_name,
7     clippy::default_trait_access,
8     clippy::missing_docs_in_private_items,
9     clippy::missing_safety_doc,
10     clippy::non_ascii_literal,
11     clippy::new_without_default,
12     clippy::needless_pass_by_value,
13     clippy::needless_lifetimes,
14     clippy::print_stdout,
15     clippy::must_use_candidate,
16     clippy::use_self,
17     clippy::useless_format,
18     clippy::wrong_self_convention,
19     clippy::unused_self,
20     unused
21 )]
22
23 #[macro_use]
24 extern crate option_helpers;
25
26 use std::collections::BTreeMap;
27 use std::collections::HashMap;
28 use std::collections::HashSet;
29 use std::collections::VecDeque;
30 use std::iter::FromIterator;
31 use std::ops::Mul;
32 use std::rc::{self, Rc};
33 use std::sync::{self, Arc};
34
35 use option_helpers::IteratorFalsePositives;
36
37 pub struct T;
38
39 impl T {
40     // *******************************************
41     // complete trait method list, should lint all
42     // *******************************************
43     pub fn add(self, other: T) -> T {
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!()
161     }
162     // *****************
163     // complete list end
164     // *****************
165 }
166
167 pub struct T1;
168 impl T1 {
169     // corner cases: should not lint
170
171     // no error, not public interface
172     pub(crate) fn drop(&mut self) {}
173
174     // no error, private function
175     fn neg(self) -> Self {
176         self
177     }
178
179     // no error, private function
180     fn eq(&self, other: Self) -> bool {
181         true
182     }
183
184     // No error; self is a ref.
185     fn sub(&self, other: Self) -> &Self {
186         self
187     }
188
189     // No error; different number of arguments.
190     fn div(self) -> Self {
191         self
192     }
193
194     // No error; wrong return type.
195     fn rem(self, other: Self) {}
196
197     // Fine
198     fn into_u32(self) -> u32 {
199         0
200     }
201
202     fn into_u16(&self) -> u16 {
203         0
204     }
205
206     fn to_something(self) -> u32 {
207         0
208     }
209
210     fn new(self) -> Self {
211         unimplemented!();
212     }
213
214     pub fn next<'b>(&'b mut self) -> Option<&'b mut T> {
215         unimplemented!();
216     }
217 }
218
219 pub struct T2;
220 impl T2 {
221     // Shouldn't trigger lint as it is unsafe.
222     pub unsafe fn add(self, rhs: Self) -> Self {
223         self
224     }
225
226     // Should not trigger lint since this is an async function.
227     pub async fn next(&mut self) -> Option<Self> {
228         None
229     }
230 }
231
232 struct Lt<'a> {
233     foo: &'a u32,
234 }
235
236 impl<'a> Lt<'a> {
237     // The lifetime is different, but that’s irrelevant; see issue #734.
238     #[allow(clippy::needless_lifetimes)]
239     pub fn new<'b>(s: &'b str) -> Lt<'b> {
240         unimplemented!()
241     }
242 }
243
244 struct Lt2<'a> {
245     foo: &'a u32,
246 }
247
248 impl<'a> Lt2<'a> {
249     // The lifetime is different, but that’s irrelevant; see issue #734.
250     pub fn new(s: &str) -> Lt2 {
251         unimplemented!()
252     }
253 }
254
255 struct Lt3<'a> {
256     foo: &'a u32,
257 }
258
259 impl<'a> Lt3<'a> {
260     // The lifetime is different, but that’s irrelevant; see issue #734.
261     pub fn new() -> Lt3<'static> {
262         unimplemented!()
263     }
264 }
265
266 #[derive(Clone, Copy)]
267 struct U;
268
269 impl U {
270     fn new() -> Self {
271         U
272     }
273     // Ok because `U` is `Copy`.
274     fn to_something(self) -> u32 {
275         0
276     }
277 }
278
279 struct V<T> {
280     _dummy: T,
281 }
282
283 impl<T> V<T> {
284     fn new() -> Option<V<T>> {
285         None
286     }
287 }
288
289 struct AsyncNew;
290
291 impl AsyncNew {
292     async fn new() -> Option<Self> {
293         None
294     }
295 }
296
297 struct BadNew;
298
299 impl BadNew {
300     fn new() -> i32 {
301         0
302     }
303 }
304
305 impl Mul<T> for T {
306     type Output = T;
307     // No error, obviously.
308     fn mul(self, other: T) -> T {
309         self
310     }
311 }
312
313 /// Checks implementation of `FILTER_NEXT` lint.
314 #[rustfmt::skip]
315 fn filter_next() {
316     let v = vec![3, 2, 1, 0, -1, -2, -3];
317
318     // Single-line case.
319     let _ = v.iter().filter(|&x| *x < 0).next();
320
321     // Multi-line case.
322     let _ = v.iter().filter(|&x| {
323                                 *x < 0
324                             }
325                    ).next();
326
327     // Check that hat we don't lint if the caller is not an `Iterator`.
328     let foo = IteratorFalsePositives { foo: 0 };
329     let _ = foo.filter().next();
330 }
331
332 /// Checks implementation of `SEARCH_IS_SOME` lint.
333 #[rustfmt::skip]
334 fn search_is_some() {
335     let v = vec![3, 2, 1, 0, -1, -2, -3];
336     let y = &&42;
337
338     // Check `find().is_some()`, single-line case.
339     let _ = v.iter().find(|&x| *x < 0).is_some();
340     let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
341     let _ = (0..1).find(|x| *x == 0).is_some();
342     let _ = v.iter().find(|x| **x == 0).is_some();
343
344     // Check `find().is_some()`, multi-line case.
345     let _ = v.iter().find(|&x| {
346                               *x < 0
347                           }
348                    ).is_some();
349
350     // Check `position().is_some()`, single-line case.
351     let _ = v.iter().position(|&x| x < 0).is_some();
352
353     // Check `position().is_some()`, multi-line case.
354     let _ = v.iter().position(|&x| {
355                                   x < 0
356                               }
357                    ).is_some();
358
359     // Check `rposition().is_some()`, single-line case.
360     let _ = v.iter().rposition(|&x| x < 0).is_some();
361
362     // Check `rposition().is_some()`, multi-line case.
363     let _ = v.iter().rposition(|&x| {
364                                    x < 0
365                                }
366                    ).is_some();
367
368     // Check that we don't lint if the caller is not an `Iterator`.
369     let foo = IteratorFalsePositives { foo: 0 };
370     let _ = foo.find().is_some();
371     let _ = foo.position().is_some();
372     let _ = foo.rposition().is_some();
373 }
374
375 fn main() {
376     filter_next();
377     search_is_some();
378 }