Lint inside macro when owned by current crate

This commit is contained in:
Devin Ragotzy 2021-07-24 07:03:52 -04:00
parent f5c3ed4463
commit 44d37a44bc
3 changed files with 29 additions and 3 deletions

View File

@ -93,14 +93,21 @@ fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
}
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));

View File

@ -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};

View File

@ -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
|
@ -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