1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
11 //! checks for `#[inline]` on trait methods without bodies
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;
21 /// **What it does:** Checks for `#[inline]` on trait methods without bodies
23 /// **Why is this bad?** Only implementations of trait methods may be inlined.
24 /// The inline attribute is ignored for trait methods without bodies.
26 /// **Known problems:** None.
32 /// fn name(&self) -> &'static str;
35 declare_clippy_lint! {
36 pub INLINE_FN_WITHOUT_BODY,
38 "use of `#[inline]` on trait methods without bodies"
41 #[derive(Copy, Clone)]
44 impl LintPass for Pass {
45 fn get_lints(&self) -> LintArray {
46 lint_array!(INLINE_FN_WITHOUT_BODY)
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);
58 fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
60 if attr.name() != "inline" {
66 INLINE_FN_WITHOUT_BODY,
68 &format!("use of `#[inline]` on trait method `{}` which has no body", name),
70 db.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);