]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/inline_fn_without_body.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / inline_fn_without_body.rs
CommitLineData
f20569fa
XL
1//! checks for `#[inline]` on trait methods without bodies
2
cdc7bbd5 3use clippy_utils::diagnostics::span_lint_and_then;
5e7ed085 4use clippy_utils::sugg::DiagnosticExt;
f20569fa
XL
5use rustc_ast::ast::Attribute;
6use rustc_errors::Applicability;
7use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
8use rustc_lint::{LateContext, LateLintPass};
4b012472 9use rustc_session::declare_lint_pass;
f20569fa
XL
10use rustc_span::{sym, Symbol};
11
12declare_clippy_lint! {
94222f64
XL
13 /// ### What it does
14 /// Checks for `#[inline]` on trait methods without bodies
f20569fa 15 ///
94222f64
XL
16 /// ### Why is this bad?
17 /// Only implementations of trait methods may be inlined.
f20569fa
XL
18 /// The inline attribute is ignored for trait methods without bodies.
19 ///
94222f64 20 /// ### Example
ed00b5ec 21 /// ```no_run
f20569fa
XL
22 /// trait Animal {
23 /// #[inline]
24 /// fn name(&self) -> &'static str;
25 /// }
26 /// ```
a2a8927a 27 #[clippy::version = "pre 1.29.0"]
f20569fa
XL
28 pub INLINE_FN_WITHOUT_BODY,
29 correctness,
30 "use of `#[inline]` on trait methods without bodies"
31}
32
33declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
34
35impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
36 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
37 if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind {
38 let attrs = cx.tcx.hir().attrs(item.hir_id());
39 check_attrs(cx, item.ident.name, attrs);
40 }
41 }
42}
43
44fn check_attrs(cx: &LateContext<'_>, name: Symbol, attrs: &[Attribute]) {
45 for attr in attrs {
46 if !attr.has_name(sym::inline) {
47 continue;
48 }
49
50 span_lint_and_then(
51 cx,
52 INLINE_FN_WITHOUT_BODY,
53 attr.span,
2b03887a 54 &format!("use of `#[inline]` on trait method `{name}` which has no body"),
f20569fa
XL
55 |diag| {
56 diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
57 },
58 );
59 }
60}