Skip to content

Commit

Permalink
Only trigger for one level of macros
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Jul 24, 2021
1 parent 5bc5bfc commit f5c3ed4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 39 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/nonstandard_macro_braces.rs
Expand Up @@ -94,7 +94,8 @@ impl EarlyLintPass for MacroBraces {

fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, mac_braces: &'a MacroBraces) -> Option<MacroInfo<'a>> {
if_chain! {
if in_macro(span);
// Make sure we are only one level deep otherwise there are to many FP's
if in_macro(span) && !in_macro(span.ctxt().outer_expn_data().call_site);
if let Some((name, braces)) = find_matching_macro(span, &mac_braces.macro_braces);
if let Some(snip) = snippet_opt(cx, span.ctxt().outer_expn_data().call_site);
// we must check only invocation sites
Expand Down
1 change: 0 additions & 1 deletion tests/ui-toml/nonstandard_macro_braces/clippy.toml
Expand Up @@ -3,5 +3,4 @@ standard-macro-braces = [
{ name = "quote::quote", brace = "{" },
{ name = "eprint", brace = "[" },
{ name = "type_pos", brace = "[" },
{ name = "printlnfoo", brace = "[" },
]
Expand Up @@ -34,7 +34,7 @@ macro_rules! type_pos {

macro_rules! printlnfoo {
($thing:expr) => {
println!("hey {}", $thing)
println!("{}", $thing)
};
}

Expand All @@ -44,7 +44,7 @@ fn main() {
let _ = format!["ugh {} stop being such a good compiler", "hello"];
let _ = quote!(let x = 1;);
let _ = quote::quote!(match match match);
let _ = test!();
let _ = test!(); // don't trigger for macro calls inside macros
let _ = vec![1,2,3];

let _ = quote::quote! {true || false};
Expand All @@ -56,7 +56,5 @@ fn main() {

eprint!("test if user config overrides defaults");

println!("test if println triggers for printlnfoo");

printlnfoo!("you");
printlnfoo!["test if printlnfoo is triggered by println"];
}
Expand Up @@ -47,25 +47,6 @@ help: consider writing `quote::quote! {match match match}`
LL | let _ = quote::quote!(match match match);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of irregular braces for `vec!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:18:9
|
LL | vec!{0, 0, 0}
| ^^^^^^^^^^^^^
...
LL | let _ = test!();
| ------- in this macro invocation
|
help: consider writing `vec![0, 0, 0]`
--> $DIR/conf_nonstandard_macro_braces.rs:18:9
|
LL | vec!{0, 0, 0}
| ^^^^^^^^^^^^^
...
LL | let _ = test!();
| ------- in this macro invocation
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)

error: use of irregular braces for `type_pos!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:55:12
|
Expand All @@ -90,17 +71,5 @@ help: consider writing `eprint!["test if user config overrides defaults"];`
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of irregular braces for `printlnfoo!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:61:5
|
LL | printlnfoo!("you");
| ^^^^^^^^^^^^^^^^^^^
|
help: consider writing `printlnfoo!["you"];`
--> $DIR/conf_nonstandard_macro_braces.rs:61:5
|
LL | printlnfoo!("you");
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 6 previous errors

0 comments on commit f5c3ed4

Please sign in to comment.