Skip to content

Commit 95d0476

Browse files
committed
(fix) Ignore empty brackets
Compiler would crash if `[]` occured in the macro invocation (e.g. `vec![]`).
1 parent 8e0b790 commit 95d0476

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "interpolate_idents"
4-
version = "0.0.2"
4+
version = "0.0.3"
55
authors = ["Skyler Lipthay <[email protected]>"]
66
description = "Useable macro identifier concatenation plugin"
77
readme = "README.md"

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ fn interpolate_idents<'a>(cx: &'a mut ExtCtxt,
3939
}
4040
}
4141

42-
let new_ident = token::str_to_ident(&new_ident[..]);
43-
Some(TtToken(new_span.unwrap(), Token::Ident(new_ident, IdentStyle::Plain)))
42+
match new_span {
43+
Some(s) => {
44+
let new_ident = token::str_to_ident(&new_ident[..]);
45+
Some(TtToken(s, Token::Ident(new_ident, IdentStyle::Plain)))
46+
},
47+
None => None
48+
}
4449
},
4550
_ => None,
4651
}

tests/tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,21 @@ fn test_macro() {
2121
assert_eq!(foo_bar_1(), 1);
2222
assert_eq!(Foobar::new().bar_30, 30);
2323
}
24+
25+
macro_rules! define_brackets {
26+
() => ( interpolate_idents! {
27+
fn brackets() -> Vec<i32> {
28+
let mut b: Vec<i32> = vec![];
29+
let c: Vec<i32> = vec![1, 2, 3];
30+
b.push(c[1]);
31+
b
32+
}
33+
} )
34+
}
35+
36+
define_brackets!();
37+
38+
#[test]
39+
fn test_brackets() {
40+
assert_eq!(brackets(), vec![2]);
41+
}

0 commit comments

Comments
 (0)