Skip to content

Commit 2179f4d

Browse files
authored
Merge pull request #2284 from rust-lang-nursery/new-macro
Move mini-macro to proc macro
2 parents f016862 + 775372d commit 2179f4d

File tree

4 files changed

+20
-44
lines changed

4 files changed

+20
-44
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ compiletest_rs = "0.3"
4747
duct = "0.8.2"
4848
lazy_static = "1.0"
4949
serde_derive = "1.0"
50-
clippy-mini-macro-test = { version = "0.1", path = "mini-macro" }
50+
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
5151
serde = "1.0"
5252

5353
[features]

mini-macro/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clippy-mini-macro-test"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = [
55
"Manish Goregaokar <[email protected]>",
66
"Andre Bogus <[email protected]>",
@@ -14,6 +14,6 @@ repository = "https://github.com/rust-lang-nursery/rust-clippy"
1414

1515
[lib]
1616
name = "clippy_mini_macro_test"
17-
plugin = true
17+
proc-macro = true
1818

1919
[dependencies]

mini-macro/src/lib.rs

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
1-
#![feature(plugin_registrar, rustc_private, quote)]
1+
#![feature(proc_macro)]
2+
extern crate proc_macro;
23

3-
extern crate rustc_plugin;
4-
extern crate syntax;
4+
use proc_macro::{TokenStream, quote};
55

6-
use rustc_plugin::Registry;
7-
use syntax::ast::MetaItem;
8-
use syntax::codemap::Span;
9-
use syntax::ext::base::{Annotatable, ExtCtxt, MacEager, MacResult, SyntaxExtension};
10-
use syntax::ext::build::AstBuilder; // trait for expr_usize
11-
use syntax::symbol::Symbol;
12-
use syntax::tokenstream::TokenTree;
13-
14-
fn expand_macro(cx: &mut ExtCtxt, sp: Span, _: &[TokenTree]) -> Box<MacResult + 'static> {
15-
let e = cx.expr_usize(sp, 42);
16-
let e = cx.expr_mut_addr_of(sp, e);
17-
MacEager::expr(cx.expr_mut_addr_of(sp, e))
18-
}
19-
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-
32-
#[plugin_registrar]
33-
pub fn plugin_registrar(reg: &mut Registry) {
34-
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-
);
39-
}
6+
#[proc_macro_derive(ClippyMiniMacroTest)]
7+
pub fn mini_macro(_: TokenStream) -> TokenStream {
8+
quote!(
9+
#[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
10+
)
11+
}

tests/run-pass/procedural_macro.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
#![feature(plugin)]
2-
#![plugin(clippy_mini_macro_test)]
1+
#[macro_use]
2+
extern crate clippy_mini_macro_test;
33

44
#[deny(warnings)]
5-
#[mini_macro_attr]
65
fn main() {
7-
let _ = mini_macro!();
6+
let x = Foo;
7+
println!("{:?}", x);
88
}
9+
10+
11+
#[derive(ClippyMiniMacroTest, Debug)]
12+
struct Foo;

0 commit comments

Comments
 (0)