rust-clippy/clippy_lints/src/inline_fn_without_body.rs

59 lines
1.8 KiB
Rust
Raw Normal View History

//! checks for `#[inline]` on trait methods without bodies
2018-05-30 08:15:50 +00:00
use crate::utils::span_lint_and_then;
use crate::utils::sugg::DiagnosticBuilderExt;
2020-05-08 11:57:01 +00:00
use rustc_ast::ast::Attribute;
use rustc_errors::Applicability;
use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
2020-01-12 06:08:41 +00:00
use rustc_lint::{LateContext, LateLintPass};
2020-01-11 11:37:08 +00:00
use rustc_session::{declare_lint_pass, declare_tool_lint};
2020-05-08 11:57:01 +00:00
use rustc_span::Symbol;
2018-03-28 13:24:26 +00:00
declare_clippy_lint! {
/// **What it does:** Checks for `#[inline]` on trait methods without bodies
///
/// **Why is this bad?** Only implementations of trait methods may be inlined.
/// The inline attribute is ignored for trait methods without bodies.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// trait Animal {
/// #[inline]
/// fn name(&self) -> &'static str;
/// }
/// ```
pub INLINE_FN_WITHOUT_BODY,
2018-03-29 11:41:53 +00:00
correctness,
"use of `#[inline]` on trait methods without bodies"
}
2019-04-08 20:43:55 +00:00
declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
2019-04-08 20:43:55 +00:00
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InlineFnWithoutBody {
2019-12-22 14:42:41 +00:00
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem<'_>) {
if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind {
2018-06-28 13:46:58 +00:00
check_attrs(cx, item.ident.name, &item.attrs);
}
}
}
2020-05-08 11:57:01 +00:00
fn check_attrs(cx: &LateContext<'_, '_>, name: Symbol, attrs: &[Attribute]) {
for attr in attrs {
2019-05-17 21:53:54 +00:00
if !attr.check_name(sym!(inline)) {
continue;
}
span_lint_and_then(
cx,
INLINE_FN_WITHOUT_BODY,
attr.span,
&format!("use of `#[inline]` on trait method `{}` which has no body", name),
|diag| {
diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
},
);
}
}