Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check binary operators and attributes in disallowed_macros #11439

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clippy_lints/src/disallowed_macros.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::macro_backtrace;
use rustc_ast::Attribute;
use rustc_data_structures::fx::FxHashSet;
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_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{ExpnId, Span};
Expand Down Expand Up @@ -111,6 +112,10 @@ impl LateLintPass<'_> for DisallowedMacros {

fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
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<'_>) {
Expand Down Expand Up @@ -147,4 +152,8 @@ impl LateLintPass<'_> for DisallowedMacros {
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
self.check(cx, path.span);
}

fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &Attribute) {
self.check(cx, attr.span);
}
}
15 changes: 15 additions & 0 deletions tests/ui-toml/disallowed_macros/auxiliary/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ macro_rules! item {
const ITEM: usize = 1;
};
}

#[macro_export]
macro_rules! binop {
($t:tt) => {
$t + $t
};
}

#[macro_export]
macro_rules! attr {
($i:item) => {
#[repr(C)]
$i
};
}
2 changes: 2 additions & 0 deletions tests/ui-toml/disallowed_macros/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ disallowed-macros = [
"macros::ty",
"macros::pat",
"macros::item",
"macros::binop",
"macros::attr",
]
5 changes: 4 additions & 1 deletion tests/ui-toml/disallowed_macros/disallowed_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ fn main() {
let macros::pat!() = 1;
let _: macros::ty!() = "";
macros::item!();
let _ = macros::binop!(1);

eprintln!("allowed");
}

struct S;
macros::attr! {
struct S;
}

impl S {
macros::item!();
Expand Down
22 changes: 18 additions & 4 deletions tests/ui-toml/disallowed_macros/disallowed_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,37 @@ error: use of a disallowed macro `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`
--> $DIR/disallowed_macros.rs:30:5
--> $DIR/disallowed_macros.rs:33:5
|
LL | 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!();
| ^^^^^^^^^^^^^^^

error: use of a disallowed macro `macros::item`
--> $DIR/disallowed_macros.rs:38:5
--> $DIR/disallowed_macros.rs:41:5
|
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