2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-03-16 11:06:34 -05:00
|
|
|
use clippy_utils::visitors::LocalUsedVisitor;
|
2019-10-03 14:09:32 -05:00
|
|
|
use if_chain::if_chain;
|
2021-02-11 08:04:38 -06:00
|
|
|
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-10-03 14:09:32 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks methods that contain a `self` argument but don't use it
|
2019-10-03 14:09:32 -05:00
|
|
|
///
|
2021-07-02 13:37:11 -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`.
|
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Example
|
2019-10-03 14:09:32 -05:00
|
|
|
/// ```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]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &ImplItem<'_>) {
|
2020-01-18 06:05:42 -06:00
|
|
|
if impl_item.span.from_expansion() {
|
2019-10-03 14:09:32 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-01-30 16:25:03 -06:00
|
|
|
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
|
2020-03-29 15:22:28 -05:00
|
|
|
let parent_item = cx.tcx.hir().expect_item(parent);
|
2021-01-30 16:25:03 -06:00
|
|
|
let assoc_item = cx.tcx.associated_item(impl_item.def_id);
|
2020-03-29 15:22:28 -05:00
|
|
|
if_chain! {
|
2020-11-22 16:46:21 -06:00
|
|
|
if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind;
|
2020-04-14 05:25:45 -05:00
|
|
|
if assoc_item.fn_has_self_parameter;
|
2020-03-29 15:22:28 -05:00
|
|
|
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
|
|
|
|
let body = cx.tcx.hir().body(*body_id);
|
2021-03-30 14:59:59 -05:00
|
|
|
if let [self_param, ..] = body.params;
|
|
|
|
let self_hir_id = self_param.pat.hir_id;
|
|
|
|
if !LocalUsedVisitor::new(cx, self_hir_id).check_body(body);
|
2020-03-29 15:22:28 -05:00
|
|
|
then {
|
2021-03-30 14:59:59 -05:00
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
UNUSED_SELF,
|
|
|
|
self_param.span,
|
|
|
|
"unused `self` argument",
|
|
|
|
None,
|
|
|
|
"consider refactoring to a associated function",
|
|
|
|
);
|
2019-10-03 14:09:32 -05:00
|
|
|
}
|
2020-03-29 15:22:28 -05:00
|
|
|
}
|
2019-10-03 14:09:32 -05:00
|
|
|
}
|
|
|
|
}
|