Skip to content

Commit d5965aa

Browse files
committed
Auto merge of rust-lang#12370 - andylizi:macro-expand-underscore, r=lnicola
ide: insert whitespaces surrounding `_` in macro expansion #### Before ```rust for_in 0..10 { foo(); } ``` #### After ```rust for _ in 0..10 { foo(); } ```
2 parents cc8140a + e34ae76 commit d5965aa

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
5757
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
5858

5959
match tok.kind() {
60-
k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
60+
k if is_text(k) && is_next(|it| !it.is_punct() || it == UNDERSCORE, false) => {
6161
mods.push(do_ws(after, tok));
6262
}
6363
L_CURLY if is_next(|it| it != R_CURLY, true) => {
@@ -118,5 +118,5 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
118118
}
119119

120120
fn is_text(k: SyntaxKind) -> bool {
121-
k.is_keyword() || k.is_literal() || k == IDENT
121+
k.is_keyword() || k.is_literal() || k == IDENT || k == UNDERSCORE
122122
}

crates/ide/src/expand_macro.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,24 @@ fn main() {
237237
);
238238
}
239239

240+
#[test]
241+
fn macro_expand_underscore() {
242+
check(
243+
r#"
244+
macro_rules! bar {
245+
($i:tt) => { for _ in 0..$i {} }
246+
}
247+
fn main() {
248+
ba$0r!(42);
249+
}
250+
"#,
251+
expect![[r#"
252+
bar
253+
for _ in 0..42{}
254+
"#]],
255+
);
256+
}
257+
240258
#[test]
241259
fn macro_expand_recursive_expansion() {
242260
check(
@@ -385,7 +403,7 @@ fn main() {
385403
"#,
386404
expect![[r#"
387405
foo
388-
0 "#]],
406+
0"#]],
389407
);
390408
}
391409

0 commit comments

Comments
 (0)