Skip to content

Commit e490f49

Browse files
committed
Auto merge of #4250 - mikerite:fix-3992, r=phansch
Fix allow bug in `trivially_copy_pass_by_ref` Closes #3992 changelog: Fix allow bug in `trivially_copy_pass_by_ref`
2 parents 8dfc21b + 8fa0232 commit e490f49

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

clippy_lints/src/trivially_copy_pass_by_ref.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,22 @@ impl<'a, 'tcx> TriviallyCopyPassByRef {
7272
Self { limit }
7373
}
7474

75-
fn check_trait_method(&mut self, cx: &LateContext<'_, 'tcx>, item: &TraitItemRef) {
76-
let method_def_id = cx.tcx.hir().local_def_id_from_hir_id(item.id.hir_id);
77-
let method_sig = cx.tcx.fn_sig(method_def_id);
78-
let method_sig = cx.tcx.erase_late_bound_regions(&method_sig);
79-
80-
let decl = match cx.tcx.hir().fn_decl_by_hir_id(item.id.hir_id) {
81-
Some(b) => b,
82-
None => return,
83-
};
75+
fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &FnDecl, span: Option<Span>) {
76+
let fn_def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
8477

85-
self.check_poly_fn(cx, &decl, &method_sig, None);
86-
}
78+
let fn_sig = cx.tcx.fn_sig(fn_def_id);
79+
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
8780

88-
fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, decl: &FnDecl, sig: &FnSig<'tcx>, span: Option<Span>) {
8981
// Use lifetimes to determine if we're returning a reference to the
9082
// argument. In that case we can't switch to pass-by-value as the
9183
// argument will not live long enough.
92-
let output_lts = match sig.output().sty {
84+
let output_lts = match fn_sig.output().sty {
9385
ty::Ref(output_lt, _, _) => vec![output_lt],
9486
ty::Adt(_, substs) => substs.regions().collect(),
9587
_ => vec![],
9688
};
9789

98-
for (input, &ty) in decl.inputs.iter().zip(sig.inputs()) {
90+
for (input, &ty) in decl.inputs.iter().zip(fn_sig.inputs()) {
9991
// All spans generated from a proc-macro invocation are the same...
10092
match span {
10193
Some(s) if s == input.span => return,
@@ -128,25 +120,18 @@ impl<'a, 'tcx> TriviallyCopyPassByRef {
128120
}
129121
}
130122
}
131-
132-
fn check_trait_items(&mut self, cx: &LateContext<'_, '_>, trait_items: &[TraitItemRef]) {
133-
for item in trait_items {
134-
if let AssocItemKind::Method { .. } = item.kind {
135-
self.check_trait_method(cx, item);
136-
}
137-
}
138-
}
139123
}
140124

141125
impl_lint_pass!(TriviallyCopyPassByRef => [TRIVIALLY_COPY_PASS_BY_REF]);
142126

143127
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
144-
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
128+
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
145129
if in_macro_or_desugar(item.span) {
146130
return;
147131
}
148-
if let ItemKind::Trait(_, _, _, _, ref trait_items) = item.node {
149-
self.check_trait_items(cx, trait_items);
132+
133+
if let hir::TraitItemKind::Method(method_sig, _) = &item.node {
134+
self.check_poly_fn(cx, item.hir_id, &*method_sig.decl, None);
150135
}
151136
}
152137

@@ -187,11 +172,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
187172
}
188173
}
189174

190-
let fn_def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
191-
192-
let fn_sig = cx.tcx.fn_sig(fn_def_id);
193-
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
194-
195-
self.check_poly_fn(cx, decl, &fn_sig, Some(span));
175+
self.check_poly_fn(cx, hir_id, decl, Some(span));
196176
}
197177
}

tests/ui/trivially_copy_pass_by_ref.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ impl MyTrait for Foo {
8585
}
8686
}
8787

88+
#[allow(unused_variables)]
89+
mod issue3992 {
90+
pub trait A {
91+
#[allow(clippy::trivially_copy_pass_by_ref)]
92+
fn a(b: &u16) {}
93+
}
94+
95+
#[allow(clippy::trivially_copy_pass_by_ref)]
96+
pub fn c(d: &u16) {}
97+
}
98+
8899
fn main() {
89100
let (mut foo, bar) = (Foo(0), Bar([0; 24]));
90101
let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);

0 commit comments

Comments
 (0)