2019-10-03 19:09:32 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::def::Res;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
|
2020-03-29 20:22:28 +00:00
|
|
|
use rustc_hir::{HirId, ImplItem, ImplItemKind, ItemKind, Path};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-10-03 19:09:32 +00:00
|
|
|
|
2020-01-27 01:56:22 +00:00
|
|
|
use crate::utils::span_lint_and_help;
|
2019-10-03 19:09:32 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks methods that contain a `self` argument but don't use it
|
|
|
|
///
|
2019-10-14 19:23:24 +00:00
|
|
|
/// **Why is this bad?** It may be clearer to define the method as an associated function instead
|
2019-10-03 19:09:32 +00:00
|
|
|
/// of an instance method if it doesn't require `self`.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// struct A;
|
|
|
|
/// impl A {
|
|
|
|
/// fn method(&self) {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// struct A;
|
|
|
|
/// impl A {
|
|
|
|
/// fn method() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub UNUSED_SELF,
|
2019-10-04 17:18:52 +00:00
|
|
|
pedantic,
|
2019-10-03 19:09:32 +00:00
|
|
|
"methods that contain a `self` argument but don't use it"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(UnusedSelf => [UNUSED_SELF]);
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
|
2020-01-18 12:05:42 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) {
|
|
|
|
if impl_item.span.from_expansion() {
|
2019-10-03 19:09:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-18 12:05:42 +00:00
|
|
|
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
|
2020-03-29 20:22:28 +00:00
|
|
|
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 ItemKind::Impl { of_trait: None, .. } = parent_item.kind;
|
2020-04-14 10:25:45 +00:00
|
|
|
if assoc_item.fn_has_self_parameter;
|
2020-03-29 20:22:28 +00:00
|
|
|
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
|
|
|
|
let body = cx.tcx.hir().body(*body_id);
|
|
|
|
if !body.params.is_empty();
|
|
|
|
then {
|
|
|
|
let self_param = &body.params[0];
|
|
|
|
let self_hir_id = self_param.pat.hir_id;
|
|
|
|
let mut visitor = UnusedSelfVisitor {
|
|
|
|
cx,
|
|
|
|
uses_self: false,
|
|
|
|
self_hir_id: &self_hir_id,
|
|
|
|
};
|
|
|
|
visitor.visit_body(body);
|
|
|
|
if !visitor.uses_self {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
UNUSED_SELF,
|
|
|
|
self_param.span,
|
|
|
|
"unused `self` argument",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2020-03-29 20:22:28 +00:00
|
|
|
"consider refactoring to a associated function",
|
|
|
|
);
|
|
|
|
return;
|
2019-10-03 19:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-29 20:22:28 +00:00
|
|
|
}
|
2019-10-03 19:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnusedSelfVisitor<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
uses_self: bool,
|
|
|
|
self_hir_id: &'a HirId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for UnusedSelfVisitor<'a, 'tcx> {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
|
2019-10-03 19:09:32 +00:00
|
|
|
if self.uses_self {
|
|
|
|
// This function already uses `self`
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Res::Local(hir_id) = &path.res {
|
|
|
|
self.uses_self = self.self_hir_id == hir_id
|
|
|
|
}
|
|
|
|
walk_path(self, path);
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
|
2019-10-03 19:09:32 +00:00
|
|
|
}
|
|
|
|
}
|