]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
removed unused imports
[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::pin::Pin;
31 use std::rc::{self, Rc};
32 use std::sync::{self, Arc};
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     async fn new() -> Option<Self> {
148         None
149     }
150 }
151
152 impl Mul<T> for T {
153     type Output = T;
154     // No error, obviously.
155     fn mul(self, other: T) -> T {
156         self
157     }
158 }
159
160 /// Checks implementation of the following lints:
161 /// * `OPTION_MAP_UNWRAP_OR`
162 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
163 #[rustfmt::skip]
164 fn option_methods() {
165     let opt = Some(1);
166
167     // Check `OPTION_MAP_UNWRAP_OR`.
168     // Single line case.
169     let _ = opt.map(|x| x + 1)
170                 // Should lint even though this call is on a separate line.
171                .unwrap_or(0);
172     // Multi-line cases.
173     let _ = opt.map(|x| {
174                         x + 1
175                     }
176               ).unwrap_or(0);
177     let _ = opt.map(|x| x + 1)
178                .unwrap_or({
179                     0
180                 });
181     // Single line `map(f).unwrap_or(None)` case.
182     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
183     // Multi-line `map(f).unwrap_or(None)` cases.
184     let _ = opt.map(|x| {
185         Some(x + 1)
186     }
187     ).unwrap_or(None);
188     let _ = opt
189         .map(|x| Some(x + 1))
190         .unwrap_or(None);
191     // macro case
192     let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
193
194     // Should not lint if not copyable
195     let id: String = "identifier".to_string();
196     let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
197     // ...but DO lint if the `unwrap_or` argument is not used in the `map`
198     let id: String = "identifier".to_string();
199     let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
200
201     // Check OPTION_MAP_UNWRAP_OR_ELSE
202     // single line case
203     let _ = opt.map(|x| x + 1)
204                 // Should lint even though this call is on a separate line.
205                .unwrap_or_else(|| 0);
206     // Multi-line cases.
207     let _ = opt.map(|x| {
208                         x + 1
209                     }
210               ).unwrap_or_else(|| 0);
211     let _ = opt.map(|x| x + 1)
212                .unwrap_or_else(||
213                     0
214                 );
215     // Macro case.
216     // Should not lint.
217     let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
218
219     // Issue #4144
220     {
221         let mut frequencies = HashMap::new();
222         let word = "foo";
223
224         frequencies
225             .get_mut(word)
226             .map(|count| {
227                 *count += 1;
228             })
229             .unwrap_or_else(|| {
230                 frequencies.insert(word.to_owned(), 1);
231             });
232     }
233 }
234
235 /// Checks implementation of `FILTER_NEXT` lint.
236 #[rustfmt::skip]
237 fn filter_next() {
238     let v = vec![3, 2, 1, 0, -1, -2, -3];
239
240     // Single-line case.
241     let _ = v.iter().filter(|&x| *x < 0).next();
242
243     // Multi-line case.
244     let _ = v.iter().filter(|&x| {
245                                 *x < 0
246                             }
247                    ).next();
248
249     // Check that hat we don't lint if the caller is not an `Iterator`.
250     let foo = IteratorFalsePositives { foo: 0 };
251     let _ = foo.filter().next();
252 }
253
254 /// Checks implementation of `SEARCH_IS_SOME` lint.
255 #[rustfmt::skip]
256 fn search_is_some() {
257     let v = vec![3, 2, 1, 0, -1, -2, -3];
258
259     // Check `find().is_some()`, single-line case.
260     let _ = v.iter().find(|&x| *x < 0).is_some();
261
262     // Check `find().is_some()`, multi-line case.
263     let _ = v.iter().find(|&x| {
264                               *x < 0
265                           }
266                    ).is_some();
267
268     // Check `position().is_some()`, single-line case.
269     let _ = v.iter().position(|&x| x < 0).is_some();
270
271     // Check `position().is_some()`, multi-line case.
272     let _ = v.iter().position(|&x| {
273                                   x < 0
274                               }
275                    ).is_some();
276
277     // Check `rposition().is_some()`, single-line case.
278     let _ = v.iter().rposition(|&x| x < 0).is_some();
279
280     // Check `rposition().is_some()`, multi-line case.
281     let _ = v.iter().rposition(|&x| {
282                                    x < 0
283                                }
284                    ).is_some();
285
286     // Check that we don't lint if the caller is not an `Iterator`.
287     let foo = IteratorFalsePositives { foo: 0 };
288     let _ = foo.find().is_some();
289     let _ = foo.position().is_some();
290     let _ = foo.rposition().is_some();
291 }
292
293 #[allow(clippy::similar_names)]
294 fn main() {
295     let opt = Some(0);
296     let _ = opt.unwrap();
297 }