Skip to content

Commit 563da52

Browse files
committed
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
2 parents e170c84 + 82f929c commit 563da52

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

clippy_lints/src/unused_self.rs

+29-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use if_chain::if_chain;
22
use rustc_hir::def::Res;
33
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
4-
use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path};
4+
use rustc_hir::{HirId, ImplItem, ImplItemKind, ItemKind, Path};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::hir::map::Map;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -45,45 +45,36 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
4545
return;
4646
}
4747
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
48-
let item = cx.tcx.hir().expect_item(parent);
49-
if let ItemKind::Impl {
50-
of_trait: None,
51-
items: impl_item_refs,
52-
..
53-
} = item.kind
54-
{
55-
for impl_item_ref in impl_item_refs {
56-
if_chain! {
57-
if let ImplItemRef {
58-
kind: AssocItemKind::Method { has_self: true },
59-
..
60-
} = impl_item_ref;
61-
if let ImplItemKind::Fn(_, body_id) = &impl_item.kind;
62-
let body = cx.tcx.hir().body(*body_id);
63-
if !body.params.is_empty();
64-
then {
65-
let self_param = &body.params[0];
66-
let self_hir_id = self_param.pat.hir_id;
67-
let mut visitor = UnusedSelfVisitor {
68-
cx,
69-
uses_self: false,
70-
self_hir_id: &self_hir_id,
71-
};
72-
visitor.visit_body(body);
73-
if !visitor.uses_self {
74-
span_lint_and_help(
75-
cx,
76-
UNUSED_SELF,
77-
self_param.span,
78-
"unused `self` argument",
79-
"consider refactoring to a associated function",
80-
);
81-
return;
82-
}
83-
}
48+
let parent_item = cx.tcx.hir().expect_item(parent);
49+
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
50+
let assoc_item = cx.tcx.associated_item(def_id);
51+
if_chain! {
52+
if let ItemKind::Impl { of_trait: None, .. } = parent_item.kind;
53+
if assoc_item.method_has_self_argument;
54+
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
55+
let body = cx.tcx.hir().body(*body_id);
56+
if !body.params.is_empty();
57+
then {
58+
let self_param = &body.params[0];
59+
let self_hir_id = self_param.pat.hir_id;
60+
let mut visitor = UnusedSelfVisitor {
61+
cx,
62+
uses_self: false,
63+
self_hir_id: &self_hir_id,
64+
};
65+
visitor.visit_body(body);
66+
if !visitor.uses_self {
67+
span_lint_and_help(
68+
cx,
69+
UNUSED_SELF,
70+
self_param.span,
71+
"unused `self` argument",
72+
"consider refactoring to a associated function",
73+
);
74+
return;
8475
}
8576
}
86-
};
77+
}
8778
}
8879
}
8980

tests/ui/unused_self.rs

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ mod unused_self_allow {
4242
impl B {
4343
fn unused_self_move(self) {}
4444
}
45+
46+
struct C {}
47+
48+
#[allow(clippy::unused_self)]
49+
impl C {
50+
#[warn(clippy::unused_self)]
51+
fn some_fn((): ()) {}
52+
53+
// shouldn't trigger
54+
fn unused_self_move(self) {}
55+
}
4556
}
4657

4758
mod used_self {

0 commit comments

Comments
 (0)