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