]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_pass_by_value.rs
Adapt the *.stderr files of the ui-tests to the tool_lints
[rust.git] / tests / ui / needless_pass_by_value.rs
1 #![feature(tool_lints)]
2
3 #![warn(clippy::needless_pass_by_value)]
4 #![allow(dead_code, clippy::single_match, clippy::if_let_redundant_pattern_matching, clippy::many_single_char_names, clippy::option_option)]
5
6 use std::borrow::Borrow;
7 use std::convert::AsRef;
8
9 // `v` should be warned
10 // `w`, `x` and `y` are allowed (moved or mutated)
11 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
12     assert_eq!(v.len(), 42);
13
14     consume(w);
15
16     x.push(T::default());
17
18     y
19 }
20
21 fn consume<T>(_: T) {}
22
23 struct Wrapper(String);
24
25 fn bar(x: String, y: Wrapper) {
26     assert_eq!(x.len(), 42);
27     assert_eq!(y.0.len(), 42);
28 }
29
30 // V implements `Borrow<V>`, but should be warned correctly
31 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
32     println!("{}", t.borrow());
33     println!("{}", u.as_ref());
34     consume(&v);
35 }
36
37 // ok
38 fn test_fn<F: Fn(i32) -> i32>(f: F) {
39     f(1);
40 }
41
42 // x should be warned, but y is ok
43 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
44     match x {
45         Some(Some(_)) => 1, // not moved
46         _ => 0,
47     };
48
49     match y {
50         Some(Some(s)) => consume(s), // moved
51         _ => (),
52     };
53 }
54
55 // x and y should be warned, but z is ok
56 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
57     let Wrapper(s) = z; // moved
58     let Wrapper(ref t) = y; // not moved
59     let Wrapper(_) = y; // still not moved
60
61     assert_eq!(x.0.len(), s.len());
62     println!("{}", t);
63 }
64
65 trait Foo {}
66
67 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
68 trait Serialize {}
69 impl<'a, T> Serialize for &'a T where T: Serialize {}
70 impl Serialize for i32 {}
71
72 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
73
74 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
75     s.capacity();
76     let _ = t.clone();
77     u.capacity();
78     let _ = v.clone();
79 }
80
81 struct S<T, U>(T, U);
82
83 impl<T: Serialize, U> S<T, U> {
84     fn foo(
85         self, // taking `self` by value is always allowed
86         s: String,
87         t: String,
88     ) -> usize {
89         s.len() + t.capacity()
90     }
91
92     fn bar(
93         _t: T, // Ok, since `&T: Serialize` too
94     ) {
95     }
96
97     fn baz(
98         &self,
99         _u: U,
100         _s: Self,
101     ) {
102     }
103 }
104
105 trait FalsePositive {
106     fn visit_str(s: &str);
107     fn visit_string(s: String) {
108         Self::visit_str(&s);
109     }
110 }
111
112 // shouldn't warn on extern funcs
113 extern "C" fn ext(x: String) -> usize { x.len() }
114
115 // whitelist RangeArgument
116 fn range<T: ::std::ops::RangeBounds<usize>>(range: T) {
117     let _ = range.start_bound();
118 }
119
120 struct CopyWrapper(u32);
121
122 fn bar_copy(x: u32, y: CopyWrapper) {
123     assert_eq!(x, 42);
124     assert_eq!(y.0, 42);
125 }
126
127 // x and y should be warned, but z is ok
128 fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
129     let CopyWrapper(s) = z; // moved
130     let CopyWrapper(ref t) = y; // not moved
131     let CopyWrapper(_) = y; // still not moved
132
133     assert_eq!(x.0, s);
134     println!("{}", t);
135 }
136
137 fn main() {}