Skip to content

Commit 6dcdd46

Browse files
committed
Auto merge of #4477 - mikerite:fix-4291, r=flip1995
Fix `extra_unused_lifetimes` false positive Fixes #4291 changelog: Fix `extra_unused_lifetimes` false positive
2 parents a3fcaee + 2fdfd60 commit 6dcdd46

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syntax::source_map::Span;
99
use syntax::symbol::kw;
1010

1111
use crate::reexport::*;
12-
use crate::utils::{last_path_segment, span_lint};
12+
use crate::utils::{last_path_segment, span_lint, trait_ref_of_method};
1313

1414
declare_clippy_lint! {
1515
/// **What it does:** Checks for lifetime annotations which can be removed by
@@ -60,13 +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-
check_fn_inner(cx, &sig.decl, Some(id), &item.generics, item.span);
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+
);
7078
}
7179
}
7280

@@ -76,7 +84,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
7684
TraitMethod::Required(_) => None,
7785
TraitMethod::Provided(id) => Some(id),
7886
};
79-
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span);
87+
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span, true);
8088
}
8189
}
8290
}
@@ -95,6 +103,7 @@ fn check_fn_inner<'a, 'tcx>(
95103
body: Option<BodyId>,
96104
generics: &'tcx Generics,
97105
span: Span,
106+
report_extra_lifetimes: bool,
98107
) {
99108
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, &generics.where_clause) {
100109
return;
@@ -144,7 +153,9 @@ fn check_fn_inner<'a, 'tcx>(
144153
(or replaced with `'_` if needed by type declaration)",
145154
);
146155
}
147-
report_extra_lifetimes(cx, decl, generics);
156+
if report_extra_lifetimes {
157+
self::report_extra_lifetimes(cx, decl, generics);
158+
}
148159
}
149160

150161
fn could_use_elision<'a, 'tcx>(

tests/ui/extra_unused_lifetimes.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ impl X {
6161
fn explicit_self_with_lifetime<'a>(self: &'a Self) {}
6262
}
6363

64+
// Methods implementing traits must have matching lifetimes
65+
mod issue4291 {
66+
trait BadTrait {
67+
fn unused_lt<'a>(x: u8) {}
68+
}
69+
70+
impl BadTrait for () {
71+
fn unused_lt<'a>(_x: u8) {}
72+
}
73+
}
74+
6475
fn main() {}

tests/ui/extra_unused_lifetimes.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ error: this lifetime isn't used in the function definition
1818
LL | fn x<'a>(&self) {}
1919
| ^^
2020

21-
error: aborting due to 3 previous errors
21+
error: this lifetime isn't used in the function definition
22+
--> $DIR/extra_unused_lifetimes.rs:67:22
23+
|
24+
LL | fn unused_lt<'a>(x: u8) {}
25+
| ^^
26+
27+
error: aborting due to 4 previous errors
2228

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)