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