Skip to content

Commit ccb5d73

Browse files
Properly handle Self type for trivially_copy_pass_by_ref
1 parent 2dbf0c1 commit ccb5d73

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::cmp;
22
use std::iter;
33

44
use clippy_utils::diagnostics::span_lint_and_sugg;
5-
use clippy_utils::is_self_ty;
65
use clippy_utils::source::snippet;
76
use clippy_utils::ty::is_copy;
7+
use clippy_utils::{is_self, is_self_ty};
88
use if_chain::if_chain;
99
use rustc_ast::attr;
1010
use rustc_errors::Applicability;
@@ -170,7 +170,7 @@ impl<'tcx> PassByRefOrValue {
170170
if size <= self.ref_min_size;
171171
if let hir::TyKind::Rptr(_, MutTy { ty: decl_ty, .. }) = input.kind;
172172
then {
173-
let value_type = if is_self_ty(decl_ty) {
173+
let value_type = if fn_body.and_then(|body| body.params.get(index)).map_or(false, is_self) {
174174
"self".into()
175175
} else {
176176
snippet(cx, decl_ty.span, "_").into()

tests/ui/trivially_copy_pass_by_ref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ impl Foo {
5858
fn bad(&self, x: &u32, y: &Foo, z: &Baz) {}
5959

6060
fn bad2(x: &u32, y: &Foo, z: &Baz) {}
61+
62+
fn bad_issue7518(self, other: &Self) {}
6163
}
6264

6365
impl AsRef<u32> for Foo {

tests/ui/trivially_copy_pass_by_ref.stderr

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,46 @@ LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {}
6565
| ^^^^ help: consider passing by value instead: `Baz`
6666

6767
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
68-
--> $DIR/trivially_copy_pass_by_ref.rs:72:16
68+
--> $DIR/trivially_copy_pass_by_ref.rs:62:35
69+
|
70+
LL | fn bad_issue7518(self, other: &Self) {}
71+
| ^^^^^ help: consider passing by value instead: `Self`
72+
73+
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
74+
--> $DIR/trivially_copy_pass_by_ref.rs:74:16
6975
|
7076
LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {}
7177
| ^^^^ help: consider passing by value instead: `u32`
7278

7379
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
74-
--> $DIR/trivially_copy_pass_by_ref.rs:72:25
80+
--> $DIR/trivially_copy_pass_by_ref.rs:74:25
7581
|
7682
LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {}
7783
| ^^^^ help: consider passing by value instead: `Foo`
7884

7985
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
80-
--> $DIR/trivially_copy_pass_by_ref.rs:72:34
86+
--> $DIR/trivially_copy_pass_by_ref.rs:74:34
8187
|
8288
LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {}
8389
| ^^^^ help: consider passing by value instead: `Baz`
8490

8591
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
86-
--> $DIR/trivially_copy_pass_by_ref.rs:76:34
92+
--> $DIR/trivially_copy_pass_by_ref.rs:78:34
8793
|
8894
LL | fn trait_method(&self, _foo: &Foo);
8995
| ^^^^ help: consider passing by value instead: `Foo`
9096

9197
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
92-
--> $DIR/trivially_copy_pass_by_ref.rs:108:21
98+
--> $DIR/trivially_copy_pass_by_ref.rs:110:21
9399
|
94100
LL | fn foo_never(x: &i32) {
95101
| ^^^^ help: consider passing by value instead: `i32`
96102

97103
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
98-
--> $DIR/trivially_copy_pass_by_ref.rs:113:15
104+
--> $DIR/trivially_copy_pass_by_ref.rs:115:15
99105
|
100106
LL | fn foo(x: &i32) {
101107
| ^^^^ help: consider passing by value instead: `i32`
102108

103-
error: aborting due to 16 previous errors
109+
error: aborting due to 17 previous errors
104110

0 commit comments

Comments
 (0)