2019-10-03 14:09:32 -05:00
|
|
|
use if_chain::if_chain;
|
2019-12-03 17:16:03 -06:00
|
|
|
use rustc::declare_lint_pass;
|
2019-10-03 14:09:32 -05:00
|
|
|
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::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-12-03 17:16:03 -06:00
|
|
|
use rustc_session::declare_tool_lint;
|
2019-10-03 14:09:32 -05:00
|
|
|
|
|
|
|
use crate::utils::span_help_and_lint;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks methods that contain a `self` argument but don't use it
|
|
|
|
///
|
2019-10-14 14:23:24 -05:00
|
|
|
/// **Why is this bad?** It may be clearer to define the method as an associated function instead
|
2019-10-03 14:09:32 -05: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 12:18:52 -05:00
|
|
|
pedantic,
|
2019-10-03 14:09:32 -05: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 {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &Item) {
|
|
|
|
if item.span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let ItemKind::Impl(_, _, _, _, None, _, ref impl_item_refs) = item.kind {
|
|
|
|
for impl_item_ref in impl_item_refs {
|
|
|
|
if_chain! {
|
|
|
|
if let ImplItemRef {
|
|
|
|
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;
|
|
|
|
then {
|
|
|
|
let body = cx.tcx.hir().body(*body_id);
|
|
|
|
let self_param = &body.params[0];
|
|
|
|
let self_hir_id = self_param.pat.hir_id;
|
2019-10-04 12:18:52 -05:00
|
|
|
let mut visitor = UnusedSelfVisitor {
|
2019-10-03 14:09:32 -05:00
|
|
|
cx,
|
|
|
|
uses_self: false,
|
|
|
|
self_hir_id: &self_hir_id,
|
|
|
|
};
|
|
|
|
visitor.visit_body(body);
|
|
|
|
if !visitor.uses_self {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
UNUSED_SELF,
|
|
|
|
self_param.span,
|
|
|
|
"unused `self` argument",
|
2019-10-14 14:23:24 -05:00
|
|
|
"consider refactoring to a associated function",
|
2019-10-03 14:09:32 -05: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> {
|
|
|
|
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2019-10-06 13:12:07 -05:00
|
|
|
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())
|
2019-10-03 14:09:32 -05:00
|
|
|
}
|
|
|
|
}
|