Skip to content

Commit

Permalink
Lint inside macro when owned by current crate
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Jul 24, 2021
1 parent 60c688a commit cc0bc01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
9 changes: 8 additions & 1 deletion clippy_lints/src/nonstandard_macro_braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,21 @@ impl EarlyLintPass for MacroBraces {
}

fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, mac_braces: &'a MacroBraces) -> Option<MacroInfo<'a>> {
let unnested_or_local = || {
let nested = in_macro(span.ctxt().outer_expn_data().call_site);
let in_local_macro = nested
&& matches!(span.macro_backtrace().last().and_then(|e| e.macro_def_id), Some(defid) if defid.is_local());
!nested || in_local_macro
};
if_chain! {
// 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 in_macro(span);
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
// https://github.com/rust-lang/rust-clippy/issues/7422
if snip.starts_with(&format!("{}!", name));
if unnested_or_local();
// make formatting consistent
let c = snip.replace(" ", "");
if !c.starts_with(&format!("{}!{}", name, braces.0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!(); // don't trigger for macro calls inside macros
let _ = test!(); // trigger when macro def is inside our own crate
let _ = vec![1,2,3];

let _ = quote::quote! {true || false};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ 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!(); // trigger when macro def is inside our own crate
| ------- 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!(); // trigger when macro def is inside our own crate
| ------- 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 @@ -71,5 +90,5 @@ help: consider writing `eprint!["test if user config overrides defaults"];`
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

0 comments on commit cc0bc01

Please sign in to comment.