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