Auto merge of #11439 - Alexendoo:disallowed-macros-bins-attrs, r=xFrednet

Check binary operators and attributes in disallowed_macros

changelog: none
This commit is contained in:
bors 2023-09-06 11:36:28 +00:00
commit 0d36d57c41
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 @@ impl LateLintPass<'_> for DisallowedMacros {
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 @@ impl LateLintPass<'_> for DisallowedMacros {
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