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