rust/clippy_lints/src/inline_fn_without_body.rs

64 lines
1.7 KiB
Rust
Raw Normal View History

//! checks for `#[inline]` on trait methods without bodies
2018-08-28 06:13:42 -05:00
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2018-07-29 04:04:40 -05:00
use rustc::{declare_tool_lint, lint_array};
use rustc::hir::*;
use syntax::ast::{Attribute, Name};
2018-05-30 03:15:50 -05:00
use crate::utils::span_lint_and_then;
use crate::utils::sugg::DiagnosticBuilderExt;
/// **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;
/// }
/// ```
2018-03-28 08:24:26 -05:00
declare_clippy_lint! {
pub INLINE_FN_WITHOUT_BODY,
2018-03-29 06:41:53 -05:00
correctness,
"use of `#[inline]` on trait methods without bodies"
}
#[derive(Copy, Clone)]
pub struct Pass;
impl LintPass for Pass {
fn get_lints(&self) -> LintArray {
lint_array!(INLINE_FN_WITHOUT_BODY)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
2018-06-28 08:46:58 -05:00
check_attrs(cx, item.ident.name, &item.attrs);
}
}
}
2018-07-23 06:01:12 -05:00
fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
for attr in attrs {
2018-05-04 03:17:30 -05:00
if attr.name() != "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),
|db| {
db.suggest_remove_item(cx, attr.span, "remove");
},
);
}
}