]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/disallowed_method.rs
Documented constant expression evaluation for `repeat_once`
[rust.git] / clippy_lints / src / disallowed_method.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::fn_def_id;
3
4 use rustc_data_structures::fx::FxHashSet;
5 use rustc_hir::{def::Res, def_id::DefId, Crate, Expr};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_tool_lint, impl_lint_pass};
8 use rustc_span::Symbol;
9
10 declare_clippy_lint! {
11     /// **What it does:** Denies the configured methods and functions in clippy.toml
12     ///
13     /// **Why is this bad?** Some methods are undesirable in certain contexts,
14     /// and it's beneficial to lint for them as needed.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     ///
20     /// An example clippy.toml configuration:
21     /// ```toml
22     /// # clippy.toml
23     /// disallowed-methods = ["std::vec::Vec::leak", "std::time::Instant::now"]
24     /// ```
25     ///
26     /// ```rust,ignore
27     /// // Example code where clippy issues a warning
28     /// let xs = vec![1, 2, 3, 4];
29     /// xs.leak(); // Vec::leak is disallowed in the config.
30     ///
31     /// let _now = Instant::now(); // Instant::now is disallowed in the config.
32     /// ```
33     ///
34     /// Use instead:
35     /// ```rust,ignore
36     /// // Example code which does not raise clippy warning
37     /// let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config.
38     /// xs.push(123); // Vec::push is _not_ disallowed in the config.
39     /// ```
40     pub DISALLOWED_METHOD,
41     nursery,
42     "use of a disallowed method call"
43 }
44
45 #[derive(Clone, Debug)]
46 pub struct DisallowedMethod {
47     disallowed: FxHashSet<Vec<Symbol>>,
48     def_ids: FxHashSet<(DefId, Vec<Symbol>)>,
49 }
50
51 impl DisallowedMethod {
52     pub fn new(disallowed: &FxHashSet<String>) -> Self {
53         Self {
54             disallowed: disallowed
55                 .iter()
56                 .map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
57                 .collect(),
58             def_ids: FxHashSet::default(),
59         }
60     }
61 }
62
63 impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
64
65 impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
66     fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
67         for path in &self.disallowed {
68             let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
69             if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>())
70             {
71                 self.def_ids.insert((id, path.clone()));
72             }
73         }
74     }
75
76     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
77         if let Some(def_id) = fn_def_id(cx, expr) {
78             if self.def_ids.iter().any(|(id, _)| def_id == *id) {
79                 let func_path = cx.get_def_path(def_id);
80                 let func_path_string = func_path
81                     .into_iter()
82                     .map(Symbol::to_ident_string)
83                     .collect::<Vec<_>>()
84                     .join("::");
85
86                 span_lint(
87                     cx,
88                     DISALLOWED_METHOD,
89                     expr.span,
90                     &format!("use of a disallowed method `{}`", func_path_string),
91                 );
92             }
93         }
94     }
95 }