Skip to content

Commit

Permalink
Use break api config for pass by value or ref
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed May 26, 2021
1 parent d7f47f2 commit ee79077
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Expand Up @@ -1947,6 +1947,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
let pass_by_ref_or_value = pass_by_ref_or_value::PassByRefOrValue::new(
conf.trivial_copy_size_limit,
conf.pass_by_value_size_limit,
conf.avoid_breaking_exported_api,
&sess.target,
);
store.register_late_pass(move || box pass_by_ref_or_value);
Expand Down
13 changes: 11 additions & 2 deletions clippy_lints/src/pass_by_ref_or_value.rs
Expand Up @@ -102,10 +102,16 @@ declare_clippy_lint! {
pub struct PassByRefOrValue {
ref_min_size: u64,
value_max_size: u64,
avoid_breaking_exported_api: bool,
}

impl<'tcx> PassByRefOrValue {
pub fn new(ref_min_size: Option<u64>, value_max_size: u64, target: &Target) -> Self {
pub fn new(
ref_min_size: Option<u64>,
value_max_size: u64,
avoid_breaking_exported_api: bool,
target: &Target,
) -> Self {
let ref_min_size = ref_min_size.unwrap_or_else(|| {
let bit_width = u64::from(target.pointer_width);
// Cap the calculated bit width at 32-bits to reduce
Expand All @@ -120,10 +126,14 @@ impl<'tcx> PassByRefOrValue {
Self {
ref_min_size,
value_max_size,
avoid_breaking_exported_api,
}
}

fn check_poly_fn(&mut self, cx: &LateContext<'tcx>, hir_id: HirId, decl: &FnDecl<'_>, span: Option<Span>) {
if self.avoid_breaking_exported_api && cx.access_levels.is_exported(hir_id) {
return;
}
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);

let fn_sig = cx.tcx.fn_sig(fn_def_id);
Expand Down Expand Up @@ -184,7 +194,6 @@ impl<'tcx> PassByRefOrValue {
}

if_chain! {
if !cx.access_levels.is_exported(hir_id);
if is_copy(cx, ty);
if !is_self_ty(input);
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
Expand Down
8 changes: 1 addition & 7 deletions tests/ui/trivially_copy_pass_by_ref.stderr
Expand Up @@ -88,12 +88,6 @@ error: this argument (N byte) is passed by reference, but would be more efficien
LL | fn trait_method(&self, _foo: &Foo);
| ^^^^ help: consider passing by value instead: `Foo`

error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
--> $DIR/trivially_copy_pass_by_ref.rs:80:37
|
LL | fn trait_method2(&self, _color: &Color);
| ^^^^^^ help: consider passing by value instead: `Color`

error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
--> $DIR/trivially_copy_pass_by_ref.rs:108:21
|
Expand All @@ -106,5 +100,5 @@ error: this argument (N byte) is passed by reference, but would be more efficien
LL | fn foo(x: &i32) {
| ^^^^ help: consider passing by value instead: `i32`

error: aborting due to 17 previous errors
error: aborting due to 16 previous errors

0 comments on commit ee79077

Please sign in to comment.