5 #![deny(clippy, clippy_pedantic)]
6 #![allow(blacklisted_name, unused, print_stdout, non_ascii_literal, new_without_default, new_without_default_derive, missing_docs_in_private_items)]
8 use std::collections::BTreeMap;
9 use std::collections::HashMap;
10 use std::collections::HashSet;
11 use std::collections::VecDeque;
17 fn add(self, other: T) -> T { self } //~ERROR defining a method called `add`
18 fn drop(&mut self) { } //~ERROR defining a method called `drop`
20 fn sub(&self, other: T) -> &T { self } // no error, self is a ref
21 fn div(self) -> T { self } // no error, different #arguments
22 fn rem(self, other: T) { } // no error, wrong return type
24 fn into_u32(self) -> u32 { 0 } // fine
25 fn into_u16(&self) -> u16 { 0 } //~ERROR methods called `into_*` usually take self by value
27 fn to_something(self) -> u32 { 0 } //~ERROR methods called `to_*` usually take self by reference
30 //~^ ERROR methods called `new` usually take no self
31 //~| ERROR methods called `new` usually return `Self`
39 // The lifetime is different, but that’s irrelevant, see #734
40 #[allow(needless_lifetimes)]
41 pub fn new<'b>(s: &'b str) -> Lt<'b> { unimplemented!() }
49 // The lifetime is different, but that’s irrelevant, see #734
50 pub fn new(s: &str) -> Lt2 { unimplemented!() }
58 // The lifetime is different, but that’s irrelevant, see #734
59 pub fn new() -> Lt3<'static> { unimplemented!() }
66 fn new() -> Self { U }
67 fn to_something(self) -> u32 { 0 } // ok because U is Copy
75 fn new() -> Option<V<T>> { None }
80 fn mul(self, other: T) -> T { self } // no error, obviously
83 /// Utility macro to test linting behavior in `option_methods()`
84 /// The lints included in `option_methods()` should not lint if the call to map is partially
86 macro_rules! opt_map {
87 ($opt:expr, $map:expr) => {($opt).map($map)};
90 /// Checks implementation of the following lints:
91 /// * `OPTION_MAP_UNWRAP_OR`
92 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
96 // Check OPTION_MAP_UNWRAP_OR
98 let _ = opt.map(|x| x + 1) //~ ERROR called `map(f).unwrap_or(a)`
99 //~| NOTE replace `map(|x| x + 1).unwrap_or(0)`
100 .unwrap_or(0); // should lint even though this call is on a separate line
102 let _ = opt.map(|x| { //~ ERROR called `map(f).unwrap_or(a)`
106 let _ = opt.map(|x| x + 1) //~ ERROR called `map(f).unwrap_or(a)`
111 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
113 // Check OPTION_MAP_UNWRAP_OR_ELSE
115 let _ = opt.map(|x| x + 1) //~ ERROR called `map(f).unwrap_or_else(g)`
116 //~| NOTE replace `map(|x| x + 1).unwrap_or_else(|| 0)`
117 .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
119 let _ = opt.map(|x| { //~ ERROR called `map(f).unwrap_or_else(g)`
122 ).unwrap_or_else(|| 0);
123 let _ = opt.map(|x| x + 1) //~ ERROR called `map(f).unwrap_or_else(g)`
128 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
132 /// Struct to generate false positives for things with .iter()
133 #[derive(Copy, Clone)]
137 fn iter(self) -> IteratorFalsePositives {
138 IteratorFalsePositives { foo: 0 }
141 fn iter_mut(self) -> IteratorFalsePositives {
142 IteratorFalsePositives { foo: 0 }
146 /// Struct to generate false positive for Iterator-based lints
147 #[derive(Copy, Clone)]
148 struct IteratorFalsePositives {
152 impl IteratorFalsePositives {
153 fn filter(self) -> IteratorFalsePositives {
157 fn next(self) -> IteratorFalsePositives {
161 fn find(self) -> Option<u32> {
165 fn position(self) -> Option<u32> {
169 fn rposition(self) -> Option<u32> {
173 fn nth(self, n: usize) -> Option<u32> {
178 /// Checks implementation of `FILTER_NEXT` lint
180 let v = vec![3, 2, 1, 0, -1, -2, -3];
182 // check single-line case
183 let _ = v.iter().filter(|&x| *x < 0).next();
184 //~^ ERROR called `filter(p).next()` on an `Iterator`.
185 //~| NOTE replace `filter(|&x| *x < 0).next()`
187 // check multi-line case
188 let _ = v.iter().filter(|&x| { //~ERROR called `filter(p).next()` on an `Iterator`.
193 // check that we don't lint if the caller is not an Iterator
194 let foo = IteratorFalsePositives { foo: 0 };
195 let _ = foo.filter().next();
198 /// Checks implementation of `SEARCH_IS_SOME` lint
199 fn search_is_some() {
200 let v = vec![3, 2, 1, 0, -1, -2, -3];
202 // check `find().is_some()`, single-line
203 let _ = v.iter().find(|&x| *x < 0).is_some();
204 //~^ ERROR called `is_some()` after searching
205 //~| NOTE replace `find(|&x| *x < 0).is_some()`
207 // check `find().is_some()`, multi-line
208 let _ = v.iter().find(|&x| { //~ERROR called `is_some()` after searching
213 // check `position().is_some()`, single-line
214 let _ = v.iter().position(|&x| x < 0).is_some();
215 //~^ ERROR called `is_some()` after searching
216 //~| NOTE replace `position(|&x| x < 0).is_some()`
218 // check `position().is_some()`, multi-line
219 let _ = v.iter().position(|&x| { //~ERROR called `is_some()` after searching
224 // check `rposition().is_some()`, single-line
225 let _ = v.iter().rposition(|&x| x < 0).is_some();
226 //~^ ERROR called `is_some()` after searching
227 //~| NOTE replace `rposition(|&x| x < 0).is_some()`
229 // check `rposition().is_some()`, multi-line
230 let _ = v.iter().rposition(|&x| { //~ERROR called `is_some()` after searching
235 // check that we don't lint if the caller is not an Iterator
236 let foo = IteratorFalsePositives { foo: 0 };
237 let _ = foo.find().is_some();
238 let _ = foo.position().is_some();
239 let _ = foo.rposition().is_some();
242 /// Checks implementation of the `OR_FUN_CALL` lint
247 fn new() -> Foo { Foo }
254 const fn make_const(i: i32) -> i32 { i }
256 fn make<T>() -> T { unimplemented!(); }
258 let with_enum = Some(Enum::A(1));
259 with_enum.unwrap_or(Enum::A(5));
261 let with_const_fn = Some(1);
262 with_const_fn.unwrap_or(make_const(5));
264 let with_constructor = Some(vec![1]);
265 with_constructor.unwrap_or(make());
266 //~^ERROR use of `unwrap_or`
268 //~|SUGGESTION with_constructor.unwrap_or_else(make)
270 let with_new = Some(vec![1]);
271 with_new.unwrap_or(Vec::new());
272 //~^ERROR use of `unwrap_or`
274 //~|SUGGESTION with_new.unwrap_or_default();
276 let with_const_args = Some(vec![1]);
277 with_const_args.unwrap_or(Vec::with_capacity(12));
278 //~^ERROR use of `unwrap_or`
280 //~|SUGGESTION with_const_args.unwrap_or_else(|| Vec::with_capacity(12));
282 let with_err : Result<_, ()> = Ok(vec![1]);
283 with_err.unwrap_or(make());
284 //~^ERROR use of `unwrap_or`
286 //~|SUGGESTION with_err.unwrap_or_else(|_| make());
288 let with_err_args : Result<_, ()> = Ok(vec![1]);
289 with_err_args.unwrap_or(Vec::with_capacity(12));
290 //~^ERROR use of `unwrap_or`
292 //~|SUGGESTION with_err_args.unwrap_or_else(|_| Vec::with_capacity(12));
294 let with_default_trait = Some(1);
295 with_default_trait.unwrap_or(Default::default());
296 //~^ERROR use of `unwrap_or`
298 //~|SUGGESTION with_default_trait.unwrap_or_default();
300 let with_default_type = Some(1);
301 with_default_type.unwrap_or(u64::default());
302 //~^ERROR use of `unwrap_or`
304 //~|SUGGESTION with_default_type.unwrap_or_default();
306 let with_vec = Some(vec![1]);
307 with_vec.unwrap_or(vec![]);
308 //~^ERROR use of `unwrap_or`
310 // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
312 let without_default = Some(Foo);
313 without_default.unwrap_or(Foo::new());
314 //~^ERROR use of `unwrap_or`
316 //~|SUGGESTION without_default.unwrap_or_else(Foo::new);
318 let mut map = HashMap::<u64, String>::new();
319 map.entry(42).or_insert(String::new());
320 //~^ERROR use of `or_insert` followed by a function call
322 //~|SUGGESTION map.entry(42).or_insert_with(String::new);
324 let mut btree = BTreeMap::<u64, String>::new();
325 btree.entry(42).or_insert(String::new());
326 //~^ERROR use of `or_insert` followed by a function call
328 //~|SUGGESTION btree.entry(42).or_insert_with(String::new);
331 /// Checks implementation of `ITER_NTH` lint
333 let mut some_vec = vec![0, 1, 2, 3];
334 let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
337 // Make sure we lint `.iter()` for relevant types
338 let bad_vec = some_vec.iter().nth(3);
339 //~^ERROR called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
340 let bad_slice = &some_vec[..].iter().nth(3);
341 //~^ERROR called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
342 let bad_vec_deque = some_vec_deque.iter().nth(3);
343 //~^ERROR called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
347 // Make sure we lint `.iter_mut()` for relevant types
348 let bad_vec = some_vec.iter_mut().nth(3);
349 //~^ERROR called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
352 let bad_slice = &some_vec[..].iter_mut().nth(3);
353 //~^ERROR called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
356 let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
357 //~^ERROR called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
360 // Make sure we don't lint for non-relevant types
361 let false_positive = HasIter;
362 let ok = false_positive.iter().nth(3);
363 let ok_mut = false_positive.iter_mut().nth(3);
366 #[allow(similar_names)]
371 let _ = opt.unwrap(); //~ERROR used unwrap() on an Option
373 let res: Result<i32, ()> = Ok(0);
374 let _ = res.unwrap(); //~ERROR used unwrap() on a Result
376 res.ok().expect("disaster!"); //~ERROR called `ok().expect()`
377 // the following should not warn, since `expect` isn't implemented unless
378 // the error type implements `Debug`
379 let res2: Result<i32, MyError> = Ok(0);
380 res2.ok().expect("oh noes!");
381 let res3: Result<u32, MyErrorWithParam<u8>>= Ok(0);
382 res3.ok().expect("whoof"); //~ERROR called `ok().expect()`
383 let res4: Result<u32, io::Error> = Ok(0);
384 res4.ok().expect("argh"); //~ERROR called `ok().expect()`
385 let res5: io::Result<u32> = Ok(0);
386 res5.ok().expect("oops"); //~ERROR called `ok().expect()`
387 let res6: Result<u32, &str> = Ok(0);
388 res6.ok().expect("meh"); //~ERROR called `ok().expect()`
391 struct MyError(()); // doesn't implement Debug
394 struct MyErrorWithParam<T> {
398 #[allow(unnecessary_operation)]
400 "".chars().next() == Some(' ');
401 //~^ ERROR starts_with
403 //~| SUGGESTION "".starts_with(' ')
405 Some(' ') != "".chars().next();
406 //~^ ERROR starts_with
408 //~| SUGGESTION !"".starts_with(' ')
411 fn use_extend_from_slice() {
412 let mut v : Vec<&'static str> = vec![];
413 v.extend(&["Hello", "World"]);
414 //~^ ERROR use of `extend`
416 //~| SUGGESTION v.extend_from_slice(&["Hello", "World"]);
417 v.extend(&vec!["Some", "more"]);
418 //~^ ERROR use of `extend`
420 //~| SUGGESTION v.extend_from_slice(&vec!["Some", "more"]);
422 v.extend(vec!["And", "even", "more"].iter());
423 //~^ ERROR use of `extend`
425 //FIXME: the suggestion if broken because of the macro
426 let o : Option<&'static str> = None;
428 v.extend(Some("Bye"));
429 v.extend(vec!["Not", "like", "this"]);
430 v.extend(["But", "this"].iter());
431 //~^ERROR use of `extend
433 //~| SUGGESTION v.extend_from_slice(&["But", "this"]);
437 42.clone(); //~ERROR using `clone` on a `Copy` type
438 //~| HELP try removing the `clone` call
440 vec![1].clone(); // ok, not a Copy type
441 Some(vec![1]).clone(); // ok, not a Copy type
442 (&42).clone(); //~ERROR using `clone` on a `Copy` type
443 //~| HELP try dereferencing it
444 //~| SUGGESTION *(&42)
447 fn clone_on_copy_generic<T: Copy>(t: T) {
448 t.clone(); //~ERROR using `clone` on a `Copy` type
449 //~| HELP try removing the `clone` call
451 Some(t).clone(); //~ERROR using `clone` on a `Copy` type
452 //~| HELP try removing the `clone` call
453 //~| SUGGESTION Some(t)
456 fn clone_on_double_ref() {
459 let z: &Vec<_> = y.clone(); //~ERROR using `clone` on a double
460 //~| HELP try dereferencing it
461 //~| SUGGESTION let z: &Vec<_> = (*y).clone();
462 println!("{:p} {:p}",*y, z);
465 fn single_char_pattern() {
468 //~^ ERROR single-character string constant used as pattern
469 //~| HELP try using a char instead:
470 //~| SUGGESTION x.split('x');
479 // Not yet testing for multi-byte characters
480 // Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern`
481 // should have done this but produced an ICE
483 // We may not want to suggest changing these anyway
484 // See: https://github.com/Manishearth/rust-clippy/issues/650#issuecomment-184328984
488 // Can't use this lint for unicode code points which don't fit in a char
492 //~^ ERROR single-character string constant used as pattern
493 //~| HELP try using a char instead:
494 //~| SUGGESTION x.contains('x');
496 //~^ ERROR single-character string constant used as pattern
497 //~| HELP try using a char instead:
498 //~| SUGGESTION x.starts_with('x');
500 //~^ ERROR single-character string constant used as pattern
501 //~| HELP try using a char instead:
502 //~| SUGGESTION x.ends_with('x');
504 //~^ ERROR single-character string constant used as pattern
505 //~| HELP try using a char instead:
506 //~| SUGGESTION x.find('x');
508 //~^ ERROR single-character string constant used as pattern
509 //~| HELP try using a char instead:
510 //~| SUGGESTION x.rfind('x');
512 //~^ ERROR single-character string constant used as pattern
513 //~| HELP try using a char instead:
514 //~| SUGGESTION x.rsplit('x');
515 x.split_terminator("x");
516 //~^ ERROR single-character string constant used as pattern
517 //~| HELP try using a char instead:
518 //~| SUGGESTION x.split_terminator('x');
519 x.rsplit_terminator("x");
520 //~^ ERROR single-character string constant used as pattern
521 //~| HELP try using a char instead:
522 //~| SUGGESTION x.rsplit_terminator('x');
524 //~^ ERROR single-character string constant used as pattern
525 //~| HELP try using a char instead:
526 //~| SUGGESTION x.splitn(0, 'x');
528 //~^ ERROR single-character string constant used as pattern
529 //~| HELP try using a char instead:
530 //~| SUGGESTION x.rsplitn(0, 'x');
532 //~^ ERROR single-character string constant used as pattern
533 //~| HELP try using a char instead:
534 //~| SUGGESTION x.matches('x');
536 //~^ ERROR single-character string constant used as pattern
537 //~| HELP try using a char instead:
538 //~| SUGGESTION x.rmatches('x');
539 x.match_indices("x");
540 //~^ ERROR single-character string constant used as pattern
541 //~| HELP try using a char instead:
542 //~| SUGGESTION x.match_indices('x');
543 x.rmatch_indices("x");
544 //~^ ERROR single-character string constant used as pattern
545 //~| HELP try using a char instead:
546 //~| SUGGESTION x.rmatch_indices('x');
547 x.trim_left_matches("x");
548 //~^ ERROR single-character string constant used as pattern
549 //~| HELP try using a char instead:
550 //~| SUGGESTION x.trim_left_matches('x');
551 x.trim_right_matches("x");
552 //~^ ERROR single-character string constant used as pattern
553 //~| HELP try using a char instead:
554 //~| SUGGESTION x.trim_right_matches('x');
556 let h = HashSet::<String>::new();
557 h.contains("X"); // should not warn
560 #[allow(result_unwrap_used)]
561 fn temporary_cstring() {
562 use std::ffi::CString;
564 CString::new("foo").unwrap().as_ptr();
565 //~^ ERROR you are getting the inner pointer of a temporary `CString`
566 //~| NOTE that pointer will be invalid outside this expression
567 //~| HELP assign the `CString` to a variable to extend its lifetime