Skip to content

Commit dd07860

Browse files
committed
Auto merge of #5941 - ThibsG:InlineInCopyPassByRef, r=yaahc
Don't lint if it has always inline attribute Don't trigger the lint `trivially_copy_pass_by_ref` if it has `#[inline(always)]` attribute. Note: I am not particularly familiar with `inline` impacts, so I implemented this the way that if only `#[inline]` attribute is here (without `always`), the lint will still trigger. Also, it will still trigger if it has `#[inline(never)]`. Just tell me if it sounds too much conservative. Fixes: #5876 changelog: none
2 parents f21d10b + 191b6c7 commit dd07860

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clippy_lints/src/trivially_copy_pass_by_ref.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cmp;
22

33
use crate::utils::{is_copy, is_self_ty, snippet, span_lint_and_sugg};
44
use if_chain::if_chain;
5+
use rustc_ast::attr;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
78
use rustc_hir::intravisit::FnKind;
@@ -155,8 +156,12 @@ impl<'tcx> LateLintPass<'tcx> for TriviallyCopyPassByRef {
155156
return;
156157
}
157158
for a in attrs {
158-
if a.meta_item_list().is_some() && a.has_name(sym!(proc_macro_derive)) {
159-
return;
159+
if let Some(meta_items) = a.meta_item_list() {
160+
if a.has_name(sym!(proc_macro_derive))
161+
|| (a.has_name(sym!(inline)) && attr::list_contains_name(&meta_items, sym!(always)))
162+
{
163+
return;
164+
}
160165
}
161166
}
162167
},

tests/ui/trivially_copy_pass_by_ref.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ mod issue3992 {
9797
pub fn c(d: &u16) {}
9898
}
9999

100+
mod issue5876 {
101+
// Don't lint here as it is always inlined
102+
#[inline(always)]
103+
fn foo_always(x: &i32) {
104+
println!("{}", x);
105+
}
106+
107+
#[inline(never)]
108+
fn foo_never(x: &i32) {
109+
println!("{}", x);
110+
}
111+
112+
#[inline]
113+
fn foo(x: &i32) {
114+
println!("{}", x);
115+
}
116+
}
117+
100118
fn main() {
101119
let (mut foo, bar) = (Foo(0), Bar([0; 24]));
102120
let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);

tests/ui/trivially_copy_pass_by_ref.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,17 @@ error: this argument (N byte) is passed by reference, but would be more efficien
9494
LL | fn trait_method2(&self, _color: &Color);
9595
| ^^^^^^ help: consider passing by value instead: `Color`
9696

97-
error: aborting due to 15 previous errors
97+
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
98+
--> $DIR/trivially_copy_pass_by_ref.rs:108:21
99+
|
100+
LL | fn foo_never(x: &i32) {
101+
| ^^^^ help: consider passing by value instead: `i32`
102+
103+
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
104+
--> $DIR/trivially_copy_pass_by_ref.rs:113:15
105+
|
106+
LL | fn foo(x: &i32) {
107+
| ^^^^ help: consider passing by value instead: `i32`
108+
109+
error: aborting due to 17 previous errors
98110

0 commit comments

Comments
 (0)