|
| 1 | +//@normalize-stderr-test: "\(\d+ byte\)" -> "(N byte)" |
| 2 | +//@normalize-stderr-test: "\(limit: \d+ byte\)" -> "(limit: N byte)" |
| 3 | +#![deny(clippy::trivially_copy_pass_by_ref)] |
| 4 | +#![allow( |
| 5 | + clippy::disallowed_names, |
| 6 | + clippy::extra_unused_lifetimes, |
| 7 | + clippy::needless_lifetimes, |
| 8 | + clippy::needless_pass_by_ref_mut, |
| 9 | + clippy::redundant_field_names, |
| 10 | + clippy::uninlined_format_args |
| 11 | +)] |
| 12 | + |
| 13 | +#[derive(Copy, Clone)] |
| 14 | +struct Foo(u32); |
| 15 | + |
| 16 | +#[derive(Copy, Clone)] |
| 17 | +struct Bar([u8; 24]); |
| 18 | + |
| 19 | +#[derive(Copy, Clone)] |
| 20 | +pub struct Color { |
| 21 | + pub r: u8, |
| 22 | + pub g: u8, |
| 23 | + pub b: u8, |
| 24 | + pub a: u8, |
| 25 | +} |
| 26 | + |
| 27 | +struct FooRef<'a> { |
| 28 | + foo: &'a Foo, |
| 29 | +} |
| 30 | + |
| 31 | +type Baz = u32; |
| 32 | + |
| 33 | +fn good(a: &mut u32, b: u32, c: &Bar) {} |
| 34 | + |
| 35 | +fn good_return_implicit_lt_ref(foo: &Foo) -> &u32 { |
| 36 | + &foo.0 |
| 37 | +} |
| 38 | + |
| 39 | +#[allow(clippy::needless_lifetimes)] |
| 40 | +fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 { |
| 41 | + &foo.0 |
| 42 | +} |
| 43 | + |
| 44 | +fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef { |
| 45 | + FooRef { foo } |
| 46 | +} |
| 47 | + |
| 48 | +#[allow(clippy::needless_lifetimes)] |
| 49 | +fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> { |
| 50 | + FooRef { foo } |
| 51 | +} |
| 52 | + |
| 53 | +fn bad(x: u32, y: Foo, z: Baz) {} |
| 54 | +//~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 55 | +//~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 56 | +//~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 57 | + |
| 58 | +impl Foo { |
| 59 | + fn good(self, a: &mut u32, b: u32, c: &Bar) {} |
| 60 | + |
| 61 | + fn good2(&mut self) {} |
| 62 | + |
| 63 | + fn bad(self, x: u32, y: Foo, z: Baz) {} |
| 64 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 65 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 66 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 67 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 68 | + |
| 69 | + fn bad2(x: u32, y: Foo, z: Baz) {} |
| 70 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 71 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 72 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 73 | + |
| 74 | + fn bad_issue7518(self, other: Self) {} |
| 75 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if |
| 76 | +} |
| 77 | + |
| 78 | +impl AsRef<u32> for Foo { |
| 79 | + fn as_ref(&self) -> &u32 { |
| 80 | + &self.0 |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Bar { |
| 85 | + fn good(&self, a: &mut u32, b: u32, c: &Bar) {} |
| 86 | + |
| 87 | + fn bad2(x: u32, y: Foo, z: Baz) {} |
| 88 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if |
| 89 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if |
| 90 | + //~| ERROR: this argument (4 byte) is passed by reference, but would be more efficient if |
| 91 | +} |
| 92 | + |
| 93 | +trait MyTrait { |
| 94 | + fn trait_method(&self, foo: Foo); |
| 95 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if |
| 96 | +} |
| 97 | + |
| 98 | +pub trait MyTrait2 { |
| 99 | + fn trait_method2(&self, color: &Color); |
| 100 | +} |
| 101 | + |
| 102 | +trait MyTrait3 { |
| 103 | + #[expect(clippy::trivially_copy_pass_by_ref)] |
| 104 | + fn trait_method(&self, foo: &Foo); |
| 105 | +} |
| 106 | + |
| 107 | +// Trait impls should not warn |
| 108 | +impl MyTrait3 for Foo { |
| 109 | + fn trait_method(&self, foo: &Foo) { |
| 110 | + unimplemented!() |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +mod issue3992 { |
| 115 | + pub trait A { |
| 116 | + #[allow(clippy::trivially_copy_pass_by_ref)] |
| 117 | + fn a(b: &u16) {} |
| 118 | + } |
| 119 | + |
| 120 | + #[allow(clippy::trivially_copy_pass_by_ref)] |
| 121 | + pub fn c(d: &u16) {} |
| 122 | +} |
| 123 | + |
| 124 | +mod issue5876 { |
| 125 | + // Don't lint here as it is always inlined |
| 126 | + #[inline(always)] |
| 127 | + fn foo_always(x: &i32) { |
| 128 | + println!("{}", x); |
| 129 | + } |
| 130 | + |
| 131 | + #[inline(never)] |
| 132 | + fn foo_never(x: i32) { |
| 133 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 134 | + println!("{}", x); |
| 135 | + } |
| 136 | + |
| 137 | + #[inline] |
| 138 | + fn foo(x: i32) { |
| 139 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 140 | + println!("{}", x); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +fn ref_to_opt_ref_implicit(x: &u32) -> Option<&u32> { |
| 145 | + Some(x) |
| 146 | +} |
| 147 | + |
| 148 | +fn ref_to_opt_ref_explicit<'a>(x: &'a u32) -> Option<&'a u32> { |
| 149 | + Some(x) |
| 150 | +} |
| 151 | + |
| 152 | +fn with_constraint<'a, 'b: 'a>(x: &'b u32, y: &'a u32) -> &'a u32 { |
| 153 | + if true { x } else { y } |
| 154 | +} |
| 155 | + |
| 156 | +async fn async_implicit(x: &u32) -> &u32 { |
| 157 | + x |
| 158 | +} |
| 159 | + |
| 160 | +async fn async_explicit<'a>(x: &'a u32) -> &'a u32 { |
| 161 | + x |
| 162 | +} |
| 163 | + |
| 164 | +fn unrelated_lifetimes<'a, 'b>(_x: u32, y: &'b u32) -> &'b u32 { |
| 165 | + //~^ ERROR: this argument (4 byte) is passed by reference, but would be more efficient if passed by |
| 166 | + y |
| 167 | +} |
| 168 | + |
| 169 | +fn return_ptr(x: &u32) -> *const u32 { |
| 170 | + x |
| 171 | +} |
| 172 | + |
| 173 | +fn return_field_ptr(x: &(u32, u32)) -> *const u32 { |
| 174 | + &x.0 |
| 175 | +} |
| 176 | + |
| 177 | +fn return_field_ptr_addr_of(x: &(u32, u32)) -> *const u32 { |
| 178 | + core::ptr::addr_of!(x.0) |
| 179 | +} |
0 commit comments