Skip to content

Commit f998e89

Browse files
committed
Auto merge of #7478 - DevinR528:preemtive, r=llogiq
Fix nonstandard_macro_braces FP and docs of disallowed_types changelog: Fix FP in [`nonstandard_macro_braces`] lint
2 parents 87ce09d + bc7fac9 commit f998e89

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

clippy_lints/src/nonstandard_macro_braces.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clippy_utils::{diagnostics::span_lint_and_help, in_macro, is_direct_expn_of,
77
use if_chain::if_chain;
88
use rustc_ast::ast;
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
10+
use rustc_hir::def_id::DefId;
1011
use rustc_lint::{EarlyContext, EarlyLintPass};
1112
use rustc_session::{declare_tool_lint, impl_lint_pass};
1213
use rustc_span::Span;
@@ -91,13 +92,23 @@ impl EarlyLintPass for MacroBraces {
9192
}
9293

9394
fn is_offending_macro<'a>(cx: &EarlyContext<'_>, span: Span, mac_braces: &'a MacroBraces) -> Option<MacroInfo<'a>> {
95+
let unnested_or_local = || {
96+
let nested = in_macro(span.ctxt().outer_expn_data().call_site);
97+
!nested
98+
|| span
99+
.macro_backtrace()
100+
.last()
101+
.map_or(false, |e| e.macro_def_id.map_or(false, DefId::is_local))
102+
};
94103
if_chain! {
104+
// Make sure we are only one level deep otherwise there are to many FP's
95105
if in_macro(span);
96106
if let Some((name, braces)) = find_matching_macro(span, &mac_braces.macro_braces);
97107
if let Some(snip) = snippet_opt(cx, span.ctxt().outer_expn_data().call_site);
98108
// we must check only invocation sites
99109
// https://github.com/rust-lang/rust-clippy/issues/7422
100-
if snip.starts_with(name);
110+
if snip.starts_with(&format!("{}!", name));
111+
if unnested_or_local();
101112
// make formatting consistent
102113
let c = snip.replace(" ", "");
103114
if !c.starts_with(&format!("{}!{}", name, braces.0));

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ macro_rules! type_pos {
3232
};
3333
}
3434

35+
macro_rules! printlnfoo {
36+
($thing:expr) => {
37+
println!("{}", $thing)
38+
};
39+
}
40+
3541
#[rustfmt::skip]
3642
fn main() {
3743
let _ = vec! {1, 2, 3};
3844
let _ = format!["ugh {} stop being such a good compiler", "hello"];
3945
let _ = quote!(let x = 1;);
4046
let _ = quote::quote!(match match match);
41-
let _ = test!();
47+
let _ = test!(); // trigger when macro def is inside our own crate
4248
let _ = vec![1,2,3];
4349

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

5157
eprint!("test if user config overrides defaults");
58+
59+
printlnfoo!["test if printlnfoo is triggered by println"];
5260
}

tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
error: use of irregular braces for `vec!` macro
2-
--> $DIR/conf_nonstandard_macro_braces.rs:37:13
2+
--> $DIR/conf_nonstandard_macro_braces.rs:43:13
33
|
44
LL | let _ = vec! {1, 2, 3};
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::nonstandard-macro-braces` implied by `-D warnings`
88
help: consider writing `vec![1, 2, 3]`
9-
--> $DIR/conf_nonstandard_macro_braces.rs:37:13
9+
--> $DIR/conf_nonstandard_macro_braces.rs:43:13
1010
|
1111
LL | let _ = vec! {1, 2, 3};
1212
| ^^^^^^^^^^^^^^
1313

1414
error: use of irregular braces for `format!` macro
15-
--> $DIR/conf_nonstandard_macro_braces.rs:38:13
15+
--> $DIR/conf_nonstandard_macro_braces.rs:44:13
1616
|
1717
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
2020
help: consider writing `format!("ugh () stop being such a good compiler", "hello")`
21-
--> $DIR/conf_nonstandard_macro_braces.rs:38:13
21+
--> $DIR/conf_nonstandard_macro_braces.rs:44:13
2222
|
2323
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525

2626
error: use of irregular braces for `quote!` macro
27-
--> $DIR/conf_nonstandard_macro_braces.rs:39:13
27+
--> $DIR/conf_nonstandard_macro_braces.rs:45:13
2828
|
2929
LL | let _ = quote!(let x = 1;);
3030
| ^^^^^^^^^^^^^^^^^^
3131
|
3232
help: consider writing `quote! {let x = 1;}`
33-
--> $DIR/conf_nonstandard_macro_braces.rs:39:13
33+
--> $DIR/conf_nonstandard_macro_braces.rs:45:13
3434
|
3535
LL | let _ = quote!(let x = 1;);
3636
| ^^^^^^^^^^^^^^^^^^
3737

3838
error: use of irregular braces for `quote::quote!` macro
39-
--> $DIR/conf_nonstandard_macro_braces.rs:40:13
39+
--> $DIR/conf_nonstandard_macro_braces.rs:46:13
4040
|
4141
LL | let _ = quote::quote!(match match match);
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4343
|
4444
help: consider writing `quote::quote! {match match match}`
45-
--> $DIR/conf_nonstandard_macro_braces.rs:40:13
45+
--> $DIR/conf_nonstandard_macro_braces.rs:46:13
4646
|
4747
LL | let _ = quote::quote!(match match match);
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -53,7 +53,7 @@ error: use of irregular braces for `vec!` macro
5353
LL | vec!{0, 0, 0}
5454
| ^^^^^^^^^^^^^
5555
...
56-
LL | let _ = test!();
56+
LL | let _ = test!(); // trigger when macro def is inside our own crate
5757
| ------- in this macro invocation
5858
|
5959
help: consider writing `vec![0, 0, 0]`
@@ -62,30 +62,30 @@ help: consider writing `vec![0, 0, 0]`
6262
LL | vec!{0, 0, 0}
6363
| ^^^^^^^^^^^^^
6464
...
65-
LL | let _ = test!();
65+
LL | let _ = test!(); // trigger when macro def is inside our own crate
6666
| ------- in this macro invocation
6767
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
6868

6969
error: use of irregular braces for `type_pos!` macro
70-
--> $DIR/conf_nonstandard_macro_braces.rs:49:12
70+
--> $DIR/conf_nonstandard_macro_braces.rs:55:12
7171
|
7272
LL | let _: type_pos!(usize) = vec![];
7373
| ^^^^^^^^^^^^^^^^
7474
|
7575
help: consider writing `type_pos![usize]`
76-
--> $DIR/conf_nonstandard_macro_braces.rs:49:12
76+
--> $DIR/conf_nonstandard_macro_braces.rs:55:12
7777
|
7878
LL | let _: type_pos!(usize) = vec![];
7979
| ^^^^^^^^^^^^^^^^
8080

8181
error: use of irregular braces for `eprint!` macro
82-
--> $DIR/conf_nonstandard_macro_braces.rs:51:5
82+
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
8383
|
8484
LL | eprint!("test if user config overrides defaults");
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8686
|
8787
help: consider writing `eprint!["test if user config overrides defaults"];`
88-
--> $DIR/conf_nonstandard_macro_braces.rs:51:5
88+
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
8989
|
9090
LL | eprint!("test if user config overrides defaults");
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)