2018-05-27 14:04:45 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2019-08-19 16:30:32 +00:00
|
|
|
use crate::utils::{is_copy, is_self_ty, snippet, span_lint_and_sugg};
|
2018-11-20 13:06:29 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use matches::matches;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir::intravisit::FnKind;
|
2019-12-03 23:16:03 +00:00
|
|
|
use rustc::impl_lint_pass;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::session::config::Config as SessionConfig;
|
2019-07-04 04:49:31 +00:00
|
|
|
use rustc::ty;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::*;
|
2019-12-03 23:16:03 +00:00
|
|
|
use rustc_session::declare_tool_lint;
|
2019-12-31 00:17:56 +00:00
|
|
|
use rustc_span::Span;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_target::abi::LayoutOf;
|
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-05-27 12:32:03 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for functions taking arguments by reference, where
|
|
|
|
/// the argument type is `Copy` and small enough to be more efficient to always
|
|
|
|
/// pass by value.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** In many calling conventions instances of structs will
|
|
|
|
/// be passed through registers if they fit into two or less general purpose
|
|
|
|
/// registers.
|
|
|
|
///
|
|
|
|
/// **Known problems:** This lint is target register size dependent, it is
|
|
|
|
/// limited to 32-bit to try and reduce portability problems between 32 and
|
|
|
|
/// 64-bit, but if you are compiling for 8 or 16-bit targets then the limit
|
|
|
|
/// will be different.
|
|
|
|
///
|
|
|
|
/// The configuration option `trivial_copy_size_limit` can be set to override
|
|
|
|
/// this limit for a project.
|
|
|
|
///
|
|
|
|
/// This lint attempts to allow passing arguments by reference if a reference
|
|
|
|
/// to that argument is returned. This is implemented by comparing the lifetime
|
|
|
|
/// of the argument and return value for equality. However, this can cause
|
|
|
|
/// false positives in cases involving multiple lifetimes that are bounded by
|
|
|
|
/// each other.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-08-03 06:01:27 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// // Bad
|
|
|
|
/// fn foo(v: &u32) {}
|
|
|
|
/// ```
|
|
|
|
///
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-08-03 06:01:27 +00:00
|
|
|
/// // Better
|
|
|
|
/// fn foo(v: u32) {}
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2018-05-27 12:32:03 +00:00
|
|
|
pub TRIVIALLY_COPY_PASS_BY_REF,
|
|
|
|
perf,
|
|
|
|
"functions taking small copyable arguments by reference"
|
|
|
|
}
|
|
|
|
|
2019-10-11 01:46:22 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2018-05-27 14:04:45 +00:00
|
|
|
pub struct TriviallyCopyPassByRef {
|
|
|
|
limit: u64,
|
|
|
|
}
|
|
|
|
|
2018-11-21 00:49:15 +00:00
|
|
|
impl<'a, 'tcx> TriviallyCopyPassByRef {
|
2018-05-27 14:04:45 +00:00
|
|
|
pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
|
|
|
|
let limit = limit.unwrap_or_else(|| {
|
2019-11-27 22:41:22 +00:00
|
|
|
let bit_width = u64::from(target.ptr_width);
|
2018-05-27 14:04:45 +00:00
|
|
|
// Cap the calculated bit width at 32-bits to reduce
|
|
|
|
// portability problems between 32 and 64-bit targets
|
|
|
|
let bit_width = cmp::min(bit_width, 32);
|
2019-06-11 16:53:12 +00:00
|
|
|
#[allow(clippy::integer_division)]
|
2018-05-27 14:04:45 +00:00
|
|
|
let byte_width = bit_width / 8;
|
2019-05-06 19:41:05 +00:00
|
|
|
// Use a limit of 2 times the register byte width
|
2018-05-27 14:04:45 +00:00
|
|
|
byte_width * 2
|
|
|
|
});
|
|
|
|
Self { limit }
|
|
|
|
}
|
2018-11-21 00:49:15 +00:00
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &FnDecl<'_>, span: Option<Span>) {
|
2019-07-06 03:52:51 +00:00
|
|
|
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
|
2018-11-21 00:49:15 +00:00
|
|
|
|
2019-07-03 05:15:21 +00:00
|
|
|
let fn_sig = cx.tcx.fn_sig(fn_def_id);
|
|
|
|
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
|
2018-11-21 00:49:15 +00:00
|
|
|
|
|
|
|
// Use lifetimes to determine if we're returning a reference to the
|
|
|
|
// argument. In that case we can't switch to pass-by-value as the
|
|
|
|
// argument will not live long enough.
|
2019-09-26 09:03:36 +00:00
|
|
|
let output_lts = match fn_sig.output().kind {
|
2018-12-04 22:19:42 +00:00
|
|
|
ty::Ref(output_lt, _, _) => vec![output_lt],
|
|
|
|
ty::Adt(_, substs) => substs.regions().collect(),
|
2018-11-21 00:49:15 +00:00
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
|
2019-07-03 05:15:21 +00:00
|
|
|
for (input, &ty) in decl.inputs.iter().zip(fn_sig.inputs()) {
|
2018-11-21 00:49:15 +00:00
|
|
|
// All spans generated from a proc-macro invocation are the same...
|
|
|
|
match span {
|
2018-11-21 12:08:33 +00:00
|
|
|
Some(s) if s == input.span => return,
|
2018-11-21 00:49:15 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
2019-12-21 18:38:45 +00:00
|
|
|
if let ty::Ref(input_lt, ty, Mutability::Not) = ty.kind;
|
2018-11-21 00:49:15 +00:00
|
|
|
if !output_lts.contains(&input_lt);
|
|
|
|
if is_copy(cx, ty);
|
|
|
|
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
|
|
|
|
if size <= self.limit;
|
2019-09-27 15:16:06 +00:00
|
|
|
if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.kind;
|
2018-11-21 00:49:15 +00:00
|
|
|
then {
|
|
|
|
let value_type = if is_self_ty(decl_ty) {
|
|
|
|
"self".into()
|
|
|
|
} else {
|
|
|
|
snippet(cx, decl_ty.span, "_").into()
|
|
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
TRIVIALLY_COPY_PASS_BY_REF,
|
|
|
|
input.span,
|
2019-05-06 19:41:05 +00:00
|
|
|
&format!("this argument ({} byte) is passed by reference, but would be more efficient if passed by value (limit: {} byte)", size, self.limit),
|
2018-11-21 00:49:15 +00:00
|
|
|
"consider passing by value instead",
|
2018-11-27 14:13:57 +00:00
|
|
|
value_type,
|
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
2018-11-21 00:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 14:04:45 +00:00
|
|
|
}
|
2018-05-27 12:32:03 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(TriviallyCopyPassByRef => [TRIVIALLY_COPY_PASS_BY_REF]);
|
2018-05-27 12:32:03 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) {
|
2019-08-19 16:30:32 +00:00
|
|
|
if item.span.from_expansion() {
|
2018-11-21 00:49:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-03 05:15:21 +00:00
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
if let hir::TraitItemKind::Method(method_sig, _) = &item.kind {
|
2019-07-03 05:15:21 +00:00
|
|
|
self.check_poly_fn(cx, item.hir_id, &*method_sig.decl, None);
|
2018-11-21 00:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 12:32:03 +00:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
kind: FnKind<'tcx>,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &'tcx FnDecl<'_>,
|
2019-12-22 14:42:41 +00:00
|
|
|
_body: &'tcx Body<'_>,
|
2018-05-27 12:32:03 +00:00
|
|
|
span: Span,
|
2019-02-20 10:11:11 +00:00
|
|
|
hir_id: HirId,
|
2018-05-27 12:32:03 +00:00
|
|
|
) {
|
2019-08-19 16:30:32 +00:00
|
|
|
if span.from_expansion() {
|
2018-05-27 12:32:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match kind {
|
2018-06-24 13:32:40 +00:00
|
|
|
FnKind::ItemFn(.., header, _, attrs) => {
|
|
|
|
if header.abi != Abi::Rust {
|
2018-05-27 12:32:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for a in attrs {
|
2019-05-17 21:53:54 +00:00
|
|
|
if a.meta_item_list().is_some() && a.check_name(sym!(proc_macro_derive)) {
|
2018-05-27 12:32:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 09:55:22 +00:00
|
|
|
},
|
2018-05-27 12:32:03 +00:00
|
|
|
FnKind::Method(..) => (),
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude non-inherent impls
|
2019-06-25 21:41:10 +00:00
|
|
|
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if matches!(item.kind, ItemKind::Impl(_, _, _, _, Some(_), _, _) |
|
2018-07-16 13:07:39 +00:00
|
|
|
ItemKind::Trait(..))
|
2018-05-27 12:32:03 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 05:15:21 +00:00
|
|
|
self.check_poly_fn(cx, hir_id, decl, Some(span));
|
2018-05-27 12:32:03 +00:00
|
|
|
}
|
|
|
|
}
|