Skip to content

Commit 94a8d9c

Browse files
committed
Auto merge of #3868 - taiki-e:needless_pass_by_value, r=phansch
Filter out proc_macro and proc_macro_attribute Related to #1617 Fixes #3067 (this issue has already been closed, but in fact the false positive in `#[proc_macro]` and `#[proc_macro_attribute]` has not been fixed yet)
2 parents 1cdac4a + 4896b25 commit 94a8d9c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1717
use rustc_errors::Applicability;
1818
use rustc_target::spec::abi::Abi;
1919
use std::borrow::Cow;
20+
use syntax::ast::Attribute;
2021
use syntax::errors::DiagnosticBuilder;
2122
use syntax_pos::Span;
2223

@@ -88,14 +89,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
8889

8990
match kind {
9091
FnKind::ItemFn(.., header, _, attrs) => {
91-
if header.abi != Abi::Rust {
92+
if header.abi != Abi::Rust || requires_exact_signature(attrs) {
9293
return;
9394
}
94-
for a in attrs {
95-
if a.meta_item_list().is_some() && a.name() == "proc_macro_derive" {
96-
return;
97-
}
98-
}
9995
},
10096
FnKind::Method(..) => (),
10197
_ => return,
@@ -323,6 +319,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
323319
}
324320
}
325321

322+
/// Functions marked with these attributes must have the exact signature.
323+
fn requires_exact_signature(attrs: &[Attribute]) -> bool {
324+
attrs.iter().any(|attr| {
325+
["proc_macro", "proc_macro_attribute", "proc_macro_derive"]
326+
.iter()
327+
.any(|&allow| attr.name() == allow)
328+
})
329+
}
330+
326331
struct MovedVariablesCtxt<'a, 'tcx: 'a> {
327332
cx: &'a LateContext<'a, 'tcx>,
328333
moved_vars: FxHashSet<HirId>,

tests/ui/needless_pass_by_value_proc_macro.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ use proc_macro::TokenStream;
99
pub fn foo(_input: TokenStream) -> TokenStream {
1010
unimplemented!()
1111
}
12+
13+
#[proc_macro]
14+
pub fn bar(_input: TokenStream) -> TokenStream {
15+
unimplemented!()
16+
}
17+
18+
#[proc_macro_attribute]
19+
pub fn baz(_args: TokenStream, _input: TokenStream) -> TokenStream {
20+
unimplemented!()
21+
}

0 commit comments

Comments
 (0)