about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-03-12 06:01:00 +0000
committerbors <bors@rust-lang.org>2019-03-12 06:01:00 +0000
commit94a8d9cbcdd56f5839dec7258ddc940de88aab87 (patch)
tree67d3c57ce2258ab9e4d8b2eba1c1d5266046df56
parent1cdac4a9c798f8e22a4db220020713175eb9159b (diff)
parent4896b259ebad8d3bfe54568698d2f24796824828 (diff)
downloadrust-94a8d9cbcdd56f5839dec7258ddc940de88aab87.tar.gz
rust-94a8d9cbcdd56f5839dec7258ddc940de88aab87.zip
Auto merge of #3868 - taiki-e:needless_pass_by_value, r=phansch
Filter out proc_macro and proc_macro_attribute

Related to https://github.com/rust-lang/rust-clippy/pull/1617

Fixes https://github.com/rust-lang/rust-clippy/issues/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)
-rw-r--r--clippy_lints/src/needless_pass_by_value.rs17
-rw-r--r--tests/ui/needless_pass_by_value_proc_macro.rs10
2 files changed, 21 insertions, 6 deletions
diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs
index 15d378bdd7f..a07b8f4942b 100644
--- a/clippy_lints/src/needless_pass_by_value.rs
+++ b/clippy_lints/src/needless_pass_by_value.rs
@@ -17,6 +17,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_errors::Applicability;
 use rustc_target::spec::abi::Abi;
 use std::borrow::Cow;
+use syntax::ast::Attribute;
 use syntax::errors::DiagnosticBuilder;
 use syntax_pos::Span;
 
@@ -88,14 +89,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
 
         match kind {
             FnKind::ItemFn(.., header, _, attrs) => {
-                if header.abi != Abi::Rust {
+                if header.abi != Abi::Rust || requires_exact_signature(attrs) {
                     return;
                 }
-                for a in attrs {
-                    if a.meta_item_list().is_some() && a.name() == "proc_macro_derive" {
-                        return;
-                    }
-                }
             },
             FnKind::Method(..) => (),
             _ => return,
@@ -323,6 +319,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
     }
 }
 
+/// Functions marked with these attributes must have the exact signature.
+fn requires_exact_signature(attrs: &[Attribute]) -> bool {
+    attrs.iter().any(|attr| {
+        ["proc_macro", "proc_macro_attribute", "proc_macro_derive"]
+            .iter()
+            .any(|&allow| attr.name() == allow)
+    })
+}
+
 struct MovedVariablesCtxt<'a, 'tcx: 'a> {
     cx: &'a LateContext<'a, 'tcx>,
     moved_vars: FxHashSet<HirId>,
diff --git a/tests/ui/needless_pass_by_value_proc_macro.rs b/tests/ui/needless_pass_by_value_proc_macro.rs
index 3142210f9bf..78a0e92d179 100644
--- a/tests/ui/needless_pass_by_value_proc_macro.rs
+++ b/tests/ui/needless_pass_by_value_proc_macro.rs
@@ -9,3 +9,13 @@ use proc_macro::TokenStream;
 pub fn foo(_input: TokenStream) -> TokenStream {
     unimplemented!()
 }
+
+#[proc_macro]
+pub fn bar(_input: TokenStream) -> TokenStream {
+    unimplemented!()
+}
+
+#[proc_macro_attribute]
+pub fn baz(_args: TokenStream, _input: TokenStream) -> TokenStream {
+    unimplemented!()
+}