2018-01-17 20:41:24 +11:00
|
|
|
//! checks for `#[inline]` on trait methods without bodies
|
|
|
|
|
2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2022-01-23 20:41:46 +00:00
|
|
|
use clippy_utils::sugg::DiagnosticExt;
|
2020-05-08 13:57:01 +02:00
|
|
|
use rustc_ast::ast::Attribute;
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc_errors::Applicability;
|
2020-03-16 16:00:16 +01:00
|
|
|
use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 20:37:08 +09:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-11-05 14:29:48 +01:00
|
|
|
use rustc_span::{sym, Symbol};
|
2018-01-17 20:41:24 +11:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `#[inline]` on trait methods without bodies
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Only implementations of trait methods may be inlined.
|
2019-03-05 11:50:33 -05:00
|
|
|
/// The inline attribute is ignored for trait methods without bodies.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2019-03-05 11:50:33 -05:00
|
|
|
/// trait Animal {
|
|
|
|
/// #[inline]
|
|
|
|
/// fn name(&self) -> &'static str;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-01-17 20:41:24 +11:00
|
|
|
pub INLINE_FN_WITHOUT_BODY,
|
2018-03-29 13:41:53 +02:00
|
|
|
correctness,
|
2018-01-17 20:41:24 +11:00
|
|
|
"use of `#[inline]` on trait methods without bodies"
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
|
2018-01-17 20:41:24 +11:00
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
|
2020-03-16 16:00:16 +01:00
|
|
|
if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind {
|
2020-11-27 09:41:53 +01:00
|
|
|
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
|
|
|
check_attrs(cx, item.ident.name, attrs);
|
2018-01-17 20:41:24 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
fn check_attrs(cx: &LateContext<'_>, name: Symbol, attrs: &[Attribute]) {
|
2018-01-17 20:41:24 +11:00
|
|
|
for attr in attrs {
|
2020-11-05 14:29:48 +01:00
|
|
|
if !attr.has_name(sym::inline) {
|
2018-01-17 20:41:24 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-18 06:08:03 +11:00
|
|
|
span_lint_and_then(
|
2018-01-17 20:41:24 +11:00
|
|
|
cx,
|
|
|
|
INLINE_FN_WITHOUT_BODY,
|
|
|
|
attr.span,
|
2022-09-23 13:42:59 -04:00
|
|
|
&format!("use of `#[inline]` on trait method `{name}` which has no body"),
|
2020-04-17 08:08:00 +02:00
|
|
|
|diag| {
|
|
|
|
diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
|
2018-01-18 06:08:03 +11:00
|
|
|
},
|
2018-01-17 20:41:24 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|