2018-10-06 16:18:06 +00: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-05-27 14:04:45 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2018-07-19 07:24:19 +00:00
|
|
|
use matches::matches;
|
2018-09-15 07:21:58 +00:00
|
|
|
use crate::rustc::hir;
|
|
|
|
use crate::rustc::hir::*;
|
|
|
|
use crate::rustc::hir::intravisit::FnKind;
|
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
2018-07-19 08:00:54 +00:00
|
|
|
use if_chain::if_chain;
|
2018-09-15 07:21:58 +00:00
|
|
|
use crate::rustc::ty::TyKind;
|
|
|
|
use crate::rustc::session::config::Config as SessionConfig;
|
|
|
|
use crate::rustc_target::spec::abi::Abi;
|
|
|
|
use crate::rustc_target::abi::LayoutOf;
|
|
|
|
use crate::syntax::ast::NodeId;
|
|
|
|
use crate::syntax_pos::Span;
|
2018-05-27 12:32:03 +00:00
|
|
|
use crate::utils::{in_macro, is_copy, is_self, span_lint_and_sugg, snippet};
|
|
|
|
|
|
|
|
/// **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.
|
|
|
|
///
|
2018-05-27 14:04:45 +00:00
|
|
|
/// **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.
|
|
|
|
///
|
2018-07-23 09:33:52 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2018-05-27 12:32:03 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo(v: &u32) {
|
|
|
|
/// assert_eq!(v, 42);
|
|
|
|
/// }
|
|
|
|
/// // should be
|
|
|
|
/// fn foo(v: u32) {
|
|
|
|
/// assert_eq!(v, 42);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_clippy_lint! {
|
|
|
|
pub TRIVIALLY_COPY_PASS_BY_REF,
|
|
|
|
perf,
|
|
|
|
"functions taking small copyable arguments by reference"
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:04:45 +00:00
|
|
|
pub struct TriviallyCopyPassByRef {
|
|
|
|
limit: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TriviallyCopyPassByRef {
|
|
|
|
pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
|
|
|
|
let limit = limit.unwrap_or_else(|| {
|
|
|
|
let bit_width = target.usize_ty.bit_width().expect("usize should have a width") as u64;
|
|
|
|
// 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);
|
|
|
|
let byte_width = bit_width / 8;
|
|
|
|
// Use a limit of 2 times the register bit width
|
|
|
|
byte_width * 2
|
|
|
|
});
|
|
|
|
Self { limit }
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 12:32:03 +00:00
|
|
|
|
|
|
|
impl LintPass for TriviallyCopyPassByRef {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![TRIVIALLY_COPY_PASS_BY_REF]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
|
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
kind: FnKind<'tcx>,
|
|
|
|
decl: &'tcx FnDecl,
|
|
|
|
body: &'tcx Body,
|
|
|
|
span: Span,
|
|
|
|
node_id: NodeId,
|
|
|
|
) {
|
|
|
|
if in_macro(span) {
|
|
|
|
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 {
|
|
|
|
if a.meta_item_list().is_some() && a.name() == "proc_macro_derive" {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
FnKind::Method(..) => (),
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude non-inherent impls
|
2018-08-28 11:13:42 +00:00
|
|
|
if let Some(Node::Item(item)) = cx.tcx.hir.find(cx.tcx.hir.get_parent_node(node_id)) {
|
2018-07-16 13:07:39 +00:00
|
|
|
if matches!(item.node, ItemKind::Impl(_, _, _, _, Some(_), _, _) |
|
|
|
|
ItemKind::Trait(..))
|
2018-05-27 12:32:03 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let fn_def_id = cx.tcx.hir.local_def_id(node_id);
|
|
|
|
|
|
|
|
let fn_sig = cx.tcx.fn_sig(fn_def_id);
|
|
|
|
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
|
|
|
|
|
2018-07-23 09:37:41 +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.
|
2018-08-02 07:57:49 +00:00
|
|
|
let output_lts = match fn_sig.output().sty {
|
2018-08-22 21:34:52 +00:00
|
|
|
TyKind::Ref(output_lt, _, _) => vec![output_lt],
|
|
|
|
TyKind::Adt(_, substs) => substs.regions().collect(),
|
2018-08-02 07:57:49 +00:00
|
|
|
_ => vec![],
|
2018-07-23 08:33:47 +00:00
|
|
|
};
|
|
|
|
|
2018-05-27 12:32:03 +00:00
|
|
|
for ((input, &ty), arg) in decl.inputs.iter().zip(fn_sig.inputs()).zip(&body.arguments) {
|
|
|
|
// All spans generated from a proc-macro invocation are the same...
|
|
|
|
if span == input.span {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
2018-08-22 21:34:52 +00:00
|
|
|
if let TyKind::Ref(input_lt, ty, Mutability::MutImmutable) = ty.sty;
|
2018-08-02 07:57:49 +00:00
|
|
|
if !output_lts.contains(&input_lt);
|
2018-05-27 12:32:03 +00:00
|
|
|
if is_copy(cx, ty);
|
|
|
|
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
|
2018-05-27 14:04:45 +00:00
|
|
|
if size <= self.limit;
|
2018-08-22 21:34:52 +00:00
|
|
|
if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.node;
|
2018-05-27 12:32:03 +00:00
|
|
|
then {
|
|
|
|
let value_type = if is_self(arg) {
|
|
|
|
"self".into()
|
|
|
|
} else {
|
|
|
|
snippet(cx, decl_ty.span, "_").into()
|
|
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
TRIVIALLY_COPY_PASS_BY_REF,
|
|
|
|
input.span,
|
|
|
|
"this argument is passed by reference, but would be more efficient if passed by value",
|
|
|
|
"consider passing by value instead",
|
|
|
|
value_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|