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