]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_pass_by_value.rs
Rename if_let_redundant_pattern_matching to redundant_pattern_matching
[rust.git] / tests / ui / needless_pass_by_value.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11
12
13 #![warn(clippy::needless_pass_by_value)]
14 #![allow(dead_code, clippy::single_match, clippy::redundant_pattern_matching, clippy::many_single_char_names, clippy::option_option)]
15
16 use std::borrow::Borrow;
17 use std::convert::AsRef;
18
19 // `v` should be warned
20 // `w`, `x` and `y` are allowed (moved or mutated)
21 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
22     assert_eq!(v.len(), 42);
23
24     consume(w);
25
26     x.push(T::default());
27
28     y
29 }
30
31 fn consume<T>(_: T) {}
32
33 struct Wrapper(String);
34
35 fn bar(x: String, y: Wrapper) {
36     assert_eq!(x.len(), 42);
37     assert_eq!(y.0.len(), 42);
38 }
39
40 // V implements `Borrow<V>`, but should be warned correctly
41 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
42     println!("{}", t.borrow());
43     println!("{}", u.as_ref());
44     consume(&v);
45 }
46
47 // ok
48 fn test_fn<F: Fn(i32) -> i32>(f: F) {
49     f(1);
50 }
51
52 // x should be warned, but y is ok
53 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
54     match x {
55         Some(Some(_)) => 1, // not moved
56         _ => 0,
57     };
58
59     match y {
60         Some(Some(s)) => consume(s), // moved
61         _ => (),
62     };
63 }
64
65 // x and y should be warned, but z is ok
66 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
67     let Wrapper(s) = z; // moved
68     let Wrapper(ref t) = y; // not moved
69     let Wrapper(_) = y; // still not moved
70
71     assert_eq!(x.0.len(), s.len());
72     println!("{}", t);
73 }
74
75 trait Foo {}
76
77 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
78 trait Serialize {}
79 impl<'a, T> Serialize for &'a T where T: Serialize {}
80 impl Serialize for i32 {}
81
82 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
83
84 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
85     s.capacity();
86     let _ = t.clone();
87     u.capacity();
88     let _ = v.clone();
89 }
90
91 struct S<T, U>(T, U);
92
93 impl<T: Serialize, U> S<T, U> {
94     fn foo(
95         self, // taking `self` by value is always allowed
96         s: String,
97         t: String,
98     ) -> usize {
99         s.len() + t.capacity()
100     }
101
102     fn bar(
103         _t: T, // Ok, since `&T: Serialize` too
104     ) {
105     }
106
107     fn baz(
108         &self,
109         _u: U,
110         _s: Self,
111     ) {
112     }
113 }
114
115 trait FalsePositive {
116     fn visit_str(s: &str);
117     fn visit_string(s: String) {
118         Self::visit_str(&s);
119     }
120 }
121
122 // shouldn't warn on extern funcs
123 extern "C" fn ext(x: String) -> usize { x.len() }
124
125 // whitelist RangeArgument
126 fn range<T: ::std::ops::RangeBounds<usize>>(range: T) {
127     let _ = range.start_bound();
128 }
129
130 struct CopyWrapper(u32);
131
132 fn bar_copy(x: u32, y: CopyWrapper) {
133     assert_eq!(x, 42);
134     assert_eq!(y.0, 42);
135 }
136
137 // x and y should be warned, but z is ok
138 fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
139     let CopyWrapper(s) = z; // moved
140     let CopyWrapper(ref t) = y; // not moved
141     let CopyWrapper(_) = y; // still not moved
142
143     assert_eq!(x.0, s);
144     println!("{}", t);
145 }
146
147 // The following 3 lines should not cause an ICE. See #2831
148 trait Bar<'a, A> {}
149 impl<'b, T> Bar<'b, T> for T {}
150 fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
151
152 // Also this should not cause an ICE. See #2831
153 trait Club<'a, A> {}
154 impl<T> Club<'static, T> for T {}
155 fn more_fun(_item: impl Club<'static, i32>) {}
156
157 fn main() {}