Skip to content

Commit 1d5108c

Browse files
bors[bot]waynrflip1995
committed
Merge #3437
3437: issue #3318 Run trivially_copy_pass_by_ref for traits r=flip1995 a=waynr Addresses #3318 Co-authored-by: Wayne Warren <[email protected]> Co-authored-by: Philipp Krones <[email protected]>
2 parents dfbc926 + 1fed72b commit 1d5108c

File tree

3 files changed

+149
-68
lines changed

3 files changed

+149
-68
lines changed

clippy_lints/src/trivially_copy_pass_by_ref.rs

+91-41
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1818
use crate::rustc::{declare_tool_lint, lint_array};
1919
use if_chain::if_chain;
2020
use crate::rustc::ty::TyKind;
21+
use crate::rustc::ty::FnSig;
2122
use crate::rustc::session::config::Config as SessionConfig;
2223
use crate::rustc_target::spec::abi::Abi;
2324
use crate::rustc_target::abi::LayoutOf;
2425
use crate::syntax::ast::NodeId;
2526
use crate::syntax_pos::Span;
26-
use crate::utils::{in_macro, is_copy, is_self, span_lint_and_sugg, snippet};
27+
use crate::utils::{in_macro, is_copy, is_self_ty, span_lint_and_sugg, snippet};
2728

2829
/// **What it does:** Checks for functions taking arguments by reference, where
2930
/// the argument type is `Copy` and small enough to be more efficient to always
@@ -67,7 +68,7 @@ pub struct TriviallyCopyPassByRef {
6768
limit: u64,
6869
}
6970

70-
impl TriviallyCopyPassByRef {
71+
impl<'a, 'tcx> TriviallyCopyPassByRef {
7172
pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
7273
let limit = limit.unwrap_or_else(|| {
7374
let bit_width = target.usize_ty.bit_width().expect("usize should have a width") as u64;
@@ -80,6 +81,83 @@ impl TriviallyCopyPassByRef {
8081
});
8182
Self { limit }
8283
}
84+
85+
fn check_trait_method(
86+
&mut self,
87+
cx: &LateContext<'_, 'tcx>,
88+
item: &TraitItemRef
89+
) {
90+
let method_def_id = cx.tcx.hir.local_def_id(item.id.node_id);
91+
let method_sig = cx.tcx.fn_sig(method_def_id);
92+
let method_sig = cx.tcx.erase_late_bound_regions(&method_sig);
93+
94+
let decl = match cx.tcx.hir.fn_decl(item.id.node_id) {
95+
Some(b) => b,
96+
None => return,
97+
};
98+
99+
self.check_poly_fn(cx, &decl, &method_sig, None);
100+
}
101+
102+
fn check_poly_fn(
103+
&mut self,
104+
cx: &LateContext<'_, 'tcx>,
105+
decl: &FnDecl,
106+
sig: &FnSig<'tcx>,
107+
span: Option<Span>,
108+
) {
109+
// Use lifetimes to determine if we're returning a reference to the
110+
// argument. In that case we can't switch to pass-by-value as the
111+
// argument will not live long enough.
112+
let output_lts = match sig.output().sty {
113+
TyKind::Ref(output_lt, _, _) => vec![output_lt],
114+
TyKind::Adt(_, substs) => substs.regions().collect(),
115+
_ => vec![],
116+
};
117+
118+
for (input, &ty) in decl.inputs.iter().zip(sig.inputs()) {
119+
// All spans generated from a proc-macro invocation are the same...
120+
match span {
121+
Some(s) if s == input.span => return,
122+
_ => (),
123+
}
124+
125+
if_chain! {
126+
if let TyKind::Ref(input_lt, ty, Mutability::MutImmutable) = ty.sty;
127+
if !output_lts.contains(&input_lt);
128+
if is_copy(cx, ty);
129+
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
130+
if size <= self.limit;
131+
if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.node;
132+
then {
133+
let value_type = if is_self_ty(decl_ty) {
134+
"self".into()
135+
} else {
136+
snippet(cx, decl_ty.span, "_").into()
137+
};
138+
span_lint_and_sugg(
139+
cx,
140+
TRIVIALLY_COPY_PASS_BY_REF,
141+
input.span,
142+
"this argument is passed by reference, but would be more efficient if passed by value",
143+
"consider passing by value instead",
144+
value_type);
145+
}
146+
}
147+
}
148+
}
149+
150+
fn check_trait_items(
151+
&mut self,
152+
cx: &LateContext<'_, '_>,
153+
trait_items: &[TraitItemRef]
154+
) {
155+
for item in trait_items {
156+
if let AssociatedItemKind::Method{..} = item.kind {
157+
self.check_trait_method(cx, item);
158+
}
159+
}
160+
}
83161
}
84162

85163
impl LintPass for TriviallyCopyPassByRef {
@@ -89,12 +167,21 @@ impl LintPass for TriviallyCopyPassByRef {
89167
}
90168

91169
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
170+
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
171+
if in_macro(item.span) {
172+
return;
173+
}
174+
if let ItemKind::Trait(_, _, _, _, ref trait_items) = item.node {
175+
self.check_trait_items(cx, trait_items);
176+
}
177+
}
178+
92179
fn check_fn(
93180
&mut self,
94181
cx: &LateContext<'a, 'tcx>,
95182
kind: FnKind<'tcx>,
96183
decl: &'tcx FnDecl,
97-
body: &'tcx Body,
184+
_body: &'tcx Body,
98185
span: Span,
99186
node_id: NodeId,
100187
) {
@@ -131,43 +218,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
131218
let fn_sig = cx.tcx.fn_sig(fn_def_id);
132219
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
133220

134-
// Use lifetimes to determine if we're returning a reference to the
135-
// argument. In that case we can't switch to pass-by-value as the
136-
// argument will not live long enough.
137-
let output_lts = match fn_sig.output().sty {
138-
TyKind::Ref(output_lt, _, _) => vec![output_lt],
139-
TyKind::Adt(_, substs) => substs.regions().collect(),
140-
_ => vec![],
141-
};
142-
143-
for ((input, &ty), arg) in decl.inputs.iter().zip(fn_sig.inputs()).zip(&body.arguments) {
144-
// All spans generated from a proc-macro invocation are the same...
145-
if span == input.span {
146-
return;
147-
}
148-
149-
if_chain! {
150-
if let TyKind::Ref(input_lt, ty, Mutability::MutImmutable) = ty.sty;
151-
if !output_lts.contains(&input_lt);
152-
if is_copy(cx, ty);
153-
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
154-
if size <= self.limit;
155-
if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.node;
156-
then {
157-
let value_type = if is_self(arg) {
158-
"self".into()
159-
} else {
160-
snippet(cx, decl_ty.span, "_").into()
161-
};
162-
span_lint_and_sugg(
163-
cx,
164-
TRIVIALLY_COPY_PASS_BY_REF,
165-
input.span,
166-
"this argument is passed by reference, but would be more efficient if passed by value",
167-
"consider passing by value instead",
168-
value_type);
169-
}
170-
}
171-
}
221+
self.check_poly_fn(cx, decl, &fn_sig, Some(span));
172222
}
173223
}

tests/ui/trivially_copy_pass_by_ref.rs

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ struct Foo(u32);
1818
#[derive(Copy, Clone)]
1919
struct Bar([u8; 24]);
2020

21+
#[derive(Copy, Clone)]
22+
pub struct Color {
23+
pub r: u8, pub g: u8, pub b: u8, pub a: u8,
24+
}
25+
2126
struct FooRef<'a> {
2227
foo: &'a Foo,
2328
}
@@ -80,6 +85,20 @@ impl Bar {
8085
}
8186
}
8287

88+
trait MyTrait {
89+
fn trait_method(&self, _foo: &Foo);
90+
}
91+
92+
pub trait MyTrait2 {
93+
fn trait_method2(&self, _color: &Color);
94+
}
95+
96+
impl MyTrait for Foo {
97+
fn trait_method(&self, _foo: &Foo) {
98+
unimplemented!()
99+
}
100+
}
101+
83102
fn main() {
84103
let (mut foo, bar) = (Foo(0), Bar([0; 24]));
85104
let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
+39-27
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,94 @@
11
error: this argument is passed by reference, but would be more efficient if passed by value
2-
--> $DIR/trivially_copy_pass_by_ref.rs:52:11
2+
--> $DIR/trivially_copy_pass_by_ref.rs:57:11
33
|
4-
52 | fn bad(x: &u32, y: &Foo, z: &Baz) {
4+
57 | fn bad(x: &u32, y: &Foo, z: &Baz) {
55
| ^^^^ help: consider passing by value instead: `u32`
66
|
77
= note: `-D clippy::trivially-copy-pass-by-ref` implied by `-D warnings`
88

99
error: this argument is passed by reference, but would be more efficient if passed by value
10-
--> $DIR/trivially_copy_pass_by_ref.rs:52:20
10+
--> $DIR/trivially_copy_pass_by_ref.rs:57:20
1111
|
12-
52 | fn bad(x: &u32, y: &Foo, z: &Baz) {
12+
57 | fn bad(x: &u32, y: &Foo, z: &Baz) {
1313
| ^^^^ help: consider passing by value instead: `Foo`
1414

1515
error: this argument is passed by reference, but would be more efficient if passed by value
16-
--> $DIR/trivially_copy_pass_by_ref.rs:52:29
16+
--> $DIR/trivially_copy_pass_by_ref.rs:57:29
1717
|
18-
52 | fn bad(x: &u32, y: &Foo, z: &Baz) {
18+
57 | fn bad(x: &u32, y: &Foo, z: &Baz) {
1919
| ^^^^ help: consider passing by value instead: `Baz`
2020

2121
error: this argument is passed by reference, but would be more efficient if passed by value
22-
--> $DIR/trivially_copy_pass_by_ref.rs:62:12
22+
--> $DIR/trivially_copy_pass_by_ref.rs:67:12
2323
|
24-
62 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
24+
67 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
2525
| ^^^^^ help: consider passing by value instead: `self`
2626

2727
error: this argument is passed by reference, but would be more efficient if passed by value
28-
--> $DIR/trivially_copy_pass_by_ref.rs:62:22
28+
--> $DIR/trivially_copy_pass_by_ref.rs:67:22
2929
|
30-
62 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
30+
67 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
3131
| ^^^^ help: consider passing by value instead: `u32`
3232

3333
error: this argument is passed by reference, but would be more efficient if passed by value
34-
--> $DIR/trivially_copy_pass_by_ref.rs:62:31
34+
--> $DIR/trivially_copy_pass_by_ref.rs:67:31
3535
|
36-
62 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
36+
67 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
3737
| ^^^^ help: consider passing by value instead: `Foo`
3838

3939
error: this argument is passed by reference, but would be more efficient if passed by value
40-
--> $DIR/trivially_copy_pass_by_ref.rs:62:40
40+
--> $DIR/trivially_copy_pass_by_ref.rs:67:40
4141
|
42-
62 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
42+
67 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
4343
| ^^^^ help: consider passing by value instead: `Baz`
4444

4545
error: this argument is passed by reference, but would be more efficient if passed by value
46-
--> $DIR/trivially_copy_pass_by_ref.rs:65:16
46+
--> $DIR/trivially_copy_pass_by_ref.rs:70:16
4747
|
48-
65 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
48+
70 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
4949
| ^^^^ help: consider passing by value instead: `u32`
5050

5151
error: this argument is passed by reference, but would be more efficient if passed by value
52-
--> $DIR/trivially_copy_pass_by_ref.rs:65:25
52+
--> $DIR/trivially_copy_pass_by_ref.rs:70:25
5353
|
54-
65 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
54+
70 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
5555
| ^^^^ help: consider passing by value instead: `Foo`
5656

5757
error: this argument is passed by reference, but would be more efficient if passed by value
58-
--> $DIR/trivially_copy_pass_by_ref.rs:65:34
58+
--> $DIR/trivially_copy_pass_by_ref.rs:70:34
5959
|
60-
65 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
60+
70 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
6161
| ^^^^ help: consider passing by value instead: `Baz`
6262

6363
error: this argument is passed by reference, but would be more efficient if passed by value
64-
--> $DIR/trivially_copy_pass_by_ref.rs:79:16
64+
--> $DIR/trivially_copy_pass_by_ref.rs:84:16
6565
|
66-
79 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
66+
84 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
6767
| ^^^^ help: consider passing by value instead: `u32`
6868

6969
error: this argument is passed by reference, but would be more efficient if passed by value
70-
--> $DIR/trivially_copy_pass_by_ref.rs:79:25
70+
--> $DIR/trivially_copy_pass_by_ref.rs:84:25
7171
|
72-
79 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
72+
84 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
7373
| ^^^^ help: consider passing by value instead: `Foo`
7474

7575
error: this argument is passed by reference, but would be more efficient if passed by value
76-
--> $DIR/trivially_copy_pass_by_ref.rs:79:34
76+
--> $DIR/trivially_copy_pass_by_ref.rs:84:34
7777
|
78-
79 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
78+
84 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
7979
| ^^^^ help: consider passing by value instead: `Baz`
8080

81-
error: aborting due to 13 previous errors
81+
error: this argument is passed by reference, but would be more efficient if passed by value
82+
--> $DIR/trivially_copy_pass_by_ref.rs:89:34
83+
|
84+
89 | fn trait_method(&self, _foo: &Foo);
85+
| ^^^^ help: consider passing by value instead: `Foo`
86+
87+
error: this argument is passed by reference, but would be more efficient if passed by value
88+
--> $DIR/trivially_copy_pass_by_ref.rs:93:37
89+
|
90+
93 | fn trait_method2(&self, _color: &Color);
91+
| ^^^^^^ help: consider passing by value instead: `Color`
92+
93+
error: aborting due to 15 previous errors
8294

0 commit comments

Comments
 (0)