2018-10-06 11:18:06 -05:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-01-17 03:41:24 -06:00
|
|
|
//! checks for `#[inline]` on trait methods without bodies
|
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::rustc::hir::*;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::rustc_errors::Applicability;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::syntax::ast::{Attribute, Name};
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::span_lint_and_then;
|
|
|
|
use crate::utils::sugg::DiagnosticBuilderExt;
|
2018-01-17 03:41:24 -06:00
|
|
|
|
|
|
|
/// **What it does:** Checks for `#[inline]` on trait methods without bodies
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Only implementations of trait methods may be inlined.
|
|
|
|
/// The inline attribute is ignored for trait methods without bodies.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// trait Animal {
|
|
|
|
/// #[inline]
|
|
|
|
/// fn name(&self) -> &'static str;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2018-01-17 03:41:24 -06:00
|
|
|
pub INLINE_FN_WITHOUT_BODY,
|
2018-03-29 06:41:53 -05:00
|
|
|
correctness,
|
2018-01-17 03:41:24 -06:00
|
|
|
"use of `#[inline]` on trait methods without bodies"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Pass;
|
|
|
|
|
|
|
|
impl LintPass for Pass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(INLINE_FN_WITHOUT_BODY)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
|
2018-03-15 10:07:15 -05:00
|
|
|
if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
|
2018-06-28 08:46:58 -05:00
|
|
|
check_attrs(cx, item.ident.name, &item.attrs);
|
2018-01-17 03:41:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
|
2018-01-17 03:41:24 -06:00
|
|
|
for attr in attrs {
|
2018-05-04 03:17:30 -05:00
|
|
|
if attr.name() != "inline" {
|
2018-01-17 03:41:24 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-17 13:08:03 -06:00
|
|
|
span_lint_and_then(
|
2018-01-17 03:41:24 -06:00
|
|
|
cx,
|
|
|
|
INLINE_FN_WITHOUT_BODY,
|
|
|
|
attr.span,
|
|
|
|
&format!("use of `#[inline]` on trait method `{}` which has no body", name),
|
2018-01-17 13:08:03 -06:00
|
|
|
|db| {
|
2018-09-18 12:01:17 -05:00
|
|
|
db.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
|
2018-01-17 13:08:03 -06:00
|
|
|
},
|
2018-01-17 03:41:24 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|