2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2023-01-06 12:52:51 +09:00
|
|
|
use clippy_utils::macros::root_macro_call_first_node;
|
2021-09-08 16:31:47 +02:00
|
|
|
use clippy_utils::visitors::is_local_used;
|
2019-10-03 14:09:32 -05:00
|
|
|
use if_chain::if_chain;
|
2023-01-06 12:52:51 +09:00
|
|
|
use rustc_hir::{Body, Impl, ImplItem, ImplItemKind, ItemKind};
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-07-28 19:08:22 +02:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2023-01-06 12:52:51 +09:00
|
|
|
use std::ops::ControlFlow;
|
2019-10-03 14:09:32 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02: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-29 12:16:06 +02: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-29 12:16:06 +02: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() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "1.40.0"]
|
2019-10-03 14:09:32 -05:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2022-07-28 19:08:22 +02:00
|
|
|
pub struct UnusedSelf {
|
|
|
|
avoid_breaking_exported_api: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(UnusedSelf => [UNUSED_SELF]);
|
|
|
|
|
|
|
|
impl UnusedSelf {
|
|
|
|
pub fn new(avoid_breaking_exported_api: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
avoid_breaking_exported_api,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 14:09:32 -05:00
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &ImplItem<'_>) {
|
2020-01-18 21:05:42 +09:00
|
|
|
if impl_item.span.from_expansion() {
|
2019-10-03 14:09:32 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-09-20 14:11:23 +09:00
|
|
|
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()).def_id;
|
2020-03-29 22:22:28 +02:00
|
|
|
let parent_item = cx.tcx.hir().expect_item(parent);
|
2022-10-27 14:02:18 +11:00
|
|
|
let assoc_item = cx.tcx.associated_item(impl_item.owner_id);
|
2023-01-06 12:52:51 +09:00
|
|
|
let contains_todo = |cx, body: &'_ Body<'_>| -> bool {
|
|
|
|
clippy_utils::visitors::for_each_expr(body.value, |e| {
|
|
|
|
if let Some(macro_call) = root_macro_call_first_node(cx, e) {
|
|
|
|
if cx.tcx.item_name(macro_call.def_id).as_str() == "todo" {
|
|
|
|
ControlFlow::Break(())
|
|
|
|
} else {
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.is_some()
|
|
|
|
};
|
2020-03-29 22:22:28 +02:00
|
|
|
if_chain! {
|
2020-11-22 17:46:21 -05:00
|
|
|
if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind;
|
2020-04-14 12:25:45 +02:00
|
|
|
if assoc_item.fn_has_self_parameter;
|
2020-03-29 22:22:28 +02:00
|
|
|
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
|
2022-10-27 14:02:18 +11:00
|
|
|
if !cx.effective_visibilities.is_exported(impl_item.owner_id.def_id) || !self.avoid_breaking_exported_api;
|
2020-03-29 22:22:28 +02:00
|
|
|
let body = cx.tcx.hir().body(*body_id);
|
2021-04-08 17:50:13 +02:00
|
|
|
if let [self_param, ..] = body.params;
|
2021-09-08 16:31:47 +02:00
|
|
|
if !is_local_used(cx, body, self_param.pat.hir_id);
|
2023-01-06 12:52:51 +09:00
|
|
|
if !contains_todo(cx, body);
|
2020-03-29 22:22:28 +02:00
|
|
|
then {
|
2021-04-08 17:50:13 +02:00
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
UNUSED_SELF,
|
|
|
|
self_param.span,
|
|
|
|
"unused `self` argument",
|
|
|
|
None,
|
2023-01-01 06:21:28 -05:00
|
|
|
"consider refactoring to an associated function",
|
2021-04-08 17:50:13 +02:00
|
|
|
);
|
2019-10-03 14:09:32 -05:00
|
|
|
}
|
2020-03-29 22:22:28 +02:00
|
|
|
}
|
2019-10-03 14:09:32 -05:00
|
|
|
}
|
|
|
|
}
|