Skip to content

Commit e966c5e

Browse files
committed
Make trivial-copy-size-limit target independent
1 parent ebcd6bc commit e966c5e

File tree

4 files changed

+13
-27
lines changed

4 files changed

+13
-27
lines changed

book/src/lint_configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,9 @@ The maximum number of lines a function or method can have
862862

863863
## `trivial-copy-size-limit`
864864
The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by
865-
reference. By default there is no limit
865+
reference
866866

867-
**Default Value:** `target_pointer_width * 2`
867+
**Default Value:** `8`
868868

869869
---
870870
**Affected lints:**

clippy_config/src/conf.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,9 @@ define_Conf! {
612612
#[lints(too_many_lines)]
613613
too_many_lines_threshold: u64 = 100,
614614
/// The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by
615-
/// reference. By default there is no limit
616-
#[default_text = "target_pointer_width * 2"]
615+
/// reference
617616
#[lints(trivially_copy_pass_by_ref)]
618-
trivial_copy_size_limit: Option<u64> = None,
617+
trivial_copy_size_limit: u64 = 8,
619618
/// The maximum complexity a type can have
620619
#[lints(type_complexity)]
621620
type_complexity_threshold: u64 = 250,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
699699
let format_args = format_args_storage.clone();
700700
store.register_late_pass(move |_| Box::new(explicit_write::ExplicitWrite::new(format_args.clone())));
701701
store.register_late_pass(|_| Box::new(needless_pass_by_value::NeedlessPassByValue));
702-
store.register_late_pass(move |tcx| Box::new(pass_by_ref_or_value::PassByRefOrValue::new(tcx, conf)));
702+
store.register_late_pass(move |_| Box::new(pass_by_ref_or_value::PassByRefOrValue::new(conf)));
703703
store.register_late_pass(|_| Box::new(ref_option_ref::RefOptionRef));
704704
store.register_late_pass(|_| Box::new(infinite_iter::InfiniteIter));
705705
store.register_late_pass(|_| Box::new(inline_fn_without_body::InlineFnWithoutBody));

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::{cmp, iter};
2-
31
use clippy_config::Conf;
42
use clippy_utils::diagnostics::span_lint_and_sugg;
53
use clippy_utils::source::snippet;
@@ -15,11 +13,12 @@ use rustc_hir::{BindingMode, Body, FnDecl, Impl, ItemKind, MutTy, Mutability, No
1513
use rustc_lint::{LateContext, LateLintPass};
1614
use rustc_middle::ty::adjustment::{Adjust, PointerCoercion};
1715
use rustc_middle::ty::layout::LayoutOf;
18-
use rustc_middle::ty::{self, RegionKind, TyCtxt};
16+
use rustc_middle::ty::{self, RegionKind};
1917
use rustc_session::impl_lint_pass;
2018
use rustc_span::def_id::LocalDefId;
2119
use rustc_span::{sym, Span};
2220
use rustc_target::spec::abi::Abi;
21+
use std::iter;
2322

2423
declare_clippy_lint! {
2524
/// ### What it does
@@ -33,13 +32,12 @@ declare_clippy_lint! {
3332
/// registers.
3433
///
3534
/// ### Known problems
36-
/// This lint is target register size dependent, it is
37-
/// limited to 32-bit to try and reduce portability problems between 32 and
38-
/// 64-bit, but if you are compiling for 8 or 16-bit targets then the limit
39-
/// will be different.
35+
/// Certain types can be different sizes depending on the target platform,
36+
/// e.g. `&(usize, usize)` may lint on a 32-bit target but not a 64-bit target.
4037
///
4138
/// The configuration option `trivial_copy_size_limit` can be set to override
42-
/// this limit for a project.
39+
/// this limit for a project. The default limit is target independent to try
40+
/// and reduce portability problems between 32-bit and 64-bit targets.
4341
///
4442
/// This lint attempts to allow passing arguments by reference if a reference
4543
/// to that argument is returned. This is implemented by comparing the lifetime
@@ -111,20 +109,9 @@ pub struct PassByRefOrValue {
111109
}
112110

113111
impl<'tcx> PassByRefOrValue {
114-
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
115-
let ref_min_size = conf.trivial_copy_size_limit.unwrap_or_else(|| {
116-
let bit_width = u64::from(tcx.sess.target.pointer_width);
117-
// Cap the calculated bit width at 32-bits to reduce
118-
// portability problems between 32 and 64-bit targets
119-
let bit_width = cmp::min(bit_width, 32);
120-
#[expect(clippy::integer_division)]
121-
let byte_width = bit_width / 8;
122-
// Use a limit of 2 times the register byte width
123-
byte_width * 2
124-
});
125-
112+
pub fn new(conf: &'static Conf) -> Self {
126113
Self {
127-
ref_min_size,
114+
ref_min_size: conf.trivial_copy_size_limit,
128115
value_max_size: conf.pass_by_value_size_limit,
129116
avoid_breaking_exported_api: conf.avoid_breaking_exported_api,
130117
}

0 commit comments

Comments
 (0)