Skip to content

Commit

Permalink
Make trivial-copy-size-limit target independent
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Aug 28, 2024
1 parent ebcd6bc commit e966c5e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
4 changes: 2 additions & 2 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,9 @@ The maximum number of lines a function or method can have

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

**Default Value:** `target_pointer_width * 2`
**Default Value:** `8`

---
**Affected lints:**
Expand Down
5 changes: 2 additions & 3 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,9 @@ define_Conf! {
#[lints(too_many_lines)]
too_many_lines_threshold: u64 = 100,
/// The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by
/// reference. By default there is no limit
#[default_text = "target_pointer_width * 2"]
/// reference
#[lints(trivially_copy_pass_by_ref)]
trivial_copy_size_limit: Option<u64> = None,
trivial_copy_size_limit: u64 = 8,
/// The maximum complexity a type can have
#[lints(type_complexity)]
type_complexity_threshold: u64 = 250,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
let format_args = format_args_storage.clone();
store.register_late_pass(move |_| Box::new(explicit_write::ExplicitWrite::new(format_args.clone())));
store.register_late_pass(|_| Box::new(needless_pass_by_value::NeedlessPassByValue));
store.register_late_pass(move |tcx| Box::new(pass_by_ref_or_value::PassByRefOrValue::new(tcx, conf)));
store.register_late_pass(move |_| Box::new(pass_by_ref_or_value::PassByRefOrValue::new(conf)));
store.register_late_pass(|_| Box::new(ref_option_ref::RefOptionRef));
store.register_late_pass(|_| Box::new(infinite_iter::InfiniteIter));
store.register_late_pass(|_| Box::new(inline_fn_without_body::InlineFnWithoutBody));
Expand Down
29 changes: 8 additions & 21 deletions clippy_lints/src/pass_by_ref_or_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{cmp, iter};

use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
Expand All @@ -15,11 +13,12 @@ use rustc_hir::{BindingMode, Body, FnDecl, Impl, ItemKind, MutTy, Mutability, No
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::adjustment::{Adjust, PointerCoercion};
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, RegionKind, TyCtxt};
use rustc_middle::ty::{self, RegionKind};
use rustc_session::impl_lint_pass;
use rustc_span::def_id::LocalDefId;
use rustc_span::{sym, Span};
use rustc_target::spec::abi::Abi;
use std::iter;

declare_clippy_lint! {
/// ### What it does
Expand All @@ -33,13 +32,12 @@ declare_clippy_lint! {
/// 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.
/// Certain types can be different sizes depending on the target platform,
/// e.g. `&(usize, usize)` may lint on a 32-bit target but not a 64-bit target.
///
/// The configuration option `trivial_copy_size_limit` can be set to override
/// this limit for a project.
/// this limit for a project. The default limit is target independent to try
/// and reduce portability problems between 32-bit and 64-bit targets.
///
/// This lint attempts to allow passing arguments by reference if a reference
/// to that argument is returned. This is implemented by comparing the lifetime
Expand Down Expand Up @@ -111,20 +109,9 @@ pub struct PassByRefOrValue {
}

impl<'tcx> PassByRefOrValue {
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
let ref_min_size = conf.trivial_copy_size_limit.unwrap_or_else(|| {
let bit_width = u64::from(tcx.sess.target.pointer_width);
// 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);
#[expect(clippy::integer_division)]
let byte_width = bit_width / 8;
// Use a limit of 2 times the register byte width
byte_width * 2
});

pub fn new(conf: &'static Conf) -> Self {
Self {
ref_min_size,
ref_min_size: conf.trivial_copy_size_limit,
value_max_size: conf.pass_by_value_size_limit,
avoid_breaking_exported_api: conf.avoid_breaking_exported_api,
}
Expand Down

0 comments on commit e966c5e

Please sign in to comment.