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 number of arguments.
62 // No error; wrong return type.
63 fn rem(self, other: T) {}
66 fn into_u32(self) -> u32 {
70 fn into_u16(&self) -> u16 {
74 fn to_something(self) -> u32 {
78 fn new(self) -> Self {
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> {
100 // The lifetime is different, but that’s irrelevant; see issue #734.
101 pub fn new(s: &str) -> Lt2 {
111 // The lifetime is different, but that’s irrelevant; see issue #734.
112 pub fn new() -> Lt3<'static> {
117 #[derive(Clone, Copy)]
124 // Ok because `U` is `Copy`.
125 fn to_something(self) -> u32 {
135 fn new() -> Option<V<T>> {
142 // No error, obviously.
143 fn mul(self, other: T) -> T {
148 /// Checks implementation of the following lints:
149 /// * `OPTION_MAP_UNWRAP_OR`
150 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
151 /// * `OPTION_MAP_OR_NONE`
153 fn option_methods() {
156 // Check `OPTION_MAP_UNWRAP_OR`.
158 let _ = opt.map(|x| x + 1)
159 // Should lint even though this call is on a separate line.
162 let _ = opt.map(|x| {
166 let _ = opt.map(|x| x + 1)
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| {
178 .map(|x| Some(x + 1))
181 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
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);
190 // Check OPTION_MAP_UNWRAP_OR_ELSE
192 let _ = opt.map(|x| x + 1)
193 // Should lint even though this call is on a separate line.
194 .unwrap_or_else(|| 0);
196 let _ = opt.map(|x| {
199 ).unwrap_or_else(|| 0);
200 let _ = opt.map(|x| x + 1)
206 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
208 // Check `OPTION_MAP_OR_NONE`.
210 let _ = opt.map_or(None, |x| Some(x + 1));
212 let _ = opt.map_or(None, |x| {
218 /// Struct to generate false positives for things with `.iter()`.
219 #[derive(Copy, Clone)]
223 fn iter(self) -> IteratorFalsePositives {
224 IteratorFalsePositives { foo: 0 }
227 fn iter_mut(self) -> IteratorFalsePositives {
228 IteratorFalsePositives { foo: 0 }
232 /// Checks implementation of `FILTER_NEXT` lint.
235 let v = vec![3, 2, 1, 0, -1, -2, -3];
238 let _ = v.iter().filter(|&x| *x < 0).next();
241 let _ = v.iter().filter(|&x| {
246 // Check that hat we don't lint if the caller is not an `Iterator`.
247 let foo = IteratorFalsePositives { foo: 0 };
248 let _ = foo.filter().next();
251 /// Checks implementation of `SEARCH_IS_SOME` lint.
253 fn search_is_some() {
254 let v = vec![3, 2, 1, 0, -1, -2, -3];
256 // Check `find().is_some()`, single-line case.
257 let _ = v.iter().find(|&x| *x < 0).is_some();
259 // Check `find().is_some()`, multi-line case.
260 let _ = v.iter().find(|&x| {
265 // Check `position().is_some()`, single-line case.
266 let _ = v.iter().position(|&x| x < 0).is_some();
268 // Check `position().is_some()`, multi-line case.
269 let _ = v.iter().position(|&x| {
274 // Check `rposition().is_some()`, single-line case.
275 let _ = v.iter().rposition(|&x| x < 0).is_some();
277 // Check `rposition().is_some()`, multi-line case.
278 let _ = v.iter().rposition(|&x| {
283 // Check that we don't lint if the caller is not an `Iterator`.
284 let foo = IteratorFalsePositives { foo: 0 };
285 let _ = foo.find().is_some();
286 let _ = foo.position().is_some();
287 let _ = foo.rposition().is_some();
290 /// Checks implementation of the `OR_FUN_CALL` lint.
308 let with_enum = Some(Enum::A(1));
309 with_enum.unwrap_or(Enum::A(5));
311 let with_const_fn = Some(::std::time::Duration::from_secs(1));
312 with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
314 let with_constructor = Some(vec![1]);
315 with_constructor.unwrap_or(make());
317 let with_new = Some(vec![1]);
318 with_new.unwrap_or(Vec::new());
320 let with_const_args = Some(vec![1]);
321 with_const_args.unwrap_or(Vec::with_capacity(12));
323 let with_err: Result<_, ()> = Ok(vec![1]);
324 with_err.unwrap_or(make());
326 let with_err_args: Result<_, ()> = Ok(vec![1]);
327 with_err_args.unwrap_or(Vec::with_capacity(12));
329 let with_default_trait = Some(1);
330 with_default_trait.unwrap_or(Default::default());
332 let with_default_type = Some(1);
333 with_default_type.unwrap_or(u64::default());
335 let with_vec = Some(vec![1]);
336 with_vec.unwrap_or(vec![]);
338 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
340 let without_default = Some(Foo);
341 without_default.unwrap_or(Foo::new());
343 let mut map = HashMap::<u64, String>::new();
344 map.entry(42).or_insert(String::new());
346 let mut btree = BTreeMap::<u64, String>::new();
347 btree.entry(42).or_insert(String::new());
349 let stringy = Some(String::from(""));
350 let _ = stringy.unwrap_or("".to_owned());
353 /// Checks implementation of `ITER_NTH` lint.
355 let mut some_vec = vec![0, 1, 2, 3];
356 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
357 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
360 // Make sure we lint `.iter()` for relevant types.
361 let bad_vec = some_vec.iter().nth(3);
362 let bad_slice = &some_vec[..].iter().nth(3);
363 let bad_boxed_slice = boxed_slice.iter().nth(3);
364 let bad_vec_deque = some_vec_deque.iter().nth(3);
368 // Make sure we lint `.iter_mut()` for relevant types.
369 let bad_vec = some_vec.iter_mut().nth(3);
372 let bad_slice = &some_vec[..].iter_mut().nth(3);
375 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
378 // Make sure we don't lint for non-relevant types.
379 let false_positive = HasIter;
380 let ok = false_positive.iter().nth(3);
381 let ok_mut = false_positive.iter_mut().nth(3);
384 #[allow(clippy::similar_names)]
387 let _ = opt.unwrap();