Skip to content

Commit 377cbef

Browse files
committed
Check binary operators and attributes in disallowed_macros
1 parent c50d86f commit 377cbef

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

clippy_lints/src/disallowed_macros.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::macro_backtrace;
3+
use rustc_ast::Attribute;
34
use rustc_data_structures::fx::FxHashSet;
45
use rustc_hir::def_id::DefIdMap;
5-
use rustc_hir::{Expr, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty};
6+
use rustc_hir::{Expr, ExprKind, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_session::{declare_tool_lint, impl_lint_pass};
89
use rustc_span::{ExpnId, Span};
@@ -111,6 +112,10 @@ impl LateLintPass<'_> for DisallowedMacros {
111112

112113
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
113114
self.check(cx, expr.span);
115+
// `$t + $t` can have the context of $t, check also the span of the binary operator
116+
if let ExprKind::Binary(op, ..) = expr.kind {
117+
self.check(cx, op.span)
118+
}
114119
}
115120

116121
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
@@ -147,4 +152,8 @@ impl LateLintPass<'_> for DisallowedMacros {
147152
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
148153
self.check(cx, path.span);
149154
}
155+
156+
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &Attribute) {
157+
self.check(cx, attr.span);
158+
}
150159
}

tests/ui-toml/disallowed_macros/auxiliary/macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ macro_rules! item {
3030
const ITEM: usize = 1;
3131
};
3232
}
33+
34+
#[macro_export]
35+
macro_rules! binop {
36+
($t:tt) => {
37+
$t + $t
38+
};
39+
}
40+
41+
#[macro_export]
42+
macro_rules! attr {
43+
($i:item) => {
44+
#[repr(C)]
45+
$i
46+
};
47+
}

tests/ui-toml/disallowed_macros/clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ disallowed-macros = [
88
"macros::ty",
99
"macros::pat",
1010
"macros::item",
11+
"macros::binop",
12+
"macros::attr",
1113
]

tests/ui-toml/disallowed_macros/disallowed_macros.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ fn main() {
2020
let macros::pat!() = 1;
2121
let _: macros::ty!() = "";
2222
macros::item!();
23+
let _ = macros::binop!(1);
2324

2425
eprintln!("allowed");
2526
}
2627

27-
struct S;
28+
macros::attr! {
29+
struct S;
30+
}
2831

2932
impl S {
3033
macros::item!();

tests/ui-toml/disallowed_macros/disallowed_macros.stderr

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,37 @@ error: use of a disallowed macro `macros::item`
6262
LL | macros::item!();
6363
| ^^^^^^^^^^^^^^^
6464

65+
error: use of a disallowed macro `macros::binop`
66+
--> $DIR/disallowed_macros.rs:23:13
67+
|
68+
LL | let _ = macros::binop!(1);
69+
| ^^^^^^^^^^^^^^^^^
70+
6571
error: use of a disallowed macro `macros::item`
66-
--> $DIR/disallowed_macros.rs:30:5
72+
--> $DIR/disallowed_macros.rs:33:5
6773
|
6874
LL | macros::item!();
6975
| ^^^^^^^^^^^^^^^
7076

7177
error: use of a disallowed macro `macros::item`
72-
--> $DIR/disallowed_macros.rs:34:5
78+
--> $DIR/disallowed_macros.rs:37:5
7379
|
7480
LL | macros::item!();
7581
| ^^^^^^^^^^^^^^^
7682

7783
error: use of a disallowed macro `macros::item`
78-
--> $DIR/disallowed_macros.rs:38:5
84+
--> $DIR/disallowed_macros.rs:41:5
7985
|
8086
LL | macros::item!();
8187
| ^^^^^^^^^^^^^^^
8288

83-
error: aborting due to 13 previous errors
89+
error: use of a disallowed macro `macros::attr`
90+
--> $DIR/disallowed_macros.rs:28:1
91+
|
92+
LL | / macros::attr! {
93+
LL | | struct S;
94+
LL | | }
95+
| |_^
96+
97+
error: aborting due to 15 previous errors
8498

0 commit comments

Comments
 (0)