Skip to content

Commit 44d37a4

Browse files
committed
Lint inside macro when owned by current crate
1 parent f5c3ed4 commit 44d37a4

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

clippy_lints/src/nonstandard_macro_braces.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,21 @@ impl EarlyLintPass for MacroBraces {
9393
}
9494

9595
fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, mac_braces: &'a MacroBraces) -> Option<MacroInfo<'a>> {
96+
let unnested_or_local = || {
97+
let nested = in_macro(span.ctxt().outer_expn_data().call_site);
98+
let in_local_macro = nested
99+
&& matches!(span.macro_backtrace().last().and_then(|e| e.macro_def_id), Some(defid) if defid.is_local());
100+
!nested || in_local_macro
101+
};
96102
if_chain! {
97103
// Make sure we are only one level deep otherwise there are to many FP's
98-
if in_macro(span) && !in_macro(span.ctxt().outer_expn_data().call_site);
104+
if in_macro(span);
99105
if let Some((name, braces)) = find_matching_macro(span, &mac_braces.macro_braces);
100106
if let Some(snip) = snippet_opt(cx, span.ctxt().outer_expn_data().call_site);
101107
// we must check only invocation sites
102108
// https://github.com/rust-lang/rust-clippy/issues/7422
103109
if snip.starts_with(&format!("{}!", name));
110+
if unnested_or_local();
104111
// make formatting consistent
105112
let c = snip.replace(" ", "");
106113
if !c.starts_with(&format!("{}!{}", name, braces.0));

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() {
4444
let _ = format!["ugh {} stop being such a good compiler", "hello"];
4545
let _ = quote!(let x = 1;);
4646
let _ = quote::quote!(match match match);
47-
let _ = test!(); // don't trigger for macro calls inside macros
47+
let _ = test!(); // trigger when macro def is inside our own crate
4848
let _ = vec![1,2,3];
4949

5050
let _ = quote::quote! {true || false};

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.stderr

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ help: consider writing `quote::quote! {match match match}`
4747
LL | let _ = quote::quote!(match match match);
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4949

50+
error: use of irregular braces for `vec!` macro
51+
--> $DIR/conf_nonstandard_macro_braces.rs:18:9
52+
|
53+
LL | vec!{0, 0, 0}
54+
| ^^^^^^^^^^^^^
55+
...
56+
LL | let _ = test!(); // trigger when macro def is inside our own crate
57+
| ------- in this macro invocation
58+
|
59+
help: consider writing `vec![0, 0, 0]`
60+
--> $DIR/conf_nonstandard_macro_braces.rs:18:9
61+
|
62+
LL | vec!{0, 0, 0}
63+
| ^^^^^^^^^^^^^
64+
...
65+
LL | let _ = test!(); // trigger when macro def is inside our own crate
66+
| ------- in this macro invocation
67+
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
68+
5069
error: use of irregular braces for `type_pos!` macro
5170
--> $DIR/conf_nonstandard_macro_braces.rs:55:12
5271
|
@@ -71,5 +90,5 @@ help: consider writing `eprint!["test if user config overrides defaults"];`
7190
LL | eprint!("test if user config overrides defaults");
7291
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7392

74-
error: aborting due to 6 previous errors
93+
error: aborting due to 7 previous errors
7594

0 commit comments

Comments
 (0)