Check binary operators and attributes in disallowed_macros

This commit is contained in:
Alex Macleod 2023-08-31 12:52:55 +00:00
parent c50d86fc6a
commit 299fbceb96
5 changed files with 49 additions and 6 deletions

View File

@ -1,8 +1,9 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::macro_backtrace; use clippy_utils::macros::macro_backtrace;
use rustc_ast::Attribute;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::DefIdMap; use rustc_hir::def_id::DefIdMap;
use rustc_hir::{Expr, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty}; use rustc_hir::{Expr, ExprKind, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{ExpnId, Span}; use rustc_span::{ExpnId, Span};
@ -111,6 +112,10 @@ fn check_crate(&mut self, cx: &LateContext<'_>) {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
self.check(cx, expr.span); self.check(cx, expr.span);
// `$t + $t` can have the context of $t, check also the span of the binary operator
if let ExprKind::Binary(op, ..) = expr.kind {
self.check(cx, op.span);
}
} }
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
@ -147,4 +152,8 @@ fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) { fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
self.check(cx, path.span); self.check(cx, path.span);
} }
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &Attribute) {
self.check(cx, attr.span);
}
} }

View File

@ -30,3 +30,18 @@ macro_rules! item {
const ITEM: usize = 1; const ITEM: usize = 1;
}; };
} }
#[macro_export]
macro_rules! binop {
($t:tt) => {
$t + $t
};
}
#[macro_export]
macro_rules! attr {
($i:item) => {
#[repr(C)]
$i
};
}

View File

@ -8,4 +8,6 @@ disallowed-macros = [
"macros::ty", "macros::ty",
"macros::pat", "macros::pat",
"macros::item", "macros::item",
"macros::binop",
"macros::attr",
] ]

View File

@ -20,11 +20,14 @@ fn main() {
let macros::pat!() = 1; let macros::pat!() = 1;
let _: macros::ty!() = ""; let _: macros::ty!() = "";
macros::item!(); macros::item!();
let _ = macros::binop!(1);
eprintln!("allowed"); eprintln!("allowed");
} }
struct S; macros::attr! {
struct S;
}
impl S { impl S {
macros::item!(); macros::item!();

View File

@ -62,23 +62,37 @@ error: use of a disallowed macro `macros::item`
LL | macros::item!(); LL | macros::item!();
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: use of a disallowed macro `macros::binop`
--> $DIR/disallowed_macros.rs:23:13
|
LL | let _ = macros::binop!(1);
| ^^^^^^^^^^^^^^^^^
error: use of a disallowed macro `macros::item` error: use of a disallowed macro `macros::item`
--> $DIR/disallowed_macros.rs:30:5 --> $DIR/disallowed_macros.rs:33:5
| |
LL | macros::item!(); LL | macros::item!();
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: use of a disallowed macro `macros::item` error: use of a disallowed macro `macros::item`
--> $DIR/disallowed_macros.rs:34:5 --> $DIR/disallowed_macros.rs:37:5
| |
LL | macros::item!(); LL | macros::item!();
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: use of a disallowed macro `macros::item` error: use of a disallowed macro `macros::item`
--> $DIR/disallowed_macros.rs:38:5 --> $DIR/disallowed_macros.rs:41:5
| |
LL | macros::item!(); LL | macros::item!();
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: aborting due to 13 previous errors error: use of a disallowed macro `macros::attr`
--> $DIR/disallowed_macros.rs:28:1
|
LL | / macros::attr! {
LL | | struct S;
LL | | }
| |_^
error: aborting due to 15 previous errors