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