Skip to content

Commit 700ece5

Browse files
committed
Allow configuring the trivial copy size limit
1 parent 7547a4d commit 700ece5

File tree

8 files changed

+74
-5
lines changed

8 files changed

+74
-5
lines changed

clippy_lints/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
400400
reg.register_late_lint_pass(box large_enum_variant::LargeEnumVariant::new(conf.enum_variant_size_threshold));
401401
reg.register_late_lint_pass(box explicit_write::Pass);
402402
reg.register_late_lint_pass(box needless_pass_by_value::NeedlessPassByValue);
403-
reg.register_late_lint_pass(box trivially_copy_pass_by_ref::TriviallyCopyPassByRef);
403+
reg.register_late_lint_pass(box trivially_copy_pass_by_ref::TriviallyCopyPassByRef::new(
404+
conf.trivial_copy_size_limit,
405+
&reg.sess.target,
406+
));
404407
reg.register_early_lint_pass(box literal_representation::LiteralDigitGrouping);
405408
reg.register_early_lint_pass(box literal_representation::LiteralRepresentation::new(
406409
conf.literal_representation_threshold

clippy_lints/src/trivially_copy_pass_by_ref.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::cmp;
2+
13
use rustc::hir::*;
24
use rustc::hir::map::*;
35
use rustc::hir::intravisit::FnKind;
46
use rustc::lint::*;
57
use rustc::ty::TypeVariants;
8+
use rustc::session::config::Config as SessionConfig;
69
use rustc_target::spec::abi::Abi;
710
use rustc_target::abi::LayoutOf;
811
use syntax::ast::NodeId;
@@ -17,6 +20,14 @@ use crate::utils::{in_macro, is_copy, is_self, span_lint_and_sugg, snippet};
1720
/// be passed through registers if they fit into two or less general purpose
1821
/// registers.
1922
///
23+
/// **Known problems:** This lint is target register size dependent, it is
24+
/// limited to 32-bit to try and reduce portability problems between 32 and
25+
/// 64-bit, but if you are compiling for 8 or 16-bit targets then the limit
26+
/// will be different.
27+
///
28+
/// The configuration option `trivial_copy_size_limit` can be set to override
29+
/// this limit for a project.
30+
///
2031
/// **Example:**
2132
/// ```rust
2233
/// fn foo(v: &u32) {
@@ -33,7 +44,24 @@ declare_clippy_lint! {
3344
"functions taking small copyable arguments by reference"
3445
}
3546

36-
pub struct TriviallyCopyPassByRef;
47+
pub struct TriviallyCopyPassByRef {
48+
limit: u64,
49+
}
50+
51+
impl TriviallyCopyPassByRef {
52+
pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
53+
let limit = limit.unwrap_or_else(|| {
54+
let bit_width = target.usize_ty.bit_width().expect("usize should have a width") as u64;
55+
// Cap the calculated bit width at 32-bits to reduce
56+
// portability problems between 32 and 64-bit targets
57+
let bit_width = cmp::min(bit_width, 32);
58+
let byte_width = bit_width / 8;
59+
// Use a limit of 2 times the register bit width
60+
byte_width * 2
61+
});
62+
Self { limit }
63+
}
64+
}
3765

3866
impl LintPass for TriviallyCopyPassByRef {
3967
fn get_lints(&self) -> LintArray {
@@ -94,7 +122,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
94122
if let TypeVariants::TyRef(_, ty, Mutability::MutImmutable) = ty.sty;
95123
if is_copy(cx, ty);
96124
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
97-
if size < 16;
125+
if size <= self.limit;
98126
if let Ty_::TyRptr(_, MutTy { ty: ref decl_ty, .. }) = input.node;
99127
then {
100128
let value_type = if is_self(arg) {

clippy_lints/src/utils/conf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ define_Conf! {
156156
(verbose_bit_mask_threshold, "verbose_bit_mask_threshold", 1 => u64),
157157
/// Lint: DECIMAL_LITERAL_REPRESENTATION. The lower bound for linting decimal literals
158158
(literal_representation_threshold, "literal_representation_threshold", 16384 => u64),
159+
/// Lint: TRIVIALLY_COPY_PASS_BY_REF. The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by reference.
160+
(trivial_copy_size_limit, "trivial_copy_size_limit", None => Option<u64>),
159161
}
160162

161163
/// Search for the configuration file.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trivial-copy-size-limit = 2
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![allow(many_single_char_names)]
2+
3+
#[derive(Copy, Clone)]
4+
struct Foo(u8);
5+
6+
#[derive(Copy, Clone)]
7+
struct Bar(u32);
8+
9+
fn good(a: &mut u32, b: u32, c: &Bar, d: &u32) {
10+
}
11+
12+
fn bad(x: &u16, y: &Foo) {
13+
}
14+
15+
fn main() {
16+
let (mut a, b, c, d, x, y) = (0, 0, Bar(0), 0, 0, Foo(0));
17+
good(&mut a, b, &c, &d);
18+
bad(&x, &y);
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this argument is passed by reference, but would be more efficient if passed by value
2+
--> $DIR/test.rs:12:11
3+
|
4+
12 | fn bad(x: &u16, y: &Foo) {
5+
| ^^^^ help: consider passing by value instead: `u16`
6+
|
7+
= note: `-D trivially-copy-pass-by-ref` implied by `-D warnings`
8+
9+
error: this argument is passed by reference, but would be more efficient if passed by value
10+
--> $DIR/test.rs:12:20
11+
|
12+
12 | fn bad(x: &u16, y: &Foo) {
13+
| ^^^^ help: consider passing by value instead: `Foo`
14+
15+
error: aborting due to 2 previous errors
16+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `third-party`
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `third-party`
22

33
error: aborting due to previous error
44

tests/ui/useless_asref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![deny(useless_asref)]
2-
2+
#![allow(trivially_copy_pass_by_ref)]
33
use std::fmt::Debug;
44

55
struct FakeAsRef;

0 commit comments

Comments
 (0)