Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nonstandard_macro_braces FP and docs of disallowed_types #7478

Merged
merged 5 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion clippy_lints/src/nonstandard_macro_braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clippy_utils::{diagnostics::span_lint_and_help, in_macro, is_direct_expn_of,
use if_chain::if_chain;
use rustc_ast::ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;
Expand Down Expand Up @@ -93,13 +94,23 @@ 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);
!nested
|| span
.macro_backtrace()
.last()
.map_or(false, |e| e.macro_def_id.map_or(false, DefId::is_local))
};
if_chain! {
// Make sure we are only one level deep otherwise there are to many FP's
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(name);
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 @@ -32,13 +32,19 @@ macro_rules! type_pos {
};
}

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

#[rustfmt::skip]
fn main() {
let _ = vec! {1, 2, 3};
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!(); // trigger when macro def is inside our own crate
let _ = vec![1,2,3];

let _ = quote::quote! {true || false};
Expand All @@ -49,4 +55,6 @@ fn main() {
let _: type_pos!(usize) = vec![];

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

printlnfoo!["test if printlnfoo is triggered by println"];
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
error: use of irregular braces for `vec!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:37:13
--> $DIR/conf_nonstandard_macro_braces.rs:43:13
|
LL | let _ = vec! {1, 2, 3};
| ^^^^^^^^^^^^^^
|
= note: `-D clippy::nonstandard-macro-braces` implied by `-D warnings`
help: consider writing `vec![1, 2, 3]`
--> $DIR/conf_nonstandard_macro_braces.rs:37:13
--> $DIR/conf_nonstandard_macro_braces.rs:43:13
|
LL | let _ = vec! {1, 2, 3};
| ^^^^^^^^^^^^^^

error: use of irregular braces for `format!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:38:13
--> $DIR/conf_nonstandard_macro_braces.rs:44:13
|
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider writing `format!("ugh () stop being such a good compiler", "hello")`
--> $DIR/conf_nonstandard_macro_braces.rs:38:13
--> $DIR/conf_nonstandard_macro_braces.rs:44:13
|
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of irregular braces for `quote!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:39:13
--> $DIR/conf_nonstandard_macro_braces.rs:45:13
|
LL | let _ = quote!(let x = 1;);
| ^^^^^^^^^^^^^^^^^^
|
help: consider writing `quote! {let x = 1;}`
--> $DIR/conf_nonstandard_macro_braces.rs:39:13
--> $DIR/conf_nonstandard_macro_braces.rs:45:13
|
LL | let _ = quote!(let x = 1;);
| ^^^^^^^^^^^^^^^^^^

error: use of irregular braces for `quote::quote!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:40:13
--> $DIR/conf_nonstandard_macro_braces.rs:46:13
|
LL | let _ = quote::quote!(match match match);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider writing `quote::quote! {match match match}`
--> $DIR/conf_nonstandard_macro_braces.rs:40:13
--> $DIR/conf_nonstandard_macro_braces.rs:46:13
|
LL | let _ = quote::quote!(match match match);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -53,7 +53,7 @@ error: use of irregular braces for `vec!` macro
LL | vec!{0, 0, 0}
| ^^^^^^^^^^^^^
...
LL | let _ = test!();
LL | let _ = test!(); // trigger when macro def is inside our own crate
| ------- in this macro invocation
|
help: consider writing `vec![0, 0, 0]`
Expand All @@ -62,30 +62,30 @@ help: consider writing `vec![0, 0, 0]`
LL | vec!{0, 0, 0}
| ^^^^^^^^^^^^^
...
LL | let _ = test!();
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:49:12
--> $DIR/conf_nonstandard_macro_braces.rs:55:12
|
LL | let _: type_pos!(usize) = vec![];
| ^^^^^^^^^^^^^^^^
|
help: consider writing `type_pos![usize]`
--> $DIR/conf_nonstandard_macro_braces.rs:49:12
--> $DIR/conf_nonstandard_macro_braces.rs:55:12
|
LL | let _: type_pos!(usize) = vec![];
| ^^^^^^^^^^^^^^^^

error: use of irregular braces for `eprint!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:51:5
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
|
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider writing `eprint!["test if user config overrides defaults"];`
--> $DIR/conf_nonstandard_macro_braces.rs:51:5
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
|
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down