]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_pass_by_value.rs
Merge pull request #2118 from chyvonomys/relax-needless-loop
[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 use std::borrow::Borrow;
8 use std::convert::AsRef;
9
10 // `v` should be warned
11 // `w`, `x` and `y` are allowed (moved or mutated)
12 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
13     assert_eq!(v.len(), 42);
14
15     consume(w);
16
17     x.push(T::default());
18
19     y
20 }
21
22 fn consume<T>(_: T) {}
23
24 struct Wrapper(String);
25
26 fn bar(x: String, y: Wrapper) {
27     assert_eq!(x.len(), 42);
28     assert_eq!(y.0.len(), 42);
29 }
30
31 // V implements `Borrow<V>`, but should be warned correctly
32 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
33     println!("{}", t.borrow());
34     println!("{}", u.as_ref());
35     consume(&v);
36 }
37
38 // ok
39 fn test_fn<F: Fn(i32) -> i32>(f: F) {
40     f(1);
41 }
42
43 // x should be warned, but y is ok
44 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
45     match x {
46         Some(Some(_)) => 1, // not moved
47         _ => 0,
48     };
49
50     match y {
51         Some(Some(s)) => consume(s), // moved
52         _ => (),
53     };
54 }
55
56 // x and y should be warned, but z is ok
57 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
58     let Wrapper(s) = z; // moved
59     let Wrapper(ref t) = y; // not moved
60     let Wrapper(_) = y; // still not moved
61
62     assert_eq!(x.0.len(), s.len());
63     println!("{}", t);
64 }
65
66 trait Foo {}
67
68 // `S: Serialize` can be passed by value
69 trait Serialize {}
70 impl<'a, T> Serialize for &'a T where T: Serialize {}
71 impl Serialize for i32 {}
72
73 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
74
75 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
76     s.capacity();
77     let _ = t.clone();
78     u.capacity();
79     let _ = v.clone();
80 }
81
82 fn main() {}