Skip to content

Commit 3cc6778

Browse files
committed
Auto merge of #7431 - DevinR528:fix-macro-brace, r=llogiq
Fix emitting in nested (proc_)macros for nonstandard_macro_braces lint fixes #7422 changelog: fixes false positives in [`nonstandard_macro_braces`]
2 parents 0a59c24 + 1d110f8 commit 3cc6778

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

clippy_lints/src/nonstandard_macro_braces.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ impl EarlyLintPass for MacroBraces {
9292
}
9393
}
9494

95-
fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, this: &'a MacroBraces) -> Option<MacroInfo<'a>> {
95+
fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, mac_braces: &'a MacroBraces) -> Option<MacroInfo<'a>> {
9696
if_chain! {
9797
if in_macro(span);
98-
if let Some((name, braces)) = find_matching_macro(span, &this.macro_braces);
98+
if let Some((name, braces)) = find_matching_macro(span, &mac_braces.macro_braces);
9999
if let Some(snip) = snippet_opt(cx, span.ctxt().outer_expn_data().call_site);
100-
let c = snip.replace(" ", ""); // make formatting consistent
100+
// we must check only invocation sites
101+
// https://github.com/rust-lang/rust-clippy/issues/7422
102+
if snip.starts_with(name);
103+
// make formatting consistent
104+
let c = snip.replace(" ", "");
101105
if !c.starts_with(&format!("{}!{}", name, braces.0));
102-
if !this.done.contains(&span.ctxt().outer_expn_data().call_site);
106+
if !mac_braces.done.contains(&span.ctxt().outer_expn_data().call_site);
103107
then {
104108
Some((name, braces, snip))
105109
} else {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: --emit=link
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::TokenStream;
9+
10+
#[proc_macro_derive(DeriveSomething)]
11+
pub fn derive(_: TokenStream) -> TokenStream {
12+
"fn _f() -> Vec<u8> { vec![] }".parse().unwrap()
13+
}
14+
15+
#[proc_macro]
16+
pub fn foo_bar(_: TokenStream) -> TokenStream {
17+
"fn issue_7422() { eprintln!(); }".parse().unwrap()
18+
}

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
// aux-build:proc_macro_derive.rs
2+
13
#![warn(clippy::nonstandard_macro_braces)]
24

5+
extern crate proc_macro_derive;
36
extern crate quote;
47

58
use quote::quote;
69

10+
#[derive(proc_macro_derive::DeriveSomething)]
11+
pub struct S;
12+
13+
proc_macro_derive::foo_bar!();
14+
715
#[rustfmt::skip]
816
macro_rules! test {
917
() => {

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
error: use of irregular braces for `vec!` macro
2-
--> $DIR/conf_nonstandard_macro_braces.rs:29:13
2+
--> $DIR/conf_nonstandard_macro_braces.rs:37:13
33
|
44
LL | let _ = vec! {1, 2, 3};
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::nonstandard-macro-braces` implied by `-D warnings`
88
help: consider writing `vec![1, 2, 3]`
9-
--> $DIR/conf_nonstandard_macro_braces.rs:29:13
9+
--> $DIR/conf_nonstandard_macro_braces.rs:37:13
1010
|
1111
LL | let _ = vec! {1, 2, 3};
1212
| ^^^^^^^^^^^^^^
1313

1414
error: use of irregular braces for `format!` macro
15-
--> $DIR/conf_nonstandard_macro_braces.rs:30:13
15+
--> $DIR/conf_nonstandard_macro_braces.rs:38:13
1616
|
1717
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
2020
help: consider writing `format!("ugh () stop being such a good compiler", "hello")`
21-
--> $DIR/conf_nonstandard_macro_braces.rs:30:13
21+
--> $DIR/conf_nonstandard_macro_braces.rs:38:13
2222
|
2323
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525

2626
error: use of irregular braces for `quote!` macro
27-
--> $DIR/conf_nonstandard_macro_braces.rs:31:13
27+
--> $DIR/conf_nonstandard_macro_braces.rs:39:13
2828
|
2929
LL | let _ = quote!(let x = 1;);
3030
| ^^^^^^^^^^^^^^^^^^
3131
|
3232
help: consider writing `quote! {let x = 1;}`
33-
--> $DIR/conf_nonstandard_macro_braces.rs:31:13
33+
--> $DIR/conf_nonstandard_macro_braces.rs:39:13
3434
|
3535
LL | let _ = quote!(let x = 1;);
3636
| ^^^^^^^^^^^^^^^^^^
3737

3838
error: use of irregular braces for `quote::quote!` macro
39-
--> $DIR/conf_nonstandard_macro_braces.rs:32:13
39+
--> $DIR/conf_nonstandard_macro_braces.rs:40:13
4040
|
4141
LL | let _ = quote::quote!(match match match);
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4343
|
4444
help: consider writing `quote::quote! {match match match}`
45-
--> $DIR/conf_nonstandard_macro_braces.rs:32:13
45+
--> $DIR/conf_nonstandard_macro_braces.rs:40:13
4646
|
4747
LL | let _ = quote::quote!(match match match);
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4949

5050
error: use of irregular braces for `vec!` macro
51-
--> $DIR/conf_nonstandard_macro_braces.rs:10:9
51+
--> $DIR/conf_nonstandard_macro_braces.rs:18:9
5252
|
5353
LL | vec!{0, 0, 0}
5454
| ^^^^^^^^^^^^^
@@ -57,7 +57,7 @@ LL | let _ = test!();
5757
| ------- in this macro invocation
5858
|
5959
help: consider writing `vec![0, 0, 0]`
60-
--> $DIR/conf_nonstandard_macro_braces.rs:10:9
60+
--> $DIR/conf_nonstandard_macro_braces.rs:18:9
6161
|
6262
LL | vec!{0, 0, 0}
6363
| ^^^^^^^^^^^^^
@@ -67,25 +67,25 @@ LL | let _ = test!();
6767
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
6868

6969
error: use of irregular braces for `type_pos!` macro
70-
--> $DIR/conf_nonstandard_macro_braces.rs:41:12
70+
--> $DIR/conf_nonstandard_macro_braces.rs:49:12
7171
|
7272
LL | let _: type_pos!(usize) = vec![];
7373
| ^^^^^^^^^^^^^^^^
7474
|
7575
help: consider writing `type_pos![usize]`
76-
--> $DIR/conf_nonstandard_macro_braces.rs:41:12
76+
--> $DIR/conf_nonstandard_macro_braces.rs:49:12
7777
|
7878
LL | let _: type_pos!(usize) = vec![];
7979
| ^^^^^^^^^^^^^^^^
8080

8181
error: use of irregular braces for `eprint!` macro
82-
--> $DIR/conf_nonstandard_macro_braces.rs:43:5
82+
--> $DIR/conf_nonstandard_macro_braces.rs:51:5
8383
|
8484
LL | eprint!("test if user config overrides defaults");
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8686
|
8787
help: consider writing `eprint!["test if user config overrides defaults"];`
88-
--> $DIR/conf_nonstandard_macro_braces.rs:43:5
88+
--> $DIR/conf_nonstandard_macro_braces.rs:51:5
8989
|
9090
LL | eprint!("test if user config overrides defaults");
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)