Skip to content

Commit 92f7f90

Browse files
committed
issue#3318 run trivially_copy_pass_by_ref for traits
1 parent 67c32eb commit 92f7f90

File tree

1 file changed

+93
-41
lines changed

1 file changed

+93
-41
lines changed

clippy_lints/src/trivially_copy_pass_by_ref.rs

Lines changed: 93 additions & 41 deletions
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,84 @@ 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+
match item.kind {
157+
AssociatedItemKind::Method{ has_self: _ } => self.check_trait_method(cx, item),
158+
_ => (),
159+
}
160+
}
161+
}
83162
}
84163

85164
impl LintPass for TriviallyCopyPassByRef {
@@ -89,12 +168,22 @@ impl LintPass for TriviallyCopyPassByRef {
89168
}
90169

91170
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
171+
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
172+
if in_macro(item.span) {
173+
return;
174+
}
175+
match item.node {
176+
ItemKind::Trait(_, _, _, _, ref trait_items) => self.check_trait_items(cx, trait_items),
177+
_ => (),
178+
}
179+
}
180+
92181
fn check_fn(
93182
&mut self,
94183
cx: &LateContext<'a, 'tcx>,
95184
kind: FnKind<'tcx>,
96185
decl: &'tcx FnDecl,
97-
body: &'tcx Body,
186+
_body: &'tcx Body,
98187
span: Span,
99188
node_id: NodeId,
100189
) {
@@ -131,43 +220,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
131220
let fn_sig = cx.tcx.fn_sig(fn_def_id);
132221
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
133222

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-
}
223+
self.check_poly_fn(cx, decl, &fn_sig, Some(span));
172224
}
173225
}

0 commit comments

Comments
 (0)