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