2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{self, LateContext, LateLintPass, LintContext};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2020-11-05 07:29:48 -06:00
|
|
|
use rustc_span::sym;
|
2018-07-04 03:51:04 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
2021-10-21 01:33:43 -05:00
|
|
|
/// It lints if an exported function, method, trait method with default impl,
|
2019-03-05 10:50:33 -06:00
|
|
|
/// or trait method impl is not `#[inline]`.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// In general, it is not. Functions can be inlined across
|
2019-03-05 10:50:33 -06:00
|
|
|
/// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
|
|
|
|
/// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
|
|
|
|
/// might intend for most of the methods in their public API to be able to be inlined across
|
|
|
|
/// crates even when LTO is disabled. For these types of crates, enabling this lint might make
|
|
|
|
/// sense. It allows the crate to require all exported methods to be `#[inline]` by default, and
|
|
|
|
/// then opt out for specific methods where this might not make sense.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
|
|
|
/// pub fn foo() {} // missing #[inline]
|
|
|
|
/// fn ok() {} // ok
|
|
|
|
/// #[inline] pub fn bar() {} // ok
|
|
|
|
/// #[inline(always)] pub fn baz() {} // ok
|
|
|
|
///
|
|
|
|
/// pub trait Bar {
|
|
|
|
/// fn bar(); // ok
|
|
|
|
/// fn def_bar() {} // missing #[inline]
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct Baz;
|
|
|
|
/// impl Baz {
|
2019-08-03 14:24:50 -05:00
|
|
|
/// fn private() {} // ok
|
2019-03-05 10:50:33 -06:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Bar for Baz {
|
|
|
|
/// fn bar() {} // ok - Baz is not exported
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// pub struct PubBaz;
|
|
|
|
/// impl PubBaz {
|
2019-08-03 14:24:50 -05:00
|
|
|
/// fn private() {} // ok
|
|
|
|
/// pub fn not_ptrivate() {} // missing #[inline]
|
2019-03-05 10:50:33 -06:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Bar for PubBaz {
|
|
|
|
/// fn bar() {} // missing #[inline]
|
|
|
|
/// fn def_bar() {} // missing #[inline]
|
|
|
|
/// }
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-07-04 03:51:04 -05:00
|
|
|
pub MISSING_INLINE_IN_PUBLIC_ITEMS,
|
|
|
|
restriction,
|
2020-01-06 00:30:43 -06:00
|
|
|
"detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
|
2020-11-05 07:29:48 -06:00
|
|
|
let has_inline = attrs.iter().any(|a| a.has_name(sym::inline));
|
2018-07-04 18:53:40 -05:00
|
|
|
if !has_inline {
|
2018-08-28 06:13:42 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
2018-07-04 18:53:40 -05:00
|
|
|
MISSING_INLINE_IN_PUBLIC_ITEMS,
|
|
|
|
sp,
|
|
|
|
&format!("missing `#[inline]` for {}", desc),
|
|
|
|
);
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 08:30:50 -06:00
|
|
|
fn is_executable_or_proc_macro(cx: &LateContext<'_>) -> bool {
|
2020-03-18 17:45:02 -05:00
|
|
|
use rustc_session::config::CrateType;
|
2018-07-04 09:39:52 -05:00
|
|
|
|
2020-07-14 07:59:59 -05:00
|
|
|
cx.tcx
|
|
|
|
.sess
|
|
|
|
.crate_types()
|
|
|
|
.iter()
|
2021-03-12 08:30:50 -06:00
|
|
|
.any(|t: &CrateType| matches!(t, CrateType::Executable | CrateType::ProcMacro))
|
2018-07-04 09:39:52 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
|
2018-07-04 03:51:04 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MissingInline {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
|
2021-03-12 08:30:50 -06:00
|
|
|
if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable_or_proc_macro(cx) {
|
2018-07-04 09:39:52 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-28 17:07:32 -05:00
|
|
|
if !cx.access_levels.is_exported(it.def_id) {
|
2018-07-04 03:51:04 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-09-27 10:16:06 -05:00
|
|
|
match it.kind {
|
2018-07-16 08:07:39 -05:00
|
|
|
hir::ItemKind::Fn(..) => {
|
2018-07-04 03:51:04 -05:00
|
|
|
let desc = "a function";
|
2021-01-24 06:17:54 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(it.hir_id());
|
|
|
|
check_missing_inline_attrs(cx, attrs, it.span, desc);
|
2018-07-04 03:51:04 -05:00
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, _bounds, trait_items) => {
|
2018-07-04 08:32:55 -05:00
|
|
|
// note: we need to check if the trait is exported so we can't use
|
|
|
|
// `LateLintPass::check_trait_item` here.
|
2018-07-04 03:51:04 -05:00
|
|
|
for tit in trait_items {
|
2018-12-07 18:56:03 -06:00
|
|
|
let tit_ = cx.tcx.hir().trait_item(tit.id);
|
2019-09-27 10:16:06 -05:00
|
|
|
match tit_.kind {
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
|
2020-03-12 14:56:55 -05:00
|
|
|
hir::TraitItemKind::Fn(..) => {
|
2018-07-04 03:51:04 -05:00
|
|
|
if tit.defaultness.has_value() {
|
|
|
|
// trait method with default body needs inline in case
|
|
|
|
// an impl is not provided
|
|
|
|
let desc = "a default trait method";
|
2021-01-30 05:06:04 -06:00
|
|
|
let item = cx.tcx.hir().trait_item(tit.id);
|
2020-11-27 02:41:53 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
|
|
|
check_missing_inline_attrs(cx, attrs, item.span, desc);
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
|
|
|
hir::ItemKind::Const(..)
|
|
|
|
| hir::ItemKind::Enum(..)
|
2021-08-05 18:58:46 -05:00
|
|
|
| hir::ItemKind::Macro(..)
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::ItemKind::Mod(..)
|
|
|
|
| hir::ItemKind::Static(..)
|
|
|
|
| hir::ItemKind::Struct(..)
|
|
|
|
| hir::ItemKind::TraitAlias(..)
|
|
|
|
| hir::ItemKind::GlobalAsm(..)
|
2019-08-05 00:30:01 -05:00
|
|
|
| hir::ItemKind::TyAlias(..)
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::ItemKind::Union(..)
|
2019-08-03 01:44:32 -05:00
|
|
|
| hir::ItemKind::OpaqueTy(..)
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::ItemKind::ExternCrate(..)
|
2020-11-11 15:40:09 -06:00
|
|
|
| hir::ItemKind::ForeignMod { .. }
|
2020-01-17 23:14:36 -06:00
|
|
|
| hir::ItemKind::Impl { .. }
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::ItemKind::Use(..) => {},
|
2018-07-04 03:51:04 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ty::{ImplContainer, TraitContainer};
|
2021-03-12 08:30:50 -06:00
|
|
|
if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable_or_proc_macro(cx) {
|
2018-07-04 09:39:52 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-07-04 03:51:04 -05:00
|
|
|
|
|
|
|
// If the item being implemented is not exported, then we don't need #[inline]
|
2021-07-28 17:07:32 -05:00
|
|
|
if !cx.access_levels.is_exported(impl_item.def_id) {
|
2018-07-04 03:51:04 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
let desc = match impl_item.kind {
|
2020-03-16 10:00:16 -05:00
|
|
|
hir::ImplItemKind::Fn(..) => "a method",
|
2020-05-10 09:05:06 -05:00
|
|
|
hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) => return,
|
2018-07-04 08:32:55 -05:00
|
|
|
};
|
|
|
|
|
2021-01-30 16:25:03 -06:00
|
|
|
let trait_def_id = match cx.tcx.associated_item(impl_item.def_id).container {
|
2018-07-08 01:03:11 -05:00
|
|
|
TraitContainer(cid) => Some(cid),
|
|
|
|
ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(trait_def_id) = trait_def_id {
|
2021-07-28 17:07:32 -05:00
|
|
|
if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.def_id) {
|
2019-03-26 04:55:03 -05:00
|
|
|
// If a trait is being implemented for an item, and the
|
|
|
|
// trait is not exported, we don't need #[inline]
|
|
|
|
return;
|
2018-07-08 01:03:11 -05:00
|
|
|
}
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 02:55:10 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
|
|
|
|
check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
}
|