Skip to content

Commit ee06aa9

Browse files
committed
Auto merge of rust-lang#5063 - JohnTitor:allow-correctly, r=flip1995
Allow `unused_self` lint at the function level Another approach of rust-lang#5062. Fixes rust-lang#5053 changelog: Allow `unused_self` lint at the function level
2 parents 7ae2442 + ff0a22d commit ee06aa9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

clippy_lints/src/unused_self.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use if_chain::if_chain;
22
use rustc::hir::map::Map;
33
use rustc_hir::def::Res;
44
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
5-
use rustc_hir::{AssocItemKind, HirId, ImplItemKind, ImplItemRef, Item, ItemKind, Path};
5+
use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88

@@ -40,10 +40,12 @@ declare_clippy_lint! {
4040
declare_lint_pass!(UnusedSelf => [UNUSED_SELF]);
4141

4242
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
43-
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &Item<'_>) {
44-
if item.span.from_expansion() {
43+
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) {
44+
if impl_item.span.from_expansion() {
4545
return;
4646
}
47+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
48+
let item = cx.tcx.hir().expect_item(parent);
4749
if let ItemKind::Impl {
4850
of_trait: None,
4951
items: impl_item_refs,
@@ -56,10 +58,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
5658
kind: AssocItemKind::Method { has_self: true },
5759
..
5860
} = impl_item_ref;
59-
let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
6061
if let ImplItemKind::Method(_, body_id) = &impl_item.kind;
62+
let body = cx.tcx.hir().body(*body_id);
63+
if !body.params.is_empty();
6164
then {
62-
let body = cx.tcx.hir().body(*body_id);
6365
let self_param = &body.params[0];
6466
let self_hir_id = self_param.pat.hir_id;
6567
let mut visitor = UnusedSelfVisitor {
@@ -75,7 +77,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
7577
self_param.span,
7678
"unused `self` argument",
7779
"consider refactoring to a associated function",
78-
)
80+
);
81+
return;
7982
}
8083
}
8184
}

tests/ui/unused_self.rs

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ mod unused_self {
2626
}
2727
}
2828

29+
mod unused_self_allow {
30+
struct A {}
31+
32+
impl A {
33+
// shouldn't trigger
34+
#[allow(clippy::unused_self)]
35+
fn unused_self_move(self) {}
36+
}
37+
38+
struct B {}
39+
40+
// shouldn't trigger
41+
#[allow(clippy::unused_self)]
42+
impl B {
43+
fn unused_self_move(self) {}
44+
}
45+
}
46+
2947
mod used_self {
3048
use std::pin::Pin;
3149

0 commit comments

Comments
 (0)