]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/inline_fn_without_body.rs
Merge pull request #2819 from zayenz/no-op-ref-in-macro
[rust.git] / clippy_lints / src / inline_fn_without_body.rs
1 //! checks for `#[inline]` on trait methods without bodies
2
3 use rustc::lint::*;
4 use rustc::hir::*;
5 use syntax::ast::{Attribute, Name};
6 use crate::utils::span_lint_and_then;
7 use crate::utils::sugg::DiagnosticBuilderExt;
8
9 /// **What it does:** Checks for `#[inline]` on trait methods without bodies
10 ///
11 /// **Why is this bad?** Only implementations of trait methods may be inlined.
12 /// The inline attribute is ignored for trait methods without bodies.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// trait Animal {
19 ///     #[inline]
20 ///     fn name(&self) -> &'static str;
21 /// }
22 /// ```
23 declare_clippy_lint! {
24     pub INLINE_FN_WITHOUT_BODY,
25     correctness,
26     "use of `#[inline]` on trait methods without bodies"
27 }
28
29 #[derive(Copy, Clone)]
30 pub struct Pass;
31
32 impl LintPass for Pass {
33     fn get_lints(&self) -> LintArray {
34         lint_array!(INLINE_FN_WITHOUT_BODY)
35     }
36 }
37
38 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
39     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
40         if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
41             check_attrs(cx, &item.name, &item.attrs);
42         }
43     }
44 }
45
46 fn check_attrs(cx: &LateContext, name: &Name, attrs: &[Attribute]) {
47     for attr in attrs {
48         if attr.name() != "inline" {
49             continue;
50         }
51
52         span_lint_and_then(
53             cx,
54             INLINE_FN_WITHOUT_BODY,
55             attr.span,
56             &format!("use of `#[inline]` on trait method `{}` which has no body", name),
57             |db| {
58                 db.suggest_remove_item(cx, attr.span, "remove");
59             },
60         );
61     }
62 }