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::missing_docs_in_private_items,
18 clippy::needless_pass_by_value,
19 clippy::default_trait_access,
21 clippy::new_ret_no_self,
22 clippy::useless_format
25 use std::collections::BTreeMap;
26 use std::collections::HashMap;
27 use std::collections::HashSet;
28 use std::collections::VecDeque;
30 use std::iter::FromIterator;
31 use std::rc::{self, Rc};
32 use std::sync::{self, Arc};
37 pub fn add(self, other: T) -> T { self }
39 pub(crate) fn drop(&mut self) { } // no error, not public interfact
40 fn neg(self) -> Self { self } // no error, private function
41 fn eq(&self, other: T) -> bool { true } // no error, private function
43 fn sub(&self, other: T) -> &T { self } // no error, self is a ref
44 fn div(self) -> T { self } // no error, different #arguments
45 fn rem(self, other: T) { } // no error, wrong return type
47 fn into_u32(self) -> u32 { 0 } // fine
48 fn into_u16(&self) -> u16 { 0 }
50 fn to_something(self) -> u32 { 0 }
52 fn new(self) -> Self { unimplemented!(); }
60 // The lifetime is different, but that’s irrelevant, see #734
61 #[allow(clippy::needless_lifetimes)]
62 pub fn new<'b>(s: &'b str) -> Lt<'b> { unimplemented!() }
70 // The lifetime is different, but that’s irrelevant, see #734
71 pub fn new(s: &str) -> Lt2 { unimplemented!() }
79 // The lifetime is different, but that’s irrelevant, see #734
80 pub fn new() -> Lt3<'static> { unimplemented!() }
87 fn new() -> Self { U }
88 fn to_something(self) -> u32 { 0 } // ok because U is Copy
96 fn new() -> Option<V<T>> { None }
101 fn mul(self, other: T) -> T { self } // no error, obviously
104 /// Utility macro to test linting behavior in `option_methods()`
105 /// The lints included in `option_methods()` should not lint if the call to map is partially
107 macro_rules! opt_map {
108 ($opt:expr, $map:expr) => {($opt).map($map)};
111 /// Checks implementation of the following lints:
112 /// * `OPTION_MAP_UNWRAP_OR`
113 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
114 /// * `OPTION_MAP_OR_NONE`
115 fn option_methods() {
118 // Check OPTION_MAP_UNWRAP_OR
120 let _ = opt.map(|x| x + 1)
122 .unwrap_or(0); // should lint even though this call is on a separate line
124 let _ = opt.map(|x| {
128 let _ = opt.map(|x| x + 1)
132 // single line `map(f).unwrap_or(None)` case
133 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
134 // multiline `map(f).unwrap_or(None)` cases
135 let _ = opt.map(|x| {
140 .map(|x| Some(x + 1))
143 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
145 // Check OPTION_MAP_UNWRAP_OR_ELSE
147 let _ = opt.map(|x| x + 1)
149 .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
151 let _ = opt.map(|x| {
154 ).unwrap_or_else(|| 0);
155 let _ = opt.map(|x| x + 1)
160 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
162 // Check OPTION_MAP_OR_NONE
164 let _ = opt.map_or(None, |x| Some(x + 1));
166 let _ = opt.map_or(None, |x| {
172 /// Checks implementation of the following lints:
173 /// * `RESULT_MAP_UNWRAP_OR_ELSE`
174 fn result_methods() {
175 let res: Result<i32, ()> = Ok(1);
177 // Check RESULT_MAP_UNWRAP_OR_ELSE
179 let _ = res.map(|x| x + 1)
181 .unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
183 let _ = res.map(|x| {
186 ).unwrap_or_else(|e| 0);
187 let _ = res.map(|x| x + 1)
192 let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
195 /// Struct to generate false positives for things with .iter()
196 #[derive(Copy, Clone)]
200 fn iter(self) -> IteratorFalsePositives {
201 IteratorFalsePositives { foo: 0 }
204 fn iter_mut(self) -> IteratorFalsePositives {
205 IteratorFalsePositives { foo: 0 }
209 /// Struct to generate false positive for Iterator-based lints
210 #[derive(Copy, Clone)]
211 struct IteratorFalsePositives {
215 impl IteratorFalsePositives {
216 fn filter(self) -> IteratorFalsePositives {
220 fn next(self) -> IteratorFalsePositives {
224 fn find(self) -> Option<u32> {
228 fn position(self) -> Option<u32> {
232 fn rposition(self) -> Option<u32> {
236 fn nth(self, n: usize) -> Option<u32> {
240 fn skip(self, _: usize) -> IteratorFalsePositives {
245 /// Checks implementation of `FILTER_NEXT` lint
247 let v = vec![3, 2, 1, 0, -1, -2, -3];
249 // check single-line case
250 let _ = v.iter().filter(|&x| *x < 0).next();
252 // check multi-line case
253 let _ = v.iter().filter(|&x| {
258 // check that we don't lint if the caller is not an Iterator
259 let foo = IteratorFalsePositives { foo: 0 };
260 let _ = foo.filter().next();
263 /// Checks implementation of `SEARCH_IS_SOME` lint
264 fn search_is_some() {
265 let v = vec![3, 2, 1, 0, -1, -2, -3];
267 // check `find().is_some()`, single-line
268 let _ = v.iter().find(|&x| *x < 0).is_some();
270 // check `find().is_some()`, multi-line
271 let _ = v.iter().find(|&x| {
276 // check `position().is_some()`, single-line
277 let _ = v.iter().position(|&x| x < 0).is_some();
279 // check `position().is_some()`, multi-line
280 let _ = v.iter().position(|&x| {
285 // check `rposition().is_some()`, single-line
286 let _ = v.iter().rposition(|&x| x < 0).is_some();
288 // check `rposition().is_some()`, multi-line
289 let _ = v.iter().rposition(|&x| {
294 // check that we don't lint if the caller is not an Iterator
295 let foo = IteratorFalsePositives { foo: 0 };
296 let _ = foo.find().is_some();
297 let _ = foo.position().is_some();
298 let _ = foo.rposition().is_some();
301 /// Checks implementation of the `OR_FUN_CALL` lint
306 fn new() -> Foo { Foo }
315 fn make<T>() -> T { unimplemented!(); }
317 let with_enum = Some(Enum::A(1));
318 with_enum.unwrap_or(Enum::A(5));
320 let with_const_fn = Some(::std::time::Duration::from_secs(1));
321 with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
323 let with_constructor = Some(vec![1]);
324 with_constructor.unwrap_or(make());
326 let with_new = Some(vec![1]);
327 with_new.unwrap_or(Vec::new());
329 let with_const_args = Some(vec![1]);
330 with_const_args.unwrap_or(Vec::with_capacity(12));
332 let with_err : Result<_, ()> = Ok(vec![1]);
333 with_err.unwrap_or(make());
335 let with_err_args : Result<_, ()> = Ok(vec![1]);
336 with_err_args.unwrap_or(Vec::with_capacity(12));
338 let with_default_trait = Some(1);
339 with_default_trait.unwrap_or(Default::default());
341 let with_default_type = Some(1);
342 with_default_type.unwrap_or(u64::default());
344 let with_vec = Some(vec![1]);
345 with_vec.unwrap_or(vec![]);
347 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
349 let without_default = Some(Foo);
350 without_default.unwrap_or(Foo::new());
352 let mut map = HashMap::<u64, String>::new();
353 map.entry(42).or_insert(String::new());
355 let mut btree = BTreeMap::<u64, String>::new();
356 btree.entry(42).or_insert(String::new());
358 let stringy = Some(String::from(""));
359 let _ = stringy.unwrap_or("".to_owned());
362 /// Checks implementation of `ITER_NTH` lint
364 let mut some_vec = vec![0, 1, 2, 3];
365 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
366 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
369 // Make sure we lint `.iter()` for relevant types
370 let bad_vec = some_vec.iter().nth(3);
371 let bad_slice = &some_vec[..].iter().nth(3);
372 let bad_boxed_slice = boxed_slice.iter().nth(3);
373 let bad_vec_deque = some_vec_deque.iter().nth(3);
377 // Make sure we lint `.iter_mut()` for relevant types
378 let bad_vec = some_vec.iter_mut().nth(3);
381 let bad_slice = &some_vec[..].iter_mut().nth(3);
384 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
387 // Make sure we don't lint for non-relevant types
388 let false_positive = HasIter;
389 let ok = false_positive.iter().nth(3);
390 let ok_mut = false_positive.iter_mut().nth(3);
393 #[allow(clippy::similar_names)]
396 let _ = opt.unwrap();