4 #![warn(needless_pass_by_value)]
5 #![allow(dead_code, single_match, if_let_redundant_pattern_matching, many_single_char_names, option_option)]
7 #![feature(collections_range)]
9 use std::borrow::Borrow;
10 use std::convert::AsRef;
12 // `v` should be warned
13 // `w`, `x` and `y` are allowed (moved or mutated)
14 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
15 assert_eq!(v.len(), 42);
24 fn consume<T>(_: T) {}
26 struct Wrapper(String);
28 fn bar(x: String, y: Wrapper) {
29 assert_eq!(x.len(), 42);
30 assert_eq!(y.0.len(), 42);
33 // V implements `Borrow<V>`, but should be warned correctly
34 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
35 println!("{}", t.borrow());
36 println!("{}", u.as_ref());
41 fn test_fn<F: Fn(i32) -> i32>(f: F) {
45 // x should be warned, but y is ok
46 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
48 Some(Some(_)) => 1, // not moved
53 Some(Some(s)) => consume(s), // moved
58 // x and y should be warned, but z is ok
59 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
60 let Wrapper(s) = z; // moved
61 let Wrapper(ref t) = y; // not moved
62 let Wrapper(_) = y; // still not moved
64 assert_eq!(x.0.len(), s.len());
70 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
72 impl<'a, T> Serialize for &'a T where T: Serialize {}
73 impl Serialize for i32 {}
75 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
77 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
86 impl<T: Serialize, U> S<T, U> {
88 self, // taking `self` by value is always allowed
92 s.len() + t.capacity()
96 _t: T, // Ok, since `&T: Serialize` too
108 trait FalsePositive {
109 fn visit_str(s: &str);
110 fn visit_string(s: String) {
115 // shouldn't warn on extern funcs
116 extern "C" fn ext(x: String) -> usize { x.len() }
118 // whitelist RangeArgument
119 fn range<T: ::std::collections::range::RangeArgument<usize>>(range: T) {
120 let _ = range.start();
123 struct CopyWrapper(u32);
125 fn bar_copy(x: u32, y: CopyWrapper) {
130 // x and y should be warned, but z is ok
131 fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
132 let CopyWrapper(s) = z; // moved
133 let CopyWrapper(ref t) = y; // not moved
134 let CopyWrapper(_) = y; // still not moved