Auto merge of #5387 - jpospychala:useless_self_fp, r=yaahc

`unused_self` false positive

fixes #5351

Remove the for loop in `unused_self` so that lint enabled for one method doesn't trigger on another method.

changelog: Fix false positive in `unused_self` around lint gates on impl items
This commit is contained in:
bors 2020-03-30 18:10:21 +00:00
commit 563da5248d
2 changed files with 40 additions and 38 deletions

View file

@ -1,7 +1,7 @@
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path}; use rustc_hir::{HirId, ImplItem, ImplItemKind, ItemKind, Path};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -45,45 +45,36 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
return; return;
} }
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id); let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
let item = cx.tcx.hir().expect_item(parent); let parent_item = cx.tcx.hir().expect_item(parent);
if let ItemKind::Impl { let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
of_trait: None, let assoc_item = cx.tcx.associated_item(def_id);
items: impl_item_refs, if_chain! {
.. if let ItemKind::Impl { of_trait: None, .. } = parent_item.kind;
} = item.kind if assoc_item.method_has_self_argument;
{ if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
for impl_item_ref in impl_item_refs { let body = cx.tcx.hir().body(*body_id);
if_chain! { if !body.params.is_empty();
if let ImplItemRef { then {
kind: AssocItemKind::Method { has_self: true }, let self_param = &body.params[0];
.. let self_hir_id = self_param.pat.hir_id;
} = impl_item_ref; let mut visitor = UnusedSelfVisitor {
if let ImplItemKind::Fn(_, body_id) = &impl_item.kind; cx,
let body = cx.tcx.hir().body(*body_id); uses_self: false,
if !body.params.is_empty(); self_hir_id: &self_hir_id,
then { };
let self_param = &body.params[0]; visitor.visit_body(body);
let self_hir_id = self_param.pat.hir_id; if !visitor.uses_self {
let mut visitor = UnusedSelfVisitor { span_lint_and_help(
cx, cx,
uses_self: false, UNUSED_SELF,
self_hir_id: &self_hir_id, self_param.span,
}; "unused `self` argument",
visitor.visit_body(body); "consider refactoring to a associated function",
if !visitor.uses_self { );
span_lint_and_help( return;
cx,
UNUSED_SELF,
self_param.span,
"unused `self` argument",
"consider refactoring to a associated function",
);
return;
}
}
} }
} }
}; }
} }
} }

View file

@ -42,6 +42,17 @@ mod unused_self_allow {
impl B { impl B {
fn unused_self_move(self) {} fn unused_self_move(self) {}
} }
struct C {}
#[allow(clippy::unused_self)]
impl C {
#[warn(clippy::unused_self)]
fn some_fn((): ()) {}
// shouldn't trigger
fn unused_self_move(self) {}
}
} }
mod used_self { mod used_self {