Skip to content

Commit ee79077

Browse files
committed
Use break api config for pass by value or ref
1 parent d7f47f2 commit ee79077

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
19471947
let pass_by_ref_or_value = pass_by_ref_or_value::PassByRefOrValue::new(
19481948
conf.trivial_copy_size_limit,
19491949
conf.pass_by_value_size_limit,
1950+
conf.avoid_breaking_exported_api,
19501951
&sess.target,
19511952
);
19521953
store.register_late_pass(move || box pass_by_ref_or_value);

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ declare_clippy_lint! {
102102
pub struct PassByRefOrValue {
103103
ref_min_size: u64,
104104
value_max_size: u64,
105+
avoid_breaking_exported_api: bool,
105106
}
106107

107108
impl<'tcx> PassByRefOrValue {
108-
pub fn new(ref_min_size: Option<u64>, value_max_size: u64, target: &Target) -> Self {
109+
pub fn new(
110+
ref_min_size: Option<u64>,
111+
value_max_size: u64,
112+
avoid_breaking_exported_api: bool,
113+
target: &Target,
114+
) -> Self {
109115
let ref_min_size = ref_min_size.unwrap_or_else(|| {
110116
let bit_width = u64::from(target.pointer_width);
111117
// Cap the calculated bit width at 32-bits to reduce
@@ -120,10 +126,14 @@ impl<'tcx> PassByRefOrValue {
120126
Self {
121127
ref_min_size,
122128
value_max_size,
129+
avoid_breaking_exported_api,
123130
}
124131
}
125132

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

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

186196
if_chain! {
187-
if !cx.access_levels.is_exported(hir_id);
188197
if is_copy(cx, ty);
189198
if !is_self_ty(input);
190199
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());

tests/ui/trivially_copy_pass_by_ref.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ error: this argument (N byte) is passed by reference, but would be more efficien
8888
LL | fn trait_method(&self, _foo: &Foo);
8989
| ^^^^ help: consider passing by value instead: `Foo`
9090

91-
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
92-
--> $DIR/trivially_copy_pass_by_ref.rs:80:37
93-
|
94-
LL | fn trait_method2(&self, _color: &Color);
95-
| ^^^^^^ help: consider passing by value instead: `Color`
96-
9791
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
9892
--> $DIR/trivially_copy_pass_by_ref.rs:108:21
9993
|
@@ -106,5 +100,5 @@ error: this argument (N byte) is passed by reference, but would be more efficien
106100
LL | fn foo(x: &i32) {
107101
| ^^^^ help: consider passing by value instead: `i32`
108102

109-
error: aborting due to 17 previous errors
103+
error: aborting due to 16 previous errors
110104

0 commit comments

Comments
 (0)