Skip to content

Commit 2fdfd60

Browse files
author
Michael Wright
committed
Fix needless_lifetimes false positive
1 parent 4458bef commit 2fdfd60

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]);
6060
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
6161
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
6262
if let ItemKind::Fn(ref decl, _, ref generics, id) = item.node {
63-
check_fn_inner(cx, decl, Some(id), generics, item.span);
63+
check_fn_inner(cx, decl, Some(id), generics, item.span, true);
6464
}
6565
}
6666

6767
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
6868
if let ImplItemKind::Method(ref sig, id) = item.node {
69-
if trait_ref_of_method(cx, item.hir_id).is_none() {
70-
check_fn_inner(cx, &sig.decl, Some(id), &item.generics, item.span);
71-
}
69+
let report_extra_lifetimes = trait_ref_of_method(cx, item.hir_id).is_none();
70+
check_fn_inner(
71+
cx,
72+
&sig.decl,
73+
Some(id),
74+
&item.generics,
75+
item.span,
76+
report_extra_lifetimes,
77+
);
7278
}
7379
}
7480

@@ -78,7 +84,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
7884
TraitMethod::Required(_) => None,
7985
TraitMethod::Provided(id) => Some(id),
8086
};
81-
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span);
87+
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span, true);
8288
}
8389
}
8490
}
@@ -97,6 +103,7 @@ fn check_fn_inner<'a, 'tcx>(
97103
body: Option<BodyId>,
98104
generics: &'tcx Generics,
99105
span: Span,
106+
report_extra_lifetimes: bool,
100107
) {
101108
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, &generics.where_clause) {
102109
return;
@@ -146,7 +153,9 @@ fn check_fn_inner<'a, 'tcx>(
146153
(or replaced with `'_` if needed by type declaration)",
147154
);
148155
}
149-
report_extra_lifetimes(cx, decl, generics);
156+
if report_extra_lifetimes {
157+
self::report_extra_lifetimes(cx, decl, generics);
158+
}
150159
}
151160

152161
fn could_use_elision<'a, 'tcx>(

tests/ui/needless_lifetimes.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,15 @@ fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
248248
unimplemented!()
249249
}
250250

251+
// Make sure we still warn on implementations
252+
mod issue4291 {
253+
trait BadTrait {
254+
fn needless_lt<'a>(x: &'a u8) {}
255+
}
256+
257+
impl BadTrait for () {
258+
fn needless_lt<'a>(_x: &'a u8) {}
259+
}
260+
}
261+
251262
fn main() {}

tests/ui/needless_lifetimes.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,17 @@ LL | | unimplemented!()
118118
LL | | }
119119
| |_^
120120

121-
error: aborting due to 15 previous errors
121+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
122+
--> $DIR/needless_lifetimes.rs:254:9
123+
|
124+
LL | fn needless_lt<'a>(x: &'a u8) {}
125+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126+
127+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
128+
--> $DIR/needless_lifetimes.rs:258:9
129+
|
130+
LL | fn needless_lt<'a>(_x: &'a u8) {}
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
132+
133+
error: aborting due to 17 previous errors
122134

0 commit comments

Comments
 (0)