Skip to content

Commit 281bd79

Browse files
authored
Merge pull request #2148 from sinkuu/proc_macro
needless_pass_by_value false-positive in annotation
2 parents 327d995 + 9221bd9 commit 281bd79

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
121121
.zip(&body.arguments)
122122
.enumerate()
123123
{
124+
// All spans generated from a proc-macro invocation are the same...
125+
if span == input.span {
126+
return;
127+
}
128+
124129
// * Exclude a type that is specifically bounded by `Borrow`.
125130
// * Exclude a type whose reference also fulfills its bound.
126131
// (e.g. `std::convert::AsRef`, `serde::Serialize`)

mini-macro/src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
1-
#![feature(plugin_registrar, rustc_private)]
1+
#![feature(plugin_registrar, rustc_private, quote)]
22

33
extern crate rustc_plugin;
44
extern crate syntax;
55

6+
use rustc_plugin::Registry;
7+
use syntax::ast::MetaItem;
68
use syntax::codemap::Span;
7-
use syntax::tokenstream::TokenTree;
8-
use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
9+
use syntax::ext::base::{Annotatable, ExtCtxt, MacEager, MacResult, SyntaxExtension};
910
use syntax::ext::build::AstBuilder; // trait for expr_usize
10-
use rustc_plugin::Registry;
11+
use syntax::symbol::Symbol;
12+
use syntax::tokenstream::TokenTree;
1113

1214
fn expand_macro(cx: &mut ExtCtxt, sp: Span, _: &[TokenTree]) -> Box<MacResult + 'static> {
1315
let e = cx.expr_usize(sp, 42);
1416
let e = cx.expr_mut_addr_of(sp, e);
1517
MacEager::expr(cx.expr_mut_addr_of(sp, e))
1618
}
1719

20+
fn expand_attr_macro(cx: &mut ExtCtxt, _: Span, _: &MetaItem, annotated: Annotatable) -> Vec<Annotatable> {
21+
vec![
22+
Annotatable::Item(
23+
quote_item!(
24+
cx,
25+
#[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
26+
).unwrap()
27+
),
28+
annotated,
29+
]
30+
}
31+
1832
#[plugin_registrar]
1933
pub fn plugin_registrar(reg: &mut Registry) {
2034
reg.register_macro("mini_macro", expand_macro);
35+
reg.register_syntax_extension(
36+
Symbol::intern("mini_macro_attr"),
37+
SyntaxExtension::MultiModifier(Box::new(expand_attr_macro)),
38+
);
2139
}

tests/run-pass/procedural_macro.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![plugin(clippy_mini_macro_test)]
33

44
#[deny(warnings)]
5+
#[mini_macro_attr]
56
fn main() {
67
let _ = mini_macro!();
78
}

0 commit comments

Comments
 (0)