]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/disallowed_macros.rs
Rollup merge of #105602 - RalfJung:read-convenience, r=oli-obk
[rust.git] / src / tools / clippy / clippy_lints / src / disallowed_macros.rs
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};
9
10 use crate::utils::conf;
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// Denies the configured macros in clippy.toml
15     ///
16     /// Note: Even though this lint is warn-by-default, it will only trigger if
17     /// macros are defined in the clippy.toml file.
18     ///
19     /// ### Why is this bad?
20     /// Some macros are undesirable in certain contexts, and it's beneficial to
21     /// lint for them as needed.
22     ///
23     /// ### Example
24     /// An example clippy.toml configuration:
25     /// ```toml
26     /// # clippy.toml
27     /// disallowed-macros = [
28     ///     # Can use a string as the path of the disallowed macro.
29     ///     "std::print",
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
33     ///     # is disallowed.
34     ///     { path = "serde::Serialize", reason = "no serializing" },
35     /// ]
36     /// ```
37     /// ```
38     /// use serde::Serialize;
39     ///
40     /// // Example code where clippy issues a warning
41     /// println!("warns");
42     ///
43     /// // The diagnostic will contain the message "no serializing"
44     /// #[derive(Serialize)]
45     /// struct Data {
46     ///     name: String,
47     ///     value: usize,
48     /// }
49     /// ```
50     #[clippy::version = "1.66.0"]
51     pub DISALLOWED_MACROS,
52     style,
53     "use of a disallowed macro"
54 }
55
56 pub struct DisallowedMacros {
57     conf_disallowed: Vec<conf::DisallowedPath>,
58     disallowed: DefIdMap<usize>,
59     seen: FxHashSet<ExpnId>,
60 }
61
62 impl DisallowedMacros {
63     pub fn new(conf_disallowed: Vec<conf::DisallowedPath>) -> Self {
64         Self {
65             conf_disallowed,
66             disallowed: DefIdMap::default(),
67             seen: FxHashSet::default(),
68         }
69     }
70
71     fn check(&mut self, cx: &LateContext<'_>, span: Span) {
72         if self.conf_disallowed.is_empty() {
73             return;
74         }
75
76         for mac in macro_backtrace(span) {
77             if !self.seen.insert(mac.expn) {
78                 return;
79             }
80
81             if let Some(&index) = self.disallowed.get(&mac.def_id) {
82                 let conf = &self.conf_disallowed[index];
83
84                 span_lint_and_then(
85                     cx,
86                     DISALLOWED_MACROS,
87                     mac.span,
88                     &format!("use of a disallowed macro `{}`", conf.path()),
89                     |diag| {
90                         if let Some(reason) = conf.reason() {
91                             diag.note(reason);
92                         }
93                     },
94                 );
95             }
96         }
97     }
98 }
99
100 impl_lint_pass!(DisallowedMacros => [DISALLOWED_MACROS]);
101
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);
108             }
109         }
110     }
111
112     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
113         self.check(cx, expr.span);
114     }
115
116     fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
117         self.check(cx, stmt.span);
118     }
119
120     fn check_ty(&mut self, cx: &LateContext<'_>, ty: &Ty<'_>) {
121         self.check(cx, ty.span);
122     }
123
124     fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
125         self.check(cx, pat.span);
126     }
127
128     fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
129         self.check(cx, item.span);
130         self.check(cx, item.vis_span);
131     }
132
133     fn check_foreign_item(&mut self, cx: &LateContext<'_>, item: &ForeignItem<'_>) {
134         self.check(cx, item.span);
135         self.check(cx, item.vis_span);
136     }
137
138     fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &ImplItem<'_>) {
139         self.check(cx, item.span);
140         self.check(cx, item.vis_span);
141     }
142
143     fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
144         self.check(cx, item.span);
145     }
146
147     fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
148         self.check(cx, path.span);
149     }
150 }