1 #![warn(clippy::needless_pass_by_value)]
5 clippy::redundant_pattern_matching,
7 clippy::redundant_clone
10 use std::borrow::Borrow;
11 use std::collections::HashSet;
12 use std::convert::AsRef;
13 use std::mem::MaybeUninit;
15 // `v` should be warned
16 // `w`, `x` and `y` are allowed (moved or mutated)
17 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
18 assert_eq!(v.len(), 42);
27 fn consume<T>(_: T) {}
29 struct Wrapper(String);
31 fn bar(x: String, y: Wrapper) {
32 assert_eq!(x.len(), 42);
33 assert_eq!(y.0.len(), 42);
36 // V implements `Borrow<V>`, but should be warned correctly
37 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
38 println!("{}", t.borrow());
39 println!("{}", u.as_ref());
44 fn test_fn<F: Fn(i32) -> i32>(f: F) {
48 // x should be warned, but y is ok
49 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
51 Some(Some(_)) => 1, // not moved
56 Some(Some(s)) => consume(s), // moved
61 // x and y should be warned, but z is ok
62 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
63 let Wrapper(s) = z; // moved
64 let Wrapper(ref t) = y; // not moved
65 let Wrapper(_) = y; // still not moved
67 assert_eq!(x.0.len(), s.len());
73 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
75 impl<'a, T> Serialize for &'a T where T: Serialize {}
76 impl Serialize for i32 {}
78 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
80 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
89 impl<T: Serialize, U> S<T, U> {
92 // taking `self` by value is always allowed
96 s.len() + t.capacity()
99 fn bar(_t: T, // Ok, since `&T: Serialize` too
103 fn baz(&self, _u: U, _s: Self) {}
106 trait FalsePositive {
107 fn visit_str(s: &str);
108 fn visit_string(s: String) {
113 // shouldn't warn on extern funcs
114 extern "C" fn ext(x: MaybeUninit<usize>) -> usize {
115 unsafe { x.assume_init() }
118 // exempt RangeArgument
119 fn range<T: ::std::ops::RangeBounds<usize>>(range: T) {
120 let _ = range.start_bound();
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
140 // The following 3 lines should not cause an ICE. See #2831
142 impl<'b, T> Bar<'b, T> for T {}
143 fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
145 // Also this should not cause an ICE. See #2831
147 impl<T> Club<'static, T> for T {}
148 fn more_fun(_item: impl Club<'static, i32>) {}
157 // This should not cause an ICE either
158 // https://github.com/rust-lang/rust-clippy/issues/3144
159 is_sync(HashSet::<usize>::new());