1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
12 clippy::blacklisted_name,
15 clippy::non_ascii_literal,
16 clippy::new_without_default,
17 clippy::new_without_default_derive,
18 clippy::missing_docs_in_private_items,
19 clippy::needless_pass_by_value,
20 clippy::default_trait_access,
22 clippy::new_ret_no_self,
23 clippy::useless_format
26 use std::collections::BTreeMap;
27 use std::collections::HashMap;
28 use std::collections::HashSet;
29 use std::collections::VecDeque;
31 use std::iter::FromIterator;
32 use std::rc::{self, Rc};
33 use std::sync::{self, Arc};
38 pub fn add(self, other: T) -> T { self }
40 pub(crate) fn drop(&mut self) { } // no error, not public interfact
41 fn neg(self) -> Self { self } // no error, private function
42 fn eq(&self, other: T) -> bool { true } // no error, private function
44 fn sub(&self, other: T) -> &T { self } // no error, self is a ref
45 fn div(self) -> T { self } // no error, different #arguments
46 fn rem(self, other: T) { } // no error, wrong return type
48 fn into_u32(self) -> u32 { 0 } // fine
49 fn into_u16(&self) -> u16 { 0 }
51 fn to_something(self) -> u32 { 0 }
53 fn new(self) -> Self { unimplemented!(); }
61 // The lifetime is different, but that’s irrelevant, see #734
62 #[allow(clippy::needless_lifetimes)]
63 pub fn new<'b>(s: &'b str) -> Lt<'b> { unimplemented!() }
71 // The lifetime is different, but that’s irrelevant, see #734
72 pub fn new(s: &str) -> Lt2 { unimplemented!() }
80 // The lifetime is different, but that’s irrelevant, see #734
81 pub fn new() -> Lt3<'static> { unimplemented!() }
88 fn new() -> Self { U }
89 fn to_something(self) -> u32 { 0 } // ok because U is Copy
97 fn new() -> Option<V<T>> { None }
102 fn mul(self, other: T) -> T { self } // no error, obviously
105 /// Utility macro to test linting behavior in `option_methods()`
106 /// The lints included in `option_methods()` should not lint if the call to map is partially
108 macro_rules! opt_map {
109 ($opt:expr, $map:expr) => {($opt).map($map)};
112 /// Checks implementation of the following lints:
113 /// * `OPTION_MAP_UNWRAP_OR`
114 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
115 /// * `OPTION_MAP_OR_NONE`
116 fn option_methods() {
119 // Check OPTION_MAP_UNWRAP_OR
121 let _ = opt.map(|x| x + 1)
123 .unwrap_or(0); // should lint even though this call is on a separate line
125 let _ = opt.map(|x| {
129 let _ = opt.map(|x| x + 1)
133 // single line `map(f).unwrap_or(None)` case
134 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
135 // multiline `map(f).unwrap_or(None)` cases
136 let _ = opt.map(|x| {
141 .map(|x| Some(x + 1))
144 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
146 // Check OPTION_MAP_UNWRAP_OR_ELSE
148 let _ = opt.map(|x| x + 1)
150 .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
152 let _ = opt.map(|x| {
155 ).unwrap_or_else(|| 0);
156 let _ = opt.map(|x| x + 1)
161 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
163 // Check OPTION_MAP_OR_NONE
165 let _ = opt.map_or(None, |x| Some(x + 1));
167 let _ = opt.map_or(None, |x| {
173 /// Checks implementation of the following lints:
174 /// * `RESULT_MAP_UNWRAP_OR_ELSE`
175 fn result_methods() {
176 let res: Result<i32, ()> = Ok(1);
178 // Check RESULT_MAP_UNWRAP_OR_ELSE
180 let _ = res.map(|x| x + 1)
182 .unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
184 let _ = res.map(|x| {
187 ).unwrap_or_else(|e| 0);
188 let _ = res.map(|x| x + 1)
193 let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
196 /// Struct to generate false positives for things with .iter()
197 #[derive(Copy, Clone)]
201 fn iter(self) -> IteratorFalsePositives {
202 IteratorFalsePositives { foo: 0 }
205 fn iter_mut(self) -> IteratorFalsePositives {
206 IteratorFalsePositives { foo: 0 }
210 /// Struct to generate false positive for Iterator-based lints
211 #[derive(Copy, Clone)]
212 struct IteratorFalsePositives {
216 impl IteratorFalsePositives {
217 fn filter(self) -> IteratorFalsePositives {
221 fn next(self) -> IteratorFalsePositives {
225 fn find(self) -> Option<u32> {
229 fn position(self) -> Option<u32> {
233 fn rposition(self) -> Option<u32> {
237 fn nth(self, n: usize) -> Option<u32> {
241 fn skip(self, _: usize) -> IteratorFalsePositives {
246 /// Checks implementation of `FILTER_NEXT` lint
248 let v = vec![3, 2, 1, 0, -1, -2, -3];
250 // check single-line case
251 let _ = v.iter().filter(|&x| *x < 0).next();
253 // check multi-line case
254 let _ = v.iter().filter(|&x| {
259 // check that we don't lint if the caller is not an Iterator
260 let foo = IteratorFalsePositives { foo: 0 };
261 let _ = foo.filter().next();
264 /// Checks implementation of `SEARCH_IS_SOME` lint
265 fn search_is_some() {
266 let v = vec![3, 2, 1, 0, -1, -2, -3];
268 // check `find().is_some()`, single-line
269 let _ = v.iter().find(|&x| *x < 0).is_some();
271 // check `find().is_some()`, multi-line
272 let _ = v.iter().find(|&x| {
277 // check `position().is_some()`, single-line
278 let _ = v.iter().position(|&x| x < 0).is_some();
280 // check `position().is_some()`, multi-line
281 let _ = v.iter().position(|&x| {
286 // check `rposition().is_some()`, single-line
287 let _ = v.iter().rposition(|&x| x < 0).is_some();
289 // check `rposition().is_some()`, multi-line
290 let _ = v.iter().rposition(|&x| {
295 // check that we don't lint if the caller is not an Iterator
296 let foo = IteratorFalsePositives { foo: 0 };
297 let _ = foo.find().is_some();
298 let _ = foo.position().is_some();
299 let _ = foo.rposition().is_some();
302 /// Checks implementation of the `OR_FUN_CALL` lint
307 fn new() -> Foo { Foo }
316 fn make<T>() -> T { unimplemented!(); }
318 let with_enum = Some(Enum::A(1));
319 with_enum.unwrap_or(Enum::A(5));
321 let with_const_fn = Some(::std::time::Duration::from_secs(1));
322 with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
324 let with_constructor = Some(vec![1]);
325 with_constructor.unwrap_or(make());
327 let with_new = Some(vec![1]);
328 with_new.unwrap_or(Vec::new());
330 let with_const_args = Some(vec![1]);
331 with_const_args.unwrap_or(Vec::with_capacity(12));
333 let with_err : Result<_, ()> = Ok(vec![1]);
334 with_err.unwrap_or(make());
336 let with_err_args : Result<_, ()> = Ok(vec![1]);
337 with_err_args.unwrap_or(Vec::with_capacity(12));
339 let with_default_trait = Some(1);
340 with_default_trait.unwrap_or(Default::default());
342 let with_default_type = Some(1);
343 with_default_type.unwrap_or(u64::default());
345 let with_vec = Some(vec![1]);
346 with_vec.unwrap_or(vec![]);
348 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
350 let without_default = Some(Foo);
351 without_default.unwrap_or(Foo::new());
353 let mut map = HashMap::<u64, String>::new();
354 map.entry(42).or_insert(String::new());
356 let mut btree = BTreeMap::<u64, String>::new();
357 btree.entry(42).or_insert(String::new());
359 let stringy = Some(String::from(""));
360 let _ = stringy.unwrap_or("".to_owned());
363 /// Checks implementation of `ITER_NTH` lint
365 let mut some_vec = vec![0, 1, 2, 3];
366 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
367 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
370 // Make sure we lint `.iter()` for relevant types
371 let bad_vec = some_vec.iter().nth(3);
372 let bad_slice = &some_vec[..].iter().nth(3);
373 let bad_boxed_slice = boxed_slice.iter().nth(3);
374 let bad_vec_deque = some_vec_deque.iter().nth(3);
378 // Make sure we lint `.iter_mut()` for relevant types
379 let bad_vec = some_vec.iter_mut().nth(3);
382 let bad_slice = &some_vec[..].iter_mut().nth(3);
385 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
388 // Make sure we don't lint for non-relevant types
389 let false_positive = HasIter;
390 let ok = false_positive.iter().nth(3);
391 let ok_mut = false_positive.iter_mut().nth(3);
394 /// Checks implementation of `ITER_SKIP_NEXT` lint
395 fn iter_skip_next() {
396 let mut some_vec = vec![0, 1, 2, 3];
397 let _ = some_vec.iter().skip(42).next();
398 let _ = some_vec.iter().cycle().skip(42).next();
399 let _ = (1..10).skip(10).next();
400 let _ = &some_vec[..].iter().skip(3).next();
401 let foo = IteratorFalsePositives { foo : 0 };
402 let _ = foo.skip(42).next();
403 let _ = foo.filter().skip(42).next();
406 #[allow(clippy::similar_names)]
409 let _ = opt.unwrap();