Skip to content

Commit

Permalink
Add type position macros impl check_ty
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Jun 10, 2021
1 parent d203656 commit 987ce04
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
25 changes: 18 additions & 7 deletions clippy_lints/src/nonstandard_macro_braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,33 @@ impl_lint_pass!(MacroBraces => [NONSTANDARD_MACRO_BRACES]);
impl EarlyLintPass for MacroBraces {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
if let Some((name, braces, snip)) = is_offending_macro(cx, item.span, self) {
emit_help(cx, snip, braces, name, item.span);
self.done.insert(item.span.ctxt().outer_expn_data().call_site);
let span = item.span.ctxt().outer_expn_data().call_site;
emit_help(cx, snip, braces, name, span);
self.done.insert(span);
}
}

fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &ast::Stmt) {
if let Some((name, braces, snip)) = is_offending_macro(cx, stmt.span, self) {
emit_help(cx, snip, braces, name, stmt.span);
self.done.insert(stmt.span.ctxt().outer_expn_data().call_site);
let span = stmt.span.ctxt().outer_expn_data().call_site;
emit_help(cx, snip, braces, name, span);
self.done.insert(span);
}
}

fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
if let Some((name, braces, snip)) = is_offending_macro(cx, expr.span, self) {
emit_help(cx, snip, braces, name, expr.span);
self.done.insert(expr.span.ctxt().outer_expn_data().call_site);
let span = expr.span.ctxt().outer_expn_data().call_site;
emit_help(cx, snip, braces, name, span);
self.done.insert(span);
}
}

fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
if let Some((name, braces, snip)) = is_offending_macro(cx, ty.span, self) {
let span = ty.span.ctxt().outer_expn_data().call_site;
emit_help(cx, snip, braces, name, span);
self.done.insert(span);
}
}
}
Expand Down Expand Up @@ -115,7 +126,7 @@ fn emit_help(cx: &EarlyContext<'_>, snip: String, braces: &(String, String), nam
NONSTANDARD_MACRO_BRACES,
span,
&format!("use of irregular braces for `{}!` macro", name),
Some(span.ctxt().outer_expn_data().call_site),
Some(span),
&format!("consider writing `{}`", help),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/nonstandard_macro_braces/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ standard-macro-braces = [
{ name = "quote", brace = "{" },
{ name = "quote::quote", brace = "{" },
{ name = "eprint", brace = "[" },
{ name = "type_pos", brace = "[" },
]
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ macro_rules! test2 {
};
}

macro_rules! type_pos {
($what:ty) => {
Vec<$what>
};
}

#[rustfmt::skip]
fn main() {
let _ = vec! {1, 2, 3};
Expand All @@ -32,5 +38,7 @@ fn main() {
let _ = format!("fds{}fds", 10);
let _ = test2!["{}{}{}", 1, 2, 3];

let _: type_pos!(usize) = vec![];

eprint!("test if user config overrides defaults");
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
error: use of irregular braces for `vec!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:23:13
--> $DIR/conf_nonstandard_macro_braces.rs:29: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:23:13
--> $DIR/conf_nonstandard_macro_braces.rs:29:13
|
LL | let _ = vec! {1, 2, 3};
| ^^^^^^^^^^^^^^
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: use of irregular braces for `format!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:24:13
--> $DIR/conf_nonstandard_macro_braces.rs:30: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:24:13
--> $DIR/conf_nonstandard_macro_braces.rs:30:13
|
LL | let _ = format!["ugh {} stop being such a good compiler", "hello"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: use of irregular braces for `quote!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:25:13
--> $DIR/conf_nonstandard_macro_braces.rs:31:13
|
LL | let _ = quote!(let x = 1;);
| ^^^^^^^^^^^^^^^^^^
|
help: consider writing `quote! {let x = 1;}`
--> $DIR/conf_nonstandard_macro_braces.rs:25:13
--> $DIR/conf_nonstandard_macro_braces.rs:31:13
|
LL | let _ = quote!(let x = 1;);
| ^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `quote` (in Nightly builds, run with -Z macro-backtrace for more info)

error: use of irregular braces for `quote::quote!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:26:13
--> $DIR/conf_nonstandard_macro_braces.rs:32:13
|
LL | let _ = quote::quote!(match match match);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider writing `quote::quote! {match match match}`
--> $DIR/conf_nonstandard_macro_braces.rs:26:13
--> $DIR/conf_nonstandard_macro_braces.rs:32:13
|
LL | let _ = quote::quote!(match match match);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `quote::quote` (in Nightly builds, run with -Z macro-backtrace for more info)

error: use of irregular braces for `vec!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:27:13
--> $DIR/conf_nonstandard_macro_braces.rs:10: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:10:9
Expand All @@ -65,20 +64,31 @@ LL | vec!{0, 0, 0}
...
LL | let _ = test!();
| ------- in this macro invocation
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
= 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:41:12
|
LL | let _: type_pos!(usize) = vec![];
| ^^^^^^^^^^^^^^^^
|
help: consider writing `type_pos![usize]`
--> $DIR/conf_nonstandard_macro_braces.rs:41:12
|
LL | let _: type_pos!(usize) = vec![];
| ^^^^^^^^^^^^^^^^

error: use of irregular braces for `eprint!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:35:5
--> $DIR/conf_nonstandard_macro_braces.rs:43: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:35:5
--> $DIR/conf_nonstandard_macro_braces.rs:43:5
|
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `eprint` (in Nightly builds, run with -Z macro-backtrace for more info)

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

0 comments on commit 987ce04

Please sign in to comment.