needless_lifetimes: ignore lifetimes in explicit self types
This commit is contained in:
parent
acfc161fa6
commit
9ef6e2199c
3 changed files with 162 additions and 35 deletions
|
|
@ -15,7 +15,7 @@ use rustc_lint::{LateContext, LateLintPass};
|
|||
use rustc_middle::hir::map::Map;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_span::symbol::{kw, Symbol};
|
||||
use rustc_span::symbol::{kw, Ident, Symbol};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
|
|
@ -85,7 +85,7 @@ declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]);
|
|||
impl<'tcx> LateLintPass<'tcx> for Lifetimes {
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
||||
if let ItemKind::Fn(ref sig, ref generics, id) = item.kind {
|
||||
check_fn_inner(cx, sig.decl, Some(id), generics, item.span, true);
|
||||
check_fn_inner(cx, sig.decl, Some(id), None, generics, item.span, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -96,6 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes {
|
|||
cx,
|
||||
sig.decl,
|
||||
Some(id),
|
||||
None,
|
||||
&item.generics,
|
||||
item.span,
|
||||
report_extra_lifetimes,
|
||||
|
|
@ -105,11 +106,11 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes {
|
|||
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
|
||||
if let TraitItemKind::Fn(ref sig, ref body) = item.kind {
|
||||
let body = match *body {
|
||||
TraitFn::Required(_) => None,
|
||||
TraitFn::Provided(id) => Some(id),
|
||||
let (body, trait_sig) = match *body {
|
||||
TraitFn::Required(sig) => (None, Some(sig)),
|
||||
TraitFn::Provided(id) => (Some(id), None),
|
||||
};
|
||||
check_fn_inner(cx, sig.decl, body, &item.generics, item.span, true);
|
||||
check_fn_inner(cx, sig.decl, body, trait_sig, &item.generics, item.span, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -126,6 +127,7 @@ fn check_fn_inner<'tcx>(
|
|||
cx: &LateContext<'tcx>,
|
||||
decl: &'tcx FnDecl<'_>,
|
||||
body: Option<BodyId>,
|
||||
trait_sig: Option<&[Ident]>,
|
||||
generics: &'tcx Generics<'_>,
|
||||
span: Span,
|
||||
report_extra_lifetimes: bool,
|
||||
|
|
@ -167,7 +169,7 @@ fn check_fn_inner<'tcx>(
|
|||
}
|
||||
}
|
||||
}
|
||||
if could_use_elision(cx, decl, body, generics.params) {
|
||||
if could_use_elision(cx, decl, body, trait_sig, generics.params) {
|
||||
span_lint(
|
||||
cx,
|
||||
NEEDLESS_LIFETIMES,
|
||||
|
|
@ -181,10 +183,31 @@ fn check_fn_inner<'tcx>(
|
|||
}
|
||||
}
|
||||
|
||||
// elision doesn't work for explicit self types, see rust-lang/rust#69064
|
||||
fn explicit_self_type<'tcx>(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, ident: Option<Ident>) -> bool {
|
||||
if_chain! {
|
||||
if let Some(ident) = ident;
|
||||
if ident.name == kw::SelfLower;
|
||||
if !func.implicit_self.has_implicit_self();
|
||||
|
||||
if let Some(self_ty) = func.inputs.first();
|
||||
then {
|
||||
let mut visitor = RefVisitor::new(cx);
|
||||
visitor.visit_ty(self_ty);
|
||||
|
||||
!visitor.all_lts().is_empty()
|
||||
}
|
||||
else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn could_use_elision<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
func: &'tcx FnDecl<'_>,
|
||||
body: Option<BodyId>,
|
||||
trait_sig: Option<&[Ident]>,
|
||||
named_generics: &'tcx [GenericParam<'_>],
|
||||
) -> bool {
|
||||
// There are two scenarios where elision works:
|
||||
|
|
@ -235,11 +258,24 @@ fn could_use_elision<'tcx>(
|
|||
let input_lts = input_visitor.lts;
|
||||
let output_lts = output_visitor.lts;
|
||||
|
||||
if let Some(trait_sig) = trait_sig {
|
||||
if explicit_self_type(cx, func, trait_sig.first().copied()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(body_id) = body {
|
||||
let body = cx.tcx.hir().body(body_id);
|
||||
|
||||
let first_ident = body.params.first().and_then(|param| param.pat.simple_ident());
|
||||
if explicit_self_type(cx, func, first_ident) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut checker = BodyLifetimeChecker {
|
||||
lifetimes_used_in_body: false,
|
||||
};
|
||||
checker.visit_expr(&cx.tcx.hir().body(body_id).value);
|
||||
checker.visit_expr(&body.value);
|
||||
if checker.lifetimes_used_in_body {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue