]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/inline_fn_without_body.rs
Supplement DiagnosticBuilderExt with Applicability
[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 use crate::rustc_errors::Applicability;
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
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
41     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
42         if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
43             check_attrs(cx, item.ident.name, &item.attrs);
44         }
45     }
46 }
47
48 fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
49     for attr in attrs {
50         if attr.name() != "inline" {
51             continue;
52         }
53
54         span_lint_and_then(
55             cx,
56             INLINE_FN_WITHOUT_BODY,
57             attr.span,
58             &format!("use of `#[inline]` on trait method `{}` which has no body", name),
59             |db| {
60                 db.suggest_remove_item(cx, attr.span, "remove", Applicability::Unspecified);
61             },
62         );
63     }
64 }