1 #![warn(clippy::needless_pass_by_value)]
5 clippy::redundant_clone,
6 clippy::redundant_pattern_matching,
8 clippy::uninlined_format_args
11 use std::borrow::Borrow;
12 use std::collections::HashSet;
13 use std::convert::AsRef;
14 use std::mem::MaybeUninit;
16 // `v` should be warned
17 // `w`, `x` and `y` are allowed (moved or mutated)
18 fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
19 assert_eq!(v.len(), 42);
28 fn consume<T>(_: T) {}
30 struct Wrapper(String);
32 fn bar(x: String, y: Wrapper) {
33 assert_eq!(x.len(), 42);
34 assert_eq!(y.0.len(), 42);
37 // V implements `Borrow<V>`, but should be warned correctly
38 fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
39 println!("{}", t.borrow());
40 println!("{}", u.as_ref());
45 fn test_fn<F: Fn(i32) -> i32>(f: F) {
49 // x should be warned, but y is ok
50 fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
52 Some(Some(_)) => 1, // not moved
57 Some(Some(s)) => consume(s), // moved
62 // x and y should be warned, but z is ok
63 fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
64 let Wrapper(s) = z; // moved
65 let Wrapper(ref t) = y; // not moved
66 let Wrapper(_) = y; // still not moved
68 assert_eq!(x.0.len(), s.len());
74 // `S: Serialize` is allowed to be passed by value, since a caller can pass `&S` instead
76 impl<'a, T> Serialize for &'a T where T: Serialize {}
77 impl Serialize for i32 {}
79 fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
81 fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
90 impl<T: Serialize, U> S<T, U> {
93 // taking `self` by value is always allowed
97 s.len() + t.capacity()
100 fn bar(_t: T, // Ok, since `&T: Serialize` too
104 fn baz(&self, _u: U, _s: Self) {}
107 trait FalsePositive {
108 fn visit_str(s: &str);
109 fn visit_string(s: String) {
114 // shouldn't warn on extern funcs
115 extern "C" fn ext(x: MaybeUninit<usize>) -> usize {
116 unsafe { x.assume_init() }
119 // exempt RangeArgument
120 fn range<T: ::std::ops::RangeBounds<usize>>(range: T) {
121 let _ = range.start_bound();
124 struct CopyWrapper(u32);
126 fn bar_copy(x: u32, y: CopyWrapper) {
131 // x and y should be warned, but z is ok
132 fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
133 let CopyWrapper(s) = z; // moved
134 let CopyWrapper(ref t) = y; // not moved
135 let CopyWrapper(_) = y; // still not moved
141 // The following 3 lines should not cause an ICE. See #2831
143 impl<'b, T> Bar<'b, T> for T {}
144 fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
146 // Also this should not cause an ICE. See #2831
148 impl<T> Club<'static, T> for T {}
149 fn more_fun(_item: impl Club<'static, i32>) {}
158 // This should not cause an ICE either
159 // https://github.com/rust-lang/rust-clippy/issues/3144
160 is_sync(HashSet::<usize>::new());