1 #![feature(tool_lints)]
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)]
6 use std::borrow::Borrow;
7 use std::convert::AsRef;
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);
21 fn consume<T>(_: T) {}
23 struct Wrapper(String);
25 fn bar(x: String, y: Wrapper) {
26 assert_eq!(x.len(), 42);
27 assert_eq!(y.0.len(), 42);
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());
38 fn test_fn<F: Fn(i32) -> i32>(f: F) {
42 // x should be warned, but y is ok
43 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
45 Some(Some(_)) => 1, // not moved
50 Some(Some(s)) => consume(s), // moved
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
61 assert_eq!(x.0.len(), s.len());
67 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
69 impl<'a, T> Serialize for &'a T where T: Serialize {}
70 impl Serialize for i32 {}
72 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
74 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
83 impl<T: Serialize, U> S<T, U> {
85 self, // taking `self` by value is always allowed
89 s.len() + t.capacity()
93 _t: T, // Ok, since `&T: Serialize` too
105 trait FalsePositive {
106 fn visit_str(s: &str);
107 fn visit_string(s: String) {
112 // shouldn't warn on extern funcs
113 extern "C" fn ext(x: String) -> usize { x.len() }
115 // whitelist RangeArgument
116 fn range<T: ::std::ops::RangeBounds<usize>>(range: T) {
117 let _ = range.start_bound();
120 struct CopyWrapper(u32);
122 fn bar_copy(x: u32, y: CopyWrapper) {
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
137 // The following 3 lines should not cause an ICE. See #2831
139 impl<'b, T> Bar<'b, T> for T {}
140 fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
142 // Also this should not cause an ICE. See #2831
144 impl<T> Club<'static, T> for T {}
145 fn more_fun(_item: impl Club<'static, i32>) {}