1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::visitors::is_local_used;
3 use if_chain::if_chain;
4 use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// Checks methods that contain a `self` argument but don't use it
12 /// ### Why is this bad?
13 /// It may be clearer to define the method as an associated function instead
14 /// of an instance method if it doesn't require `self`.
20 /// fn method(&self) {}
32 #[clippy::version = "1.40.0"]
35 "methods that contain a `self` argument but don't use it"
38 declare_lint_pass!(UnusedSelf => [UNUSED_SELF]);
40 impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
41 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &ImplItem<'_>) {
42 if impl_item.span.from_expansion() {
45 let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
46 let parent_item = cx.tcx.hir().expect_item(parent);
47 let assoc_item = cx.tcx.associated_item(impl_item.def_id);
49 if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind;
50 if assoc_item.fn_has_self_parameter;
51 if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
52 let body = cx.tcx.hir().body(*body_id);
53 if let [self_param, ..] = body.params;
54 if !is_local_used(cx, body, self_param.pat.hir_id);
60 "unused `self` argument",
62 "consider refactoring to a associated function",