]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/inline_fn_without_body.rs
Merge pull request #3466 from phansch/clippy_rfc_readme
[rust.git] / clippy_lints / src / inline_fn_without_body.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 //! checks for `#[inline]` on trait methods without bodies
12
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::rustc::hir::*;
16 use crate::syntax::ast::{Attribute, Name};
17 use crate::utils::span_lint_and_then;
18 use crate::utils::sugg::DiagnosticBuilderExt;
19 use crate::rustc_errors::Applicability;
20
21 /// **What it does:** Checks for `#[inline]` on trait methods without bodies
22 ///
23 /// **Why is this bad?** Only implementations of trait methods may be inlined.
24 /// The inline attribute is ignored for trait methods without bodies.
25 ///
26 /// **Known problems:** None.
27 ///
28 /// **Example:**
29 /// ```rust
30 /// trait Animal {
31 ///     #[inline]
32 ///     fn name(&self) -> &'static str;
33 /// }
34 /// ```
35 declare_clippy_lint! {
36     pub INLINE_FN_WITHOUT_BODY,
37     correctness,
38     "use of `#[inline]` on trait methods without bodies"
39 }
40
41 #[derive(Copy, Clone)]
42 pub struct Pass;
43
44 impl LintPass for Pass {
45     fn get_lints(&self) -> LintArray {
46         lint_array!(INLINE_FN_WITHOUT_BODY)
47     }
48 }
49
50 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
51     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
52         if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
53             check_attrs(cx, item.ident.name, &item.attrs);
54         }
55     }
56 }
57
58 fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
59     for attr in attrs {
60         if attr.name() != "inline" {
61             continue;
62         }
63
64         span_lint_and_then(
65             cx,
66             INLINE_FN_WITHOUT_BODY,
67             attr.span,
68             &format!("use of `#[inline]` on trait method `{}` which has no body", name),
69             |db| {
70                 db.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
71             },
72         );
73     }
74 }