1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::macros::macro_backtrace;
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_hir::def_id::DefIdMap;
5 use rustc_hir::{Expr, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_tool_lint, impl_lint_pass};
8 use rustc_span::{ExpnId, Span};
10 use crate::utils::conf;
12 declare_clippy_lint! {
14 /// Denies the configured macros in clippy.toml
16 /// Note: Even though this lint is warn-by-default, it will only trigger if
17 /// macros are defined in the clippy.toml file.
19 /// ### Why is this bad?
20 /// Some macros are undesirable in certain contexts, and it's beneficial to
21 /// lint for them as needed.
24 /// An example clippy.toml configuration:
27 /// disallowed-macros = [
28 /// # Can use a string as the path of the disallowed macro.
30 /// # Can also use an inline table with a `path` key.
31 /// { path = "std::println" },
32 /// # When using an inline table, can add a `reason` for why the macro
34 /// { path = "serde::Serialize", reason = "no serializing" },
38 /// use serde::Serialize;
40 /// // Example code where clippy issues a warning
41 /// println!("warns");
43 /// // The diagnostic will contain the message "no serializing"
44 /// #[derive(Serialize)]
50 #[clippy::version = "1.65.0"]
51 pub DISALLOWED_MACROS,
53 "use of a disallowed macro"
56 pub struct DisallowedMacros {
57 conf_disallowed: Vec<conf::DisallowedPath>,
58 disallowed: DefIdMap<usize>,
59 seen: FxHashSet<ExpnId>,
62 impl DisallowedMacros {
63 pub fn new(conf_disallowed: Vec<conf::DisallowedPath>) -> Self {
66 disallowed: DefIdMap::default(),
67 seen: FxHashSet::default(),
71 fn check(&mut self, cx: &LateContext<'_>, span: Span) {
72 if self.conf_disallowed.is_empty() {
76 for mac in macro_backtrace(span) {
77 if !self.seen.insert(mac.expn) {
81 if let Some(&index) = self.disallowed.get(&mac.def_id) {
82 let conf = &self.conf_disallowed[index];
88 &format!("use of a disallowed macro `{}`", conf.path()),
90 if let Some(reason) = conf.reason() {
100 impl_lint_pass!(DisallowedMacros => [DISALLOWED_MACROS]);
102 impl LateLintPass<'_> for DisallowedMacros {
103 fn check_crate(&mut self, cx: &LateContext<'_>) {
104 for (index, conf) in self.conf_disallowed.iter().enumerate() {
105 let segs: Vec<_> = conf.path().split("::").collect();
106 for id in clippy_utils::def_path_def_ids(cx, &segs) {
107 self.disallowed.insert(id, index);
112 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
113 self.check(cx, expr.span);
116 fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
117 self.check(cx, stmt.span);
120 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &Ty<'_>) {
121 self.check(cx, ty.span);
124 fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
125 self.check(cx, pat.span);
128 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
129 self.check(cx, item.span);
130 self.check(cx, item.vis_span);
133 fn check_foreign_item(&mut self, cx: &LateContext<'_>, item: &ForeignItem<'_>) {
134 self.check(cx, item.span);
135 self.check(cx, item.vis_span);
138 fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &ImplItem<'_>) {
139 self.check(cx, item.span);
140 self.check(cx, item.vis_span);
143 fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
144 self.check(cx, item.span);
147 fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
148 self.check(cx, path.span);