Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extra_unused_lifetimes false positive #4477

Merged
merged 3 commits into from Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syntax::source_map::Span;
use syntax::symbol::kw;

use crate::reexport::*;
use crate::utils::{last_path_segment, span_lint};
use crate::utils::{last_path_segment, span_lint, trait_ref_of_method};

declare_clippy_lint! {
/// **What it does:** Checks for lifetime annotations which can be removed by
Expand Down Expand Up @@ -60,13 +60,21 @@ declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if let ItemKind::Fn(ref decl, _, ref generics, id) = item.node {
check_fn_inner(cx, decl, Some(id), generics, item.span);
check_fn_inner(cx, decl, Some(id), generics, item.span, true);
}
}

fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
if let ImplItemKind::Method(ref sig, id) = item.node {
check_fn_inner(cx, &sig.decl, Some(id), &item.generics, item.span);
let report_extra_lifetimes = trait_ref_of_method(cx, item.hir_id).is_none();
check_fn_inner(
cx,
&sig.decl,
Some(id),
&item.generics,
item.span,
report_extra_lifetimes,
);
}
}

Expand All @@ -76,7 +84,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
TraitMethod::Required(_) => None,
TraitMethod::Provided(id) => Some(id),
};
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span);
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span, true);
}
}
}
Expand All @@ -95,6 +103,7 @@ fn check_fn_inner<'a, 'tcx>(
body: Option<BodyId>,
generics: &'tcx Generics,
span: Span,
report_extra_lifetimes: bool,
) {
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, &generics.where_clause) {
return;
Expand Down Expand Up @@ -144,7 +153,9 @@ fn check_fn_inner<'a, 'tcx>(
(or replaced with `'_` if needed by type declaration)",
);
}
report_extra_lifetimes(cx, decl, generics);
if report_extra_lifetimes {
self::report_extra_lifetimes(cx, decl, generics);
}
}

fn could_use_elision<'a, 'tcx>(
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/extra_unused_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,15 @@ impl X {
fn explicit_self_with_lifetime<'a>(self: &'a Self) {}
}

// Methods implementing traits must have matching lifetimes
mod issue4291 {
trait BadTrait {
fn unused_lt<'a>(x: u8) {}
}

impl BadTrait for () {
fn unused_lt<'a>(_x: u8) {}
}
}

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/extra_unused_lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ error: this lifetime isn't used in the function definition
LL | fn x<'a>(&self) {}
| ^^

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

error: aborting due to 4 previous errors

11 changes: 11 additions & 0 deletions tests/ui/needless_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,15 @@ fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
unimplemented!()
}

// Make sure we still warn on implementations
mod issue4291 {
trait BadTrait {
fn needless_lt<'a>(x: &'a u8) {}
}

impl BadTrait for () {
fn needless_lt<'a>(_x: &'a u8) {}
}
}

fn main() {}
14 changes: 13 additions & 1 deletion tests/ui/needless_lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,17 @@ LL | | unimplemented!()
LL | | }
| |_^

error: aborting due to 15 previous errors
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
--> $DIR/needless_lifetimes.rs:254:9
|
LL | fn needless_lt<'a>(x: &'a u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
--> $DIR/needless_lifetimes.rs:258:9
|
LL | fn needless_lt<'a>(_x: &'a u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 17 previous errors