4 #![warn(needless_pass_by_value)]
5 #![allow(dead_code, single_match, if_let_redundant_pattern_matching, many_single_char_names)]
7 use std::borrow::Borrow;
8 use std::convert::AsRef;
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);
22 fn consume<T>(_: T) {}
24 struct Wrapper(String);
26 fn bar(x: String, y: Wrapper) {
27 assert_eq!(x.len(), 42);
28 assert_eq!(y.0.len(), 42);
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());
39 fn test_fn<F: Fn(i32) -> i32>(f: F) {
43 // x should be warned, but y is ok
44 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
46 Some(Some(_)) => 1, // not moved
51 Some(Some(s)) => consume(s), // moved
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
62 assert_eq!(x.0.len(), s.len());
68 // `S: Serialize` can be passed by value
70 impl<'a, T> Serialize for &'a T where T: Serialize {}
71 impl Serialize for i32 {}
73 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
75 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {