]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_pass_by_value.rs
Reduce the hackiness of cargo-clippy
[rust.git] / tests / ui / needless_pass_by_value.rs
1
2
3
4 #![warn(needless_pass_by_value)]
5 #![allow(dead_code, single_match, if_let_redundant_pattern_matching, many_single_char_names)]
6
7 // `v` should be warned
8 // `w`, `x` and `y` are allowed (moved or mutated)
9 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
10     assert_eq!(v.len(), 42);
11
12     consume(w);
13
14     x.push(T::default());
15
16     y
17 }
18
19 fn consume<T>(_: T) {}
20
21 struct Wrapper(String);
22
23 fn bar(x: String, y: Wrapper) {
24     assert_eq!(x.len(), 42);
25     assert_eq!(y.0.len(), 42);
26 }
27
28 // U implements `Borrow<U>`, but should be warned correctly
29 fn test_borrow_trait<T: std::borrow::Borrow<str>, U>(t: T, u: U) {
30     println!("{}", t.borrow());
31     consume(&u);
32 }
33
34 // ok
35 fn test_fn<F: Fn(i32) -> i32>(f: F) {
36     f(1);
37 }
38
39 // x should be warned, but y is ok
40 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
41     match x {
42         Some(Some(_)) => 1, // not moved
43         _ => 0,
44     };
45
46     match y {
47         Some(Some(s)) => consume(s), // moved
48         _ => (),
49     };
50 }
51
52 // x and y should be warned, but z is ok
53 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
54     let Wrapper(s) = z; // moved
55     let Wrapper(ref t) = y; // not moved
56     let Wrapper(_) = y; // still not moved
57
58     assert_eq!(x.0.len(), s.len());
59     println!("{}", t);
60 }
61
62 fn main() {}