rust/clippy_lints/src/inline_fn_without_body.rs

61 lines
1.9 KiB
Rust
Raw Normal View History

//! checks for `#[inline]` on trait methods without bodies
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::sugg::DiagnosticExt;
2020-05-08 06:57:01 -05:00
use rustc_ast::ast::Attribute;
use rustc_errors::Applicability;
use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
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};
use rustc_span::{sym, Symbol};
2018-03-28 08:24:26 -05: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.
///
/// ### Example
/// ```rust
/// trait Animal {
/// #[inline]
/// fn name(&self) -> &'static str;
/// }
/// ```
#[clippy::version = "pre 1.29.0"]
pub INLINE_FN_WITHOUT_BODY,
2018-03-29 06:41:53 -05:00
correctness,
"use of `#[inline]` on trait methods without bodies"
}
2019-04-08 15:43:55 -05:00
declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind {
2020-11-27 02:41:53 -06:00
let attrs = cx.tcx.hir().attrs(item.hir_id());
check_attrs(cx, item.ident.name, attrs);
}
}
}
fn check_attrs(cx: &LateContext<'_>, name: Symbol, attrs: &[Attribute]) {
for attr in attrs {
if !attr.has_name(sym::inline) {
continue;
}
span_lint_and_then(
cx,
INLINE_FN_WITHOUT_BODY,
attr.span,
&format!("use of `#[inline]` on trait method `{name}` which has no body"),
|diag| {
diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
},
);
}
}