]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_pass_by_value.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / needless_pass_by_value.rs
1 #![warn(clippy::needless_pass_by_value)]
2 #![allow(
3     dead_code,
4     clippy::single_match,
5     clippy::redundant_pattern_matching,
6     clippy::many_single_char_names,
7     clippy::option_option
8 )]
9
10 use std::borrow::Borrow;
11 use std::collections::HashSet;
12 use std::convert::AsRef;
13
14 // `v` should be warned
15 // `w`, `x` and `y` are allowed (moved or mutated)
16 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
17     assert_eq!(v.len(), 42);
18
19     consume(w);
20
21     x.push(T::default());
22
23     y
24 }
25
26 fn consume<T>(_: T) {}
27
28 struct Wrapper(String);
29
30 fn bar(x: String, y: Wrapper) {
31     assert_eq!(x.len(), 42);
32     assert_eq!(y.0.len(), 42);
33 }
34
35 // V implements `Borrow<V>`, but should be warned correctly
36 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
37     println!("{}", t.borrow());
38     println!("{}", u.as_ref());
39     consume(&v);
40 }
41
42 // ok
43 fn test_fn<F: Fn(i32) -> i32>(f: F) {
44     f(1);
45 }
46
47 // x should be warned, but y is ok
48 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
49     match x {
50         Some(Some(_)) => 1, // not moved
51         _ => 0,
52     };
53
54     match y {
55         Some(Some(s)) => consume(s), // moved
56         _ => (),
57     };
58 }
59
60 // x and y should be warned, but z is ok
61 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
62     let Wrapper(s) = z; // moved
63     let Wrapper(ref t) = y; // not moved
64     let Wrapper(_) = y; // still not moved
65
66     assert_eq!(x.0.len(), s.len());
67     println!("{}", t);
68 }
69
70 trait Foo {}
71
72 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
73 trait Serialize {}
74 impl<'a, T> Serialize for &'a T where T: Serialize {}
75 impl Serialize for i32 {}
76
77 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
78
79 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
80     s.capacity();
81     let _ = t.clone();
82     u.capacity();
83     let _ = v.clone();
84 }
85
86 struct S<T, U>(T, U);
87
88 impl<T: Serialize, U> S<T, U> {
89     fn foo(
90         self,
91         // taking `self` by value is always allowed
92         s: String,
93         t: String,
94     ) -> usize {
95         s.len() + t.capacity()
96     }
97
98     fn bar(_t: T, // Ok, since `&T: Serialize` too
99     ) {
100     }
101
102     fn baz(&self, _u: U, _s: Self) {}
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 {
114     x.len()
115 }
116
117 // whitelist RangeArgument
118 fn range<T: ::std::ops::RangeBounds<usize>>(range: T) {
119     let _ = range.start_bound();
120 }
121
122 struct CopyWrapper(u32);
123
124 fn bar_copy(x: u32, y: CopyWrapper) {
125     assert_eq!(x, 42);
126     assert_eq!(y.0, 42);
127 }
128
129 // x and y should be warned, but z is ok
130 fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
131     let CopyWrapper(s) = z; // moved
132     let CopyWrapper(ref t) = y; // not moved
133     let CopyWrapper(_) = y; // still not moved
134
135     assert_eq!(x.0, s);
136     println!("{}", t);
137 }
138
139 // The following 3 lines should not cause an ICE. See #2831
140 trait Bar<'a, A> {}
141 impl<'b, T> Bar<'b, T> for T {}
142 fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
143
144 // Also this should not cause an ICE. See #2831
145 trait Club<'a, A> {}
146 impl<T> Club<'static, T> for T {}
147 fn more_fun(_item: impl Club<'static, i32>) {}
148
149 fn is_sync<T>(_: T)
150 where
151     T: Sync,
152 {
153 }
154
155 fn main() {
156     // This should not cause an ICE either
157     // https://github.com/rust-lang/rust-clippy/issues/3144
158     is_sync(HashSet::<usize>::new());
159 }