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;
25 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 {
39 // no error, not public interface
40 pub(crate) fn drop(&mut self) {}
42 // no error, private function
43 fn neg(self) -> Self {
47 // no error, private function
48 fn eq(&self, other: T) -> bool {
52 // no error, self is a ref
53 fn sub(&self, other: T) -> &T {
57 // no error, different #arguments
62 fn rem(self, other: T) {} // no error, wrong return type
65 fn into_u32(self) -> u32 {
69 fn into_u16(&self) -> u16 {
73 fn to_something(self) -> u32 {
77 fn new(self) -> Self {
87 // The lifetime is different, but that’s irrelevant, see #734
88 #[allow(clippy::needless_lifetimes)]
89 pub fn new<'b>(s: &'b str) -> Lt<'b> {
99 // The lifetime is different, but that’s irrelevant, see #734
100 pub fn new(s: &str) -> Lt2 {
110 // The lifetime is different, but that’s irrelevant, see #734
111 pub fn new() -> Lt3<'static> {
116 #[derive(Clone, Copy)]
123 // ok because U is Copy
124 fn to_something(self) -> u32 {
134 fn new() -> Option<V<T>> {
141 // no error, obviously
142 fn mul(self, other: T) -> T {
147 /// Checks implementation of the following lints:
148 /// * `OPTION_MAP_UNWRAP_OR`
149 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
150 /// * `OPTION_MAP_OR_NONE`
152 fn option_methods() {
155 // Check OPTION_MAP_UNWRAP_OR
157 let _ = opt.map(|x| x + 1)
159 .unwrap_or(0); // should lint even though this call is on a separate line
161 let _ = opt.map(|x| {
165 let _ = opt.map(|x| x + 1)
169 // single line `map(f).unwrap_or(None)` case
170 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
171 // multiline `map(f).unwrap_or(None)` cases
172 let _ = opt.map(|x| {
177 .map(|x| Some(x + 1))
180 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
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);
189 // Check OPTION_MAP_UNWRAP_OR_ELSE
191 let _ = opt.map(|x| x + 1)
193 .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
195 let _ = opt.map(|x| {
198 ).unwrap_or_else(|| 0);
199 let _ = opt.map(|x| x + 1)
204 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
206 // Check OPTION_MAP_OR_NONE
208 let _ = opt.map_or(None, |x| Some(x + 1));
210 let _ = opt.map_or(None, |x| {
216 /// Struct to generate false positives for things with .iter()
217 #[derive(Copy, Clone)]
221 fn iter(self) -> IteratorFalsePositives {
222 IteratorFalsePositives { foo: 0 }
225 fn iter_mut(self) -> IteratorFalsePositives {
226 IteratorFalsePositives { foo: 0 }
230 /// Checks implementation of `FILTER_NEXT` lint
233 let v = vec![3, 2, 1, 0, -1, -2, -3];
235 // check single-line case
236 let _ = v.iter().filter(|&x| *x < 0).next();
238 // check multi-line case
239 let _ = v.iter().filter(|&x| {
244 // check that we don't lint if the caller is not an Iterator
245 let foo = IteratorFalsePositives { foo: 0 };
246 let _ = foo.filter().next();
249 /// Checks implementation of `SEARCH_IS_SOME` lint
251 fn search_is_some() {
252 let v = vec![3, 2, 1, 0, -1, -2, -3];
254 // check `find().is_some()`, single-line
255 let _ = v.iter().find(|&x| *x < 0).is_some();
257 // check `find().is_some()`, multi-line
258 let _ = v.iter().find(|&x| {
263 // check `position().is_some()`, single-line
264 let _ = v.iter().position(|&x| x < 0).is_some();
266 // check `position().is_some()`, multi-line
267 let _ = v.iter().position(|&x| {
272 // check `rposition().is_some()`, single-line
273 let _ = v.iter().rposition(|&x| x < 0).is_some();
275 // check `rposition().is_some()`, multi-line
276 let _ = v.iter().rposition(|&x| {
281 // check that we don't lint if the caller is not an Iterator
282 let foo = IteratorFalsePositives { foo: 0 };
283 let _ = foo.find().is_some();
284 let _ = foo.position().is_some();
285 let _ = foo.rposition().is_some();
288 /// Checks implementation of the `OR_FUN_CALL` lint
306 let with_enum = Some(Enum::A(1));
307 with_enum.unwrap_or(Enum::A(5));
309 let with_const_fn = Some(::std::time::Duration::from_secs(1));
310 with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
312 let with_constructor = Some(vec![1]);
313 with_constructor.unwrap_or(make());
315 let with_new = Some(vec![1]);
316 with_new.unwrap_or(Vec::new());
318 let with_const_args = Some(vec![1]);
319 with_const_args.unwrap_or(Vec::with_capacity(12));
321 let with_err: Result<_, ()> = Ok(vec![1]);
322 with_err.unwrap_or(make());
324 let with_err_args: Result<_, ()> = Ok(vec![1]);
325 with_err_args.unwrap_or(Vec::with_capacity(12));
327 let with_default_trait = Some(1);
328 with_default_trait.unwrap_or(Default::default());
330 let with_default_type = Some(1);
331 with_default_type.unwrap_or(u64::default());
333 let with_vec = Some(vec![1]);
334 with_vec.unwrap_or(vec![]);
336 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
338 let without_default = Some(Foo);
339 without_default.unwrap_or(Foo::new());
341 let mut map = HashMap::<u64, String>::new();
342 map.entry(42).or_insert(String::new());
344 let mut btree = BTreeMap::<u64, String>::new();
345 btree.entry(42).or_insert(String::new());
347 let stringy = Some(String::from(""));
348 let _ = stringy.unwrap_or("".to_owned());
351 /// Checks implementation of `ITER_NTH` lint
353 let mut some_vec = vec![0, 1, 2, 3];
354 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
355 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
358 // Make sure we lint `.iter()` for relevant types
359 let bad_vec = some_vec.iter().nth(3);
360 let bad_slice = &some_vec[..].iter().nth(3);
361 let bad_boxed_slice = boxed_slice.iter().nth(3);
362 let bad_vec_deque = some_vec_deque.iter().nth(3);
366 // Make sure we lint `.iter_mut()` for relevant types
367 let bad_vec = some_vec.iter_mut().nth(3);
370 let bad_slice = &some_vec[..].iter_mut().nth(3);
373 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
376 // Make sure we don't lint for non-relevant types
377 let false_positive = HasIter;
378 let ok = false_positive.iter().nth(3);
379 let ok_mut = false_positive.iter_mut().nth(3);
382 #[allow(clippy::similar_names)]
385 let _ = opt.unwrap();