1 // aux-build:option_helpers.rs
3 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
5 clippy::blacklisted_name,
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,
14 clippy::new_ret_no_self,
15 clippy::useless_format
19 extern crate option_helpers;
21 use std::collections::BTreeMap;
22 use std::collections::HashMap;
23 use std::collections::HashSet;
24 use std::collections::VecDeque;
26 use std::iter::FromIterator;
27 use std::rc::{self, Rc};
28 use std::sync::{self, Arc};
30 use option_helpers::IteratorFalsePositives;
35 pub fn add(self, other: T) -> T { self }
37 pub(crate) fn drop(&mut self) { } // no error, not public interfact
38 fn neg(self) -> Self { self } // no error, private function
39 fn eq(&self, other: T) -> bool { true } // no error, private function
41 fn sub(&self, other: T) -> &T { self } // no error, self is a ref
42 fn div(self) -> T { self } // no error, different #arguments
43 fn rem(self, other: T) { } // no error, wrong return type
45 fn into_u32(self) -> u32 { 0 } // fine
46 fn into_u16(&self) -> u16 { 0 }
48 fn to_something(self) -> u32 { 0 }
50 fn new(self) -> Self { unimplemented!(); }
58 // The lifetime is different, but that’s irrelevant, see #734
59 #[allow(clippy::needless_lifetimes)]
60 pub fn new<'b>(s: &'b str) -> Lt<'b> { unimplemented!() }
68 // The lifetime is different, but that’s irrelevant, see #734
69 pub fn new(s: &str) -> Lt2 { unimplemented!() }
77 // The lifetime is different, but that’s irrelevant, see #734
78 pub fn new() -> Lt3<'static> { unimplemented!() }
85 fn new() -> Self { U }
86 fn to_something(self) -> u32 { 0 } // ok because U is Copy
94 fn new() -> Option<V<T>> { None }
99 fn mul(self, other: T) -> T { self } // no error, obviously
102 /// Checks implementation of the following lints:
103 /// * `OPTION_MAP_UNWRAP_OR`
104 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
105 /// * `OPTION_MAP_OR_NONE`
107 fn option_methods() {
110 // Check OPTION_MAP_UNWRAP_OR
112 let _ = opt.map(|x| x + 1)
114 .unwrap_or(0); // should lint even though this call is on a separate line
116 let _ = opt.map(|x| {
120 let _ = opt.map(|x| x + 1)
124 // single line `map(f).unwrap_or(None)` case
125 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
126 // multiline `map(f).unwrap_or(None)` cases
127 let _ = opt.map(|x| {
132 .map(|x| Some(x + 1))
135 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
137 // Check OPTION_MAP_UNWRAP_OR_ELSE
139 let _ = opt.map(|x| x + 1)
141 .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
143 let _ = opt.map(|x| {
146 ).unwrap_or_else(|| 0);
147 let _ = opt.map(|x| x + 1)
152 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
154 // Check OPTION_MAP_OR_NONE
156 let _ = opt.map_or(None, |x| Some(x + 1));
158 let _ = opt.map_or(None, |x| {
164 /// Struct to generate false positives for things with .iter()
165 #[derive(Copy, Clone)]
169 fn iter(self) -> IteratorFalsePositives {
170 IteratorFalsePositives { foo: 0 }
173 fn iter_mut(self) -> IteratorFalsePositives {
174 IteratorFalsePositives { foo: 0 }
178 /// Checks implementation of `FILTER_NEXT` lint
181 let v = vec![3, 2, 1, 0, -1, -2, -3];
183 // check single-line case
184 let _ = v.iter().filter(|&x| *x < 0).next();
186 // check multi-line case
187 let _ = v.iter().filter(|&x| {
192 // check that we don't lint if the caller is not an Iterator
193 let foo = IteratorFalsePositives { foo: 0 };
194 let _ = foo.filter().next();
197 /// Checks implementation of `SEARCH_IS_SOME` lint
199 fn search_is_some() {
200 let v = vec![3, 2, 1, 0, -1, -2, -3];
202 // check `find().is_some()`, single-line
203 let _ = v.iter().find(|&x| *x < 0).is_some();
205 // check `find().is_some()`, multi-line
206 let _ = v.iter().find(|&x| {
211 // check `position().is_some()`, single-line
212 let _ = v.iter().position(|&x| x < 0).is_some();
214 // check `position().is_some()`, multi-line
215 let _ = v.iter().position(|&x| {
220 // check `rposition().is_some()`, single-line
221 let _ = v.iter().rposition(|&x| x < 0).is_some();
223 // check `rposition().is_some()`, multi-line
224 let _ = v.iter().rposition(|&x| {
229 // check that we don't lint if the caller is not an Iterator
230 let foo = IteratorFalsePositives { foo: 0 };
231 let _ = foo.find().is_some();
232 let _ = foo.position().is_some();
233 let _ = foo.rposition().is_some();
236 /// Checks implementation of the `OR_FUN_CALL` lint
241 fn new() -> Foo { Foo }
250 fn make<T>() -> T { unimplemented!(); }
252 let with_enum = Some(Enum::A(1));
253 with_enum.unwrap_or(Enum::A(5));
255 let with_const_fn = Some(::std::time::Duration::from_secs(1));
256 with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
258 let with_constructor = Some(vec![1]);
259 with_constructor.unwrap_or(make());
261 let with_new = Some(vec![1]);
262 with_new.unwrap_or(Vec::new());
264 let with_const_args = Some(vec![1]);
265 with_const_args.unwrap_or(Vec::with_capacity(12));
267 let with_err : Result<_, ()> = Ok(vec![1]);
268 with_err.unwrap_or(make());
270 let with_err_args : Result<_, ()> = Ok(vec![1]);
271 with_err_args.unwrap_or(Vec::with_capacity(12));
273 let with_default_trait = Some(1);
274 with_default_trait.unwrap_or(Default::default());
276 let with_default_type = Some(1);
277 with_default_type.unwrap_or(u64::default());
279 let with_vec = Some(vec![1]);
280 with_vec.unwrap_or(vec![]);
282 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
284 let without_default = Some(Foo);
285 without_default.unwrap_or(Foo::new());
287 let mut map = HashMap::<u64, String>::new();
288 map.entry(42).or_insert(String::new());
290 let mut btree = BTreeMap::<u64, String>::new();
291 btree.entry(42).or_insert(String::new());
293 let stringy = Some(String::from(""));
294 let _ = stringy.unwrap_or("".to_owned());
297 /// Checks implementation of `ITER_NTH` lint
299 let mut some_vec = vec![0, 1, 2, 3];
300 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
301 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
304 // Make sure we lint `.iter()` for relevant types
305 let bad_vec = some_vec.iter().nth(3);
306 let bad_slice = &some_vec[..].iter().nth(3);
307 let bad_boxed_slice = boxed_slice.iter().nth(3);
308 let bad_vec_deque = some_vec_deque.iter().nth(3);
312 // Make sure we lint `.iter_mut()` for relevant types
313 let bad_vec = some_vec.iter_mut().nth(3);
316 let bad_slice = &some_vec[..].iter_mut().nth(3);
319 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
322 // Make sure we don't lint for non-relevant types
323 let false_positive = HasIter;
324 let ok = false_positive.iter().nth(3);
325 let ok_mut = false_positive.iter_mut().nth(3);
328 #[allow(clippy::similar_names)]
331 let _ = opt.unwrap();