Skip to content

Allow unused_self lint at the function level #5063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions clippy_lints/src/unused_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ImplItemKind, ImplItemRef, Item, ItemKind, Path};
use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

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

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &Item<'_>) {
if item.span.from_expansion() {
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) {
if impl_item.span.from_expansion() {
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,
Expand All @@ -56,10 +58,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
kind: AssocItemKind::Method { has_self: true },
..
} = impl_item_ref;
let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
if let ImplItemKind::Method(_, body_id) = &impl_item.kind;
let body = cx.tcx.hir().body(*body_id);
if !body.params.is_empty();
then {
let body = cx.tcx.hir().body(*body_id);
let self_param = &body.params[0];
let self_hir_id = self_param.pat.hir_id;
let mut visitor = UnusedSelfVisitor {
Expand All @@ -75,7 +77,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
self_param.span,
"unused `self` argument",
"consider refactoring to a associated function",
)
);
return;
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/unused_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ mod unused_self {
}
}

mod unused_self_allow {
struct A {}

impl A {
// shouldn't trigger
#[allow(clippy::unused_self)]
fn unused_self_move(self) {}
}

struct B {}

// shouldn't trigger
#[allow(clippy::unused_self)]
impl B {
fn unused_self_move(self) {}
}
}

mod used_self {
use std::pin::Pin;

Expand Down