unused_self false positive

This commit is contained in:
Jacek Pospychala 2020-03-29 22:22:28 +02:00
parent d3c40a1519
commit 82f929cbaf
2 changed files with 40 additions and 38 deletions

View file

@ -2,7 +2,7 @@ use if_chain::if_chain;
use rustc::hir::map::Map;
use rustc_hir::def::Res;
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_session::{declare_lint_pass, declare_tool_lint};
@ -45,20 +45,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
return;
}
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
let item = cx.tcx.hir().expect_item(parent);
if let ItemKind::Impl {
of_trait: None,
items: impl_item_refs,
..
} = item.kind
{
for impl_item_ref in impl_item_refs {
let parent_item = cx.tcx.hir().expect_item(parent);
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
let assoc_item = cx.tcx.associated_item(def_id);
if_chain! {
if let ImplItemRef {
kind: AssocItemKind::Method { has_self: true },
..
} = impl_item_ref;
if let ImplItemKind::Fn(_, body_id) = &impl_item.kind;
if let ItemKind::Impl { of_trait: None, .. } = parent_item.kind;
if assoc_item.method_has_self_argument;
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
let body = cx.tcx.hir().body(*body_id);
if !body.params.is_empty();
then {
@ -83,8 +76,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
}
}
}
};
}
}
struct UnusedSelfVisitor<'a, 'tcx> {

View file

@ -42,6 +42,17 @@ mod unused_self_allow {
impl B {
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 {