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`
106 fn option_methods() {
109 // Check OPTION_MAP_UNWRAP_OR
111 let _ = opt.map(|x| x + 1)
113 .unwrap_or(0); // should lint even though this call is on a separate line
115 let _ = opt.map(|x| {
119 let _ = opt.map(|x| x + 1)
123 // single line `map(f).unwrap_or(None)` case
124 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
125 // multiline `map(f).unwrap_or(None)` cases
126 let _ = opt.map(|x| {
131 .map(|x| Some(x + 1))
134 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
136 // Check OPTION_MAP_UNWRAP_OR_ELSE
138 let _ = opt.map(|x| x + 1)
140 .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
142 let _ = opt.map(|x| {
145 ).unwrap_or_else(|| 0);
146 let _ = opt.map(|x| x + 1)
151 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
153 // Check OPTION_MAP_OR_NONE
155 let _ = opt.map_or(None, |x| Some(x + 1));
157 let _ = opt.map_or(None, |x| {
163 /// Struct to generate false positives for things with .iter()
164 #[derive(Copy, Clone)]
168 fn iter(self) -> IteratorFalsePositives {
169 IteratorFalsePositives { foo: 0 }
172 fn iter_mut(self) -> IteratorFalsePositives {
173 IteratorFalsePositives { foo: 0 }
177 /// Checks implementation of `FILTER_NEXT` lint
179 let v = vec![3, 2, 1, 0, -1, -2, -3];
181 // check single-line case
182 let _ = v.iter().filter(|&x| *x < 0).next();
184 // check multi-line case
185 let _ = v.iter().filter(|&x| {
190 // check that we don't lint if the caller is not an Iterator
191 let foo = IteratorFalsePositives { foo: 0 };
192 let _ = foo.filter().next();
195 /// Checks implementation of `SEARCH_IS_SOME` lint
196 fn search_is_some() {
197 let v = vec![3, 2, 1, 0, -1, -2, -3];
199 // check `find().is_some()`, single-line
200 let _ = v.iter().find(|&x| *x < 0).is_some();
202 // check `find().is_some()`, multi-line
203 let _ = v.iter().find(|&x| {
208 // check `position().is_some()`, single-line
209 let _ = v.iter().position(|&x| x < 0).is_some();
211 // check `position().is_some()`, multi-line
212 let _ = v.iter().position(|&x| {
217 // check `rposition().is_some()`, single-line
218 let _ = v.iter().rposition(|&x| x < 0).is_some();
220 // check `rposition().is_some()`, multi-line
221 let _ = v.iter().rposition(|&x| {
226 // check that we don't lint if the caller is not an Iterator
227 let foo = IteratorFalsePositives { foo: 0 };
228 let _ = foo.find().is_some();
229 let _ = foo.position().is_some();
230 let _ = foo.rposition().is_some();
233 /// Checks implementation of the `OR_FUN_CALL` lint
238 fn new() -> Foo { Foo }
247 fn make<T>() -> T { unimplemented!(); }
249 let with_enum = Some(Enum::A(1));
250 with_enum.unwrap_or(Enum::A(5));
252 let with_const_fn = Some(::std::time::Duration::from_secs(1));
253 with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
255 let with_constructor = Some(vec![1]);
256 with_constructor.unwrap_or(make());
258 let with_new = Some(vec![1]);
259 with_new.unwrap_or(Vec::new());
261 let with_const_args = Some(vec![1]);
262 with_const_args.unwrap_or(Vec::with_capacity(12));
264 let with_err : Result<_, ()> = Ok(vec![1]);
265 with_err.unwrap_or(make());
267 let with_err_args : Result<_, ()> = Ok(vec![1]);
268 with_err_args.unwrap_or(Vec::with_capacity(12));
270 let with_default_trait = Some(1);
271 with_default_trait.unwrap_or(Default::default());
273 let with_default_type = Some(1);
274 with_default_type.unwrap_or(u64::default());
276 let with_vec = Some(vec![1]);
277 with_vec.unwrap_or(vec![]);
279 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
281 let without_default = Some(Foo);
282 without_default.unwrap_or(Foo::new());
284 let mut map = HashMap::<u64, String>::new();
285 map.entry(42).or_insert(String::new());
287 let mut btree = BTreeMap::<u64, String>::new();
288 btree.entry(42).or_insert(String::new());
290 let stringy = Some(String::from(""));
291 let _ = stringy.unwrap_or("".to_owned());
294 /// Checks implementation of `ITER_NTH` lint
296 let mut some_vec = vec![0, 1, 2, 3];
297 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
298 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
301 // Make sure we lint `.iter()` for relevant types
302 let bad_vec = some_vec.iter().nth(3);
303 let bad_slice = &some_vec[..].iter().nth(3);
304 let bad_boxed_slice = boxed_slice.iter().nth(3);
305 let bad_vec_deque = some_vec_deque.iter().nth(3);
309 // Make sure we lint `.iter_mut()` for relevant types
310 let bad_vec = some_vec.iter_mut().nth(3);
313 let bad_slice = &some_vec[..].iter_mut().nth(3);
316 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
319 // Make sure we don't lint for non-relevant types
320 let false_positive = HasIter;
321 let ok = false_positive.iter().nth(3);
322 let ok_mut = false_positive.iter_mut().nth(3);
325 #[allow(clippy::similar_names)]
328 let _ = opt.unwrap();