]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
should_impl_trait - ignore methods with lifetime params
[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     pub fn add(self, other: T) -> T {
41         self
42     }
43
44     // no error, not public interface
45     pub(crate) fn drop(&mut self) {}
46
47     // no error, private function
48     fn neg(self) -> Self {
49         self
50     }
51
52     // no error, private function
53     fn eq(&self, other: T) -> bool {
54         true
55     }
56
57     // No error; self is a ref.
58     fn sub(&self, other: T) -> &T {
59         self
60     }
61
62     // No error; different number of arguments.
63     fn div(self) -> T {
64         self
65     }
66
67     // No error; wrong return type.
68     fn rem(self, other: T) {}
69
70     // Fine
71     fn into_u32(self) -> u32 {
72         0
73     }
74
75     fn into_u16(&self) -> u16 {
76         0
77     }
78
79     fn to_something(self) -> u32 {
80         0
81     }
82
83     fn new(self) -> Self {
84         unimplemented!();
85     }
86
87     pub fn next<'b>(&'b mut self) -> Option<&'b mut T> {
88         unimplemented!();
89     }
90 }
91
92 pub struct T1;
93
94 impl T1 {
95     // Shouldn't trigger lint as it is unsafe.
96     pub unsafe fn add(self, rhs: T1) -> T1 {
97         self
98     }
99
100     // Should not trigger lint since this is an async function.
101     pub async fn next(&mut self) -> Option<T1> {
102         None
103     }
104 }
105
106 struct Lt<'a> {
107     foo: &'a u32,
108 }
109
110 impl<'a> Lt<'a> {
111     // The lifetime is different, but that’s irrelevant; see issue #734.
112     #[allow(clippy::needless_lifetimes)]
113     pub fn new<'b>(s: &'b str) -> Lt<'b> {
114         unimplemented!()
115     }
116 }
117
118 struct Lt2<'a> {
119     foo: &'a u32,
120 }
121
122 impl<'a> Lt2<'a> {
123     // The lifetime is different, but that’s irrelevant; see issue #734.
124     pub fn new(s: &str) -> Lt2 {
125         unimplemented!()
126     }
127 }
128
129 struct Lt3<'a> {
130     foo: &'a u32,
131 }
132
133 impl<'a> Lt3<'a> {
134     // The lifetime is different, but that’s irrelevant; see issue #734.
135     pub fn new() -> Lt3<'static> {
136         unimplemented!()
137     }
138 }
139
140 #[derive(Clone, Copy)]
141 struct U;
142
143 impl U {
144     fn new() -> Self {
145         U
146     }
147     // Ok because `U` is `Copy`.
148     fn to_something(self) -> u32 {
149         0
150     }
151 }
152
153 struct V<T> {
154     _dummy: T,
155 }
156
157 impl<T> V<T> {
158     fn new() -> Option<V<T>> {
159         None
160     }
161 }
162
163 struct AsyncNew;
164
165 impl AsyncNew {
166     async fn new() -> Option<Self> {
167         None
168     }
169 }
170
171 struct BadNew;
172
173 impl BadNew {
174     fn new() -> i32 {
175         0
176     }
177 }
178
179 impl Mul<T> for T {
180     type Output = T;
181     // No error, obviously.
182     fn mul(self, other: T) -> T {
183         self
184     }
185 }
186
187 /// Checks implementation of `FILTER_NEXT` lint.
188 #[rustfmt::skip]
189 fn filter_next() {
190     let v = vec![3, 2, 1, 0, -1, -2, -3];
191
192     // Single-line case.
193     let _ = v.iter().filter(|&x| *x < 0).next();
194
195     // Multi-line case.
196     let _ = v.iter().filter(|&x| {
197                                 *x < 0
198                             }
199                    ).next();
200
201     // Check that hat we don't lint if the caller is not an `Iterator`.
202     let foo = IteratorFalsePositives { foo: 0 };
203     let _ = foo.filter().next();
204 }
205
206 /// Checks implementation of `SEARCH_IS_SOME` lint.
207 #[rustfmt::skip]
208 fn search_is_some() {
209     let v = vec![3, 2, 1, 0, -1, -2, -3];
210     let y = &&42;
211
212     // Check `find().is_some()`, single-line case.
213     let _ = v.iter().find(|&x| *x < 0).is_some();
214     let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
215     let _ = (0..1).find(|x| *x == 0).is_some();
216     let _ = v.iter().find(|x| **x == 0).is_some();
217
218     // Check `find().is_some()`, multi-line case.
219     let _ = v.iter().find(|&x| {
220                               *x < 0
221                           }
222                    ).is_some();
223
224     // Check `position().is_some()`, single-line case.
225     let _ = v.iter().position(|&x| x < 0).is_some();
226
227     // Check `position().is_some()`, multi-line case.
228     let _ = v.iter().position(|&x| {
229                                   x < 0
230                               }
231                    ).is_some();
232
233     // Check `rposition().is_some()`, single-line case.
234     let _ = v.iter().rposition(|&x| x < 0).is_some();
235
236     // Check `rposition().is_some()`, multi-line case.
237     let _ = v.iter().rposition(|&x| {
238                                    x < 0
239                                }
240                    ).is_some();
241
242     // Check that we don't lint if the caller is not an `Iterator`.
243     let foo = IteratorFalsePositives { foo: 0 };
244     let _ = foo.find().is_some();
245     let _ = foo.position().is_some();
246     let _ = foo.rposition().is_some();
247 }
248
249 fn main() {
250     filter_next();
251     search_is_some();
252 }