2018-01-17 09:41:24 +00:00
|
|
|
//! 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;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use syntax::ast::{Attribute, Name};
|
2018-01-17 09:41:24 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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-01-17 09:41:24 +00:00
|
|
|
pub INLINE_FN_WITHOUT_BODY,
|
2018-03-29 11:41:53 +00:00
|
|
|
correctness,
|
2018-01-17 09:41:24 +00:00
|
|
|
"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)
|
|
|
|
}
|
2019-01-26 19:40:55 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"InlineFnWithoutBody"
|
|
|
|
}
|
2018-01-17 09:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
|
2018-03-15 15:07:15 +00:00
|
|
|
if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
|
2018-06-28 13:46:58 +00:00
|
|
|
check_attrs(cx, item.ident.name, &item.attrs);
|
2018-01-17 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
|
2018-01-17 09:41:24 +00:00
|
|
|
for attr in attrs {
|
2018-05-04 08:17:30 +00:00
|
|
|
if attr.name() != "inline" {
|
2018-01-17 09:41:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-17 19:08:03 +00:00
|
|
|
span_lint_and_then(
|
2018-01-17 09:41:24 +00:00
|
|
|
cx,
|
|
|
|
INLINE_FN_WITHOUT_BODY,
|
|
|
|
attr.span,
|
|
|
|
&format!("use of `#[inline]` on trait method `{}` which has no body", name),
|
2018-01-17 19:08:03 +00:00
|
|
|
|db| {
|
2018-09-18 17:01:17 +00:00
|
|
|
db.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
|
2018-01-17 19:08:03 +00:00
|
|
|
},
|
2018-01-17 09:41:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|