Skip to content

Commit 96ac099

Browse files
committed
fix: nonstandard_macro_braces FN on macros with empty args
1 parent e629869 commit 96ac099

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed

clippy_lints/src/nonstandard_macro_braces.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,41 @@ impl EarlyLintPass for MacroBraces {
115115
}
116116

117117
fn is_offending_macro(cx: &EarlyContext<'_>, span: Span, mac_braces: &MacroBraces) -> Option<MacroInfo> {
118-
let unnested_or_local = || {
119-
!span.ctxt().outer_expn_data().call_site.from_expansion()
118+
let unnested_or_local = |span: Span| {
119+
!span.from_expansion()
120120
|| span
121121
.macro_backtrace()
122122
.last()
123123
.is_some_and(|e| e.macro_def_id.is_some_and(DefId::is_local))
124124
};
125-
let callsite_span = span.ctxt().outer_expn_data().call_site;
126-
if let ExpnKind::Macro(MacroKind::Bang, mac_name) = span.ctxt().outer_expn_data().kind
125+
126+
let mut ctxt = span.ctxt();
127+
while !ctxt.is_root() {
128+
let expn_data = ctxt.outer_expn_data();
129+
if let ExpnKind::Macro(MacroKind::Bang, mac_name) = expn_data.kind
127130
&& let name = mac_name.as_str()
128131
&& let Some(&braces) = mac_braces.macro_braces.get(name)
129-
&& let Some(snip) = callsite_span.get_source_text(cx)
132+
&& let Some(snip) = expn_data.call_site.get_source_text(cx)
130133
// we must check only invocation sites
131134
// https://github.com/rust-lang/rust-clippy/issues/7422
132135
&& let Some(macro_args_str) = snip.strip_prefix(name).and_then(|snip| snip.strip_prefix('!'))
133136
&& let Some(old_open_brace @ ('{' | '(' | '[')) = macro_args_str.trim_start().chars().next()
134137
&& old_open_brace != braces.0
135-
&& unnested_or_local()
136-
&& !mac_braces.done.contains(&callsite_span)
137-
{
138-
Some(MacroInfo {
139-
callsite_span,
140-
callsite_snippet: snip,
141-
old_open_brace,
142-
braces,
143-
})
144-
} else {
145-
None
138+
&& unnested_or_local(expn_data.call_site)
139+
&& !mac_braces.done.contains(&expn_data.call_site)
140+
{
141+
return Some(MacroInfo {
142+
callsite_span: expn_data.call_site,
143+
callsite_snippet: snip,
144+
old_open_brace,
145+
braces,
146+
});
147+
}
148+
149+
ctxt = expn_data.call_site.ctxt();
146150
}
151+
152+
None
147153
}
148154

149155
fn emit_help(cx: &EarlyContext<'_>, snip: &str, (open, close): (char, char), span: Span, add_semi: bool) {

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@aux-build:proc_macro_derive.rs
22

33
#![warn(clippy::nonstandard_macro_braces)]
4+
#![allow(clippy::println_empty_string)]
45

56
extern crate proc_macro_derive;
67
extern crate quote;
@@ -75,3 +76,16 @@ fn issue9913() {
7576
[0]; // separate statement, not indexing into the result of println.
7677
//~^^ nonstandard_macro_braces
7778
}
79+
80+
fn issue15594() {
81+
println!();
82+
println!("");
83+
println!();
84+
//~^ nonstandard_macro_braces
85+
println!("");
86+
//~^ nonstandard_macro_braces
87+
println!();
88+
//~^ nonstandard_macro_braces
89+
println!("");
90+
//~^ nonstandard_macro_braces
91+
}

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@aux-build:proc_macro_derive.rs
22

33
#![warn(clippy::nonstandard_macro_braces)]
4+
#![allow(clippy::println_empty_string)]
45

56
extern crate proc_macro_derive;
67
extern crate quote;
@@ -75,3 +76,16 @@ fn issue9913() {
7576
[0]; // separate statement, not indexing into the result of println.
7677
//~^^ nonstandard_macro_braces
7778
}
79+
80+
fn issue15594() {
81+
println!();
82+
println!("");
83+
println![];
84+
//~^ nonstandard_macro_braces
85+
println![""];
86+
//~^ nonstandard_macro_braces
87+
println! {};
88+
//~^ nonstandard_macro_braces
89+
println! {""};
90+
//~^ nonstandard_macro_braces
91+
}
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: use of irregular braces for `vec!` macro
2-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:44:13
2+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:45:13
33
|
44
LL | let _ = vec! {1, 2, 3};
55
| ^^^^^^^^^^^^^^ help: consider writing: `vec![1, 2, 3]`
@@ -8,31 +8,31 @@ LL | let _ = vec! {1, 2, 3};
88
= help: to override `-D warnings` add `#[allow(clippy::nonstandard_macro_braces)]`
99

1010
error: use of irregular braces for `format!` macro
11-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:46:13
11+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:47:13
1212
|
1313
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `format!("ugh {} stop being such a good compiler", "hello")`
1515

1616
error: use of irregular braces for `matches!` macro
17-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:48:13
17+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:49:13
1818
|
1919
LL | let _ = matches!{{}, ()};
2020
| ^^^^^^^^^^^^^^^^ help: consider writing: `matches!({}, ())`
2121

2222
error: use of irregular braces for `quote!` macro
23-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:50:13
23+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:51:13
2424
|
2525
LL | let _ = quote!(let x = 1;);
2626
| ^^^^^^^^^^^^^^^^^^ help: consider writing: `quote!{let x = 1;}`
2727

2828
error: use of irregular braces for `quote::quote!` macro
29-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:52:13
29+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:53:13
3030
|
3131
LL | let _ = quote::quote!(match match match);
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `quote::quote!{match match match}`
3333

3434
error: use of irregular braces for `vec!` macro
35-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:18:9
35+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:19:9
3636
|
3737
LL | vec!{0, 0, 0}
3838
| ^^^^^^^^^^^^^ help: consider writing: `vec![0, 0, 0]`
@@ -43,22 +43,46 @@ LL | let _ = test!(); // trigger when macro def is inside our own crate
4343
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
4444

4545
error: use of irregular braces for `type_pos!` macro
46-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:62:12
46+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:63:12
4747
|
4848
LL | let _: type_pos!(usize) = vec![];
4949
| ^^^^^^^^^^^^^^^^ help: consider writing: `type_pos![usize]`
5050

5151
error: use of irregular braces for `eprint!` macro
52-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:65:5
52+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:66:5
5353
|
5454
LL | eprint!("test if user config overrides defaults");
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `eprint!["test if user config overrides defaults"]`
5656

5757
error: use of irregular braces for `println!` macro
58-
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:74:5
58+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:75:5
5959
|
6060
LL | println! {"hello world"}
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `println!("hello world");`
6262

63-
error: aborting due to 9 previous errors
63+
error: use of irregular braces for `println!` macro
64+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:83:5
65+
|
66+
LL | println![];
67+
| ^^^^^^^^^^ help: consider writing: `println!()`
68+
69+
error: use of irregular braces for `println!` macro
70+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:85:5
71+
|
72+
LL | println![""];
73+
| ^^^^^^^^^^^^ help: consider writing: `println!("")`
74+
75+
error: use of irregular braces for `println!` macro
76+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:87:5
77+
|
78+
LL | println! {};
79+
| ^^^^^^^^^^^ help: consider writing: `println!()`
80+
81+
error: use of irregular braces for `println!` macro
82+
--> tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs:89:5
83+
|
84+
LL | println! {""};
85+
| ^^^^^^^^^^^^^ help: consider writing: `println!("")`
86+
87+
error: aborting due to 13 previous errors
6488

0 commit comments

Comments
 (0)