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