]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
new_ret_no_self: walk associated types in impl Trait return types
[rust.git] / tests / ui / methods.rs
1 // aux-build:option_helpers.rs
2
3 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
4 #![allow(
5     clippy::blacklisted_name,
6     unused,
7     clippy::print_stdout,
8     clippy::non_ascii_literal,
9     clippy::new_without_default,
10     clippy::missing_docs_in_private_items,
11     clippy::needless_pass_by_value,
12     clippy::default_trait_access,
13     clippy::use_self,
14     clippy::new_ret_no_self,
15     clippy::useless_format,
16     clippy::wrong_self_convention
17 )]
18
19 #[macro_use]
20 extern crate option_helpers;
21
22 use std::collections::BTreeMap;
23 use std::collections::HashMap;
24 use std::collections::HashSet;
25 use std::collections::VecDeque;
26 use std::future::Future;
27 use std::iter::FromIterator;
28 use std::ops::Mul;
29 use std::pin::Pin;
30 use std::rc::{self, Rc};
31 use std::sync::{self, Arc};
32 use std::task::{Context, Poll};
33
34 use option_helpers::IteratorFalsePositives;
35
36 pub struct T;
37
38 impl T {
39     pub fn add(self, other: T) -> T {
40         self
41     }
42
43     // no error, not public interface
44     pub(crate) fn drop(&mut self) {}
45
46     // no error, private function
47     fn neg(self) -> Self {
48         self
49     }
50
51     // no error, private function
52     fn eq(&self, other: T) -> bool {
53         true
54     }
55
56     // No error; self is a ref.
57     fn sub(&self, other: T) -> &T {
58         self
59     }
60
61     // No error; different number of arguments.
62     fn div(self) -> T {
63         self
64     }
65
66     // No error; wrong return type.
67     fn rem(self, other: T) {}
68
69     // Fine
70     fn into_u32(self) -> u32 {
71         0
72     }
73
74     fn into_u16(&self) -> u16 {
75         0
76     }
77
78     fn to_something(self) -> u32 {
79         0
80     }
81
82     fn new(self) -> Self {
83         unimplemented!();
84     }
85 }
86
87 struct Lt<'a> {
88     foo: &'a u32,
89 }
90
91 impl<'a> Lt<'a> {
92     // The lifetime is different, but that’s irrelevant; see issue #734.
93     #[allow(clippy::needless_lifetimes)]
94     pub fn new<'b>(s: &'b str) -> Lt<'b> {
95         unimplemented!()
96     }
97 }
98
99 struct Lt2<'a> {
100     foo: &'a u32,
101 }
102
103 impl<'a> Lt2<'a> {
104     // The lifetime is different, but that’s irrelevant; see issue #734.
105     pub fn new(s: &str) -> Lt2 {
106         unimplemented!()
107     }
108 }
109
110 struct Lt3<'a> {
111     foo: &'a u32,
112 }
113
114 impl<'a> Lt3<'a> {
115     // The lifetime is different, but that’s irrelevant; see issue #734.
116     pub fn new() -> Lt3<'static> {
117         unimplemented!()
118     }
119 }
120
121 #[derive(Clone, Copy)]
122 struct U;
123
124 impl U {
125     fn new() -> Self {
126         U
127     }
128     // Ok because `U` is `Copy`.
129     fn to_something(self) -> u32 {
130         0
131     }
132 }
133
134 struct V<T> {
135     _dummy: T,
136 }
137
138 impl<T> V<T> {
139     fn new() -> Option<V<T>> {
140         None
141     }
142 }
143
144 struct AsyncNew;
145
146 impl AsyncNew {
147     fn new() -> impl Future<Output = Option<Self>> {
148         struct F;
149         impl Future for F {
150             type Output = Option<AsyncNew>;
151             fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
152                 unimplemented!()
153             }
154         }
155         F
156     }
157 }
158
159 impl Mul<T> for T {
160     type Output = T;
161     // No error, obviously.
162     fn mul(self, other: T) -> T {
163         self
164     }
165 }
166
167 /// Checks implementation of the following lints:
168 /// * `OPTION_MAP_UNWRAP_OR`
169 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
170 #[rustfmt::skip]
171 fn option_methods() {
172     let opt = Some(1);
173
174     // Check `OPTION_MAP_UNWRAP_OR`.
175     // Single line case.
176     let _ = opt.map(|x| x + 1)
177                 // Should lint even though this call is on a separate line.
178                .unwrap_or(0);
179     // Multi-line cases.
180     let _ = opt.map(|x| {
181                         x + 1
182                     }
183               ).unwrap_or(0);
184     let _ = opt.map(|x| x + 1)
185                .unwrap_or({
186                     0
187                 });
188     // Single line `map(f).unwrap_or(None)` case.
189     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
190     // Multi-line `map(f).unwrap_or(None)` cases.
191     let _ = opt.map(|x| {
192         Some(x + 1)
193     }
194     ).unwrap_or(None);
195     let _ = opt
196         .map(|x| Some(x + 1))
197         .unwrap_or(None);
198     // macro case
199     let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
200
201     // Should not lint if not copyable
202     let id: String = "identifier".to_string();
203     let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
204     // ...but DO lint if the `unwrap_or` argument is not used in the `map`
205     let id: String = "identifier".to_string();
206     let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
207
208     // Check OPTION_MAP_UNWRAP_OR_ELSE
209     // single line case
210     let _ = opt.map(|x| x + 1)
211                 // Should lint even though this call is on a separate line.
212                .unwrap_or_else(|| 0);
213     // Multi-line cases.
214     let _ = opt.map(|x| {
215                         x + 1
216                     }
217               ).unwrap_or_else(|| 0);
218     let _ = opt.map(|x| x + 1)
219                .unwrap_or_else(||
220                     0
221                 );
222     // Macro case.
223     // Should not lint.
224     let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
225
226     // Issue #4144
227     {
228         let mut frequencies = HashMap::new();
229         let word = "foo";
230
231         frequencies
232             .get_mut(word)
233             .map(|count| {
234                 *count += 1;
235             })
236             .unwrap_or_else(|| {
237                 frequencies.insert(word.to_owned(), 1);
238             });
239     }
240 }
241
242 /// Checks implementation of `FILTER_NEXT` lint.
243 #[rustfmt::skip]
244 fn filter_next() {
245     let v = vec![3, 2, 1, 0, -1, -2, -3];
246
247     // Single-line case.
248     let _ = v.iter().filter(|&x| *x < 0).next();
249
250     // Multi-line case.
251     let _ = v.iter().filter(|&x| {
252                                 *x < 0
253                             }
254                    ).next();
255
256     // Check that hat we don't lint if the caller is not an `Iterator`.
257     let foo = IteratorFalsePositives { foo: 0 };
258     let _ = foo.filter().next();
259 }
260
261 /// Checks implementation of `SEARCH_IS_SOME` lint.
262 #[rustfmt::skip]
263 fn search_is_some() {
264     let v = vec![3, 2, 1, 0, -1, -2, -3];
265
266     // Check `find().is_some()`, single-line case.
267     let _ = v.iter().find(|&x| *x < 0).is_some();
268
269     // Check `find().is_some()`, multi-line case.
270     let _ = v.iter().find(|&x| {
271                               *x < 0
272                           }
273                    ).is_some();
274
275     // Check `position().is_some()`, single-line case.
276     let _ = v.iter().position(|&x| x < 0).is_some();
277
278     // Check `position().is_some()`, multi-line case.
279     let _ = v.iter().position(|&x| {
280                                   x < 0
281                               }
282                    ).is_some();
283
284     // Check `rposition().is_some()`, single-line case.
285     let _ = v.iter().rposition(|&x| x < 0).is_some();
286
287     // Check `rposition().is_some()`, multi-line case.
288     let _ = v.iter().rposition(|&x| {
289                                    x < 0
290                                }
291                    ).is_some();
292
293     // Check that we don't lint if the caller is not an `Iterator`.
294     let foo = IteratorFalsePositives { foo: 0 };
295     let _ = foo.find().is_some();
296     let _ = foo.position().is_some();
297     let _ = foo.rposition().is_some();
298 }
299
300 #[allow(clippy::similar_names)]
301 fn main() {
302     let opt = Some(0);
303     let _ = opt.unwrap();
304 }