X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Finline_fn_without_body.rs;h=20f00bd51ba868dcf440314e8b11045e5764519c;hb=ebf88c9734246a790305f6faaba0bf8e3f150dff;hp=bdc17ab462442f7f6cd09e5adb98e199daa33470;hpb=13421e3945a28dad49226c4d08986eaacd1033d9;p=rust.git diff --git a/clippy_lints/src/inline_fn_without_body.rs b/clippy_lints/src/inline_fn_without_body.rs index bdc17ab4624..20f00bd51ba 100644 --- a/clippy_lints/src/inline_fn_without_body.rs +++ b/clippy_lints/src/inline_fn_without_body.rs @@ -1,57 +1,48 @@ //! checks for `#[inline]` on trait methods without bodies -use crate::utils::span_lint_and_then; -use crate::utils::sugg::DiagnosticBuilderExt; -use rustc::hir::*; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_tool_lint, lint_array}; +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::sugg::DiagnosticBuilderExt; +use rustc_ast::ast::Attribute; use rustc_errors::Applicability; -use syntax::ast::{Attribute, Name}; +use rustc_hir::{TraitFn, TraitItem, TraitItemKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_span::{sym, Symbol}; -/// **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; -/// } -/// ``` declare_clippy_lint! { + /// **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; + /// } + /// ``` pub INLINE_FN_WITHOUT_BODY, correctness, "use of `#[inline]` on trait methods without bodies" } -#[derive(Copy, Clone)] -pub struct Pass; +declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]); -impl LintPass for Pass { - fn get_lints(&self) -> LintArray { - lint_array!(INLINE_FN_WITHOUT_BODY) - } - - fn name(&self) -> &'static str { - "InlineFnWithoutBody" - } -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { - fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) { - if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node { - check_attrs(cx, item.ident.name, &item.attrs); +impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody { + fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) { + if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind { + let attrs = cx.tcx.hir().attrs(item.hir_id()); + check_attrs(cx, item.ident.name, attrs); } } } -fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) { +fn check_attrs(cx: &LateContext<'_>, name: Symbol, attrs: &[Attribute]) { for attr in attrs { - if attr.name() != "inline" { + if !attr.has_name(sym::inline) { continue; } @@ -60,8 +51,8 @@ fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) { INLINE_FN_WITHOUT_BODY, attr.span, &format!("use of `#[inline]` on trait method `{}` which has no body", name), - |db| { - db.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable); + |diag| { + diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable); }, ); }