1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::{fn_def_id, get_parent_expr, path_def_id};
4 use rustc_hir::def_id::DefIdMap;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use crate::utils::conf;
11 declare_clippy_lint! {
13 /// Denies the configured methods and functions in clippy.toml
15 /// Note: Even though this lint is warn-by-default, it will only trigger if
16 /// methods are defined in the clippy.toml file.
18 /// ### Why is this bad?
19 /// Some methods are undesirable in certain contexts, and it's beneficial to
20 /// lint for them as needed.
23 /// An example clippy.toml configuration:
26 /// disallowed-methods = [
27 /// # Can use a string as the path of the disallowed method.
28 /// "std::boxed::Box::new",
29 /// # Can also use an inline table with a `path` key.
30 /// { path = "std::time::Instant::now" },
31 /// # When using an inline table, can add a `reason` for why the method
33 /// { path = "std::vec::Vec::leak", reason = "no leaking memory" },
38 /// // Example code where clippy issues a warning
39 /// let xs = vec![1, 2, 3, 4];
40 /// xs.leak(); // Vec::leak is disallowed in the config.
41 /// // The diagnostic contains the message "no leaking memory".
43 /// let _now = Instant::now(); // Instant::now is disallowed in the config.
45 /// let _box = Box::new(3); // Box::new is disallowed in the config.
50 /// // Example code which does not raise clippy warning
51 /// let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config.
52 /// xs.push(123); // Vec::push is _not_ disallowed in the config.
54 #[clippy::version = "1.49.0"]
55 pub DISALLOWED_METHODS,
57 "use of a disallowed method call"
60 #[derive(Clone, Debug)]
61 pub struct DisallowedMethods {
62 conf_disallowed: Vec<conf::DisallowedPath>,
63 disallowed: DefIdMap<usize>,
66 impl DisallowedMethods {
67 pub fn new(conf_disallowed: Vec<conf::DisallowedPath>) -> Self {
70 disallowed: DefIdMap::default(),
75 impl_lint_pass!(DisallowedMethods => [DISALLOWED_METHODS]);
77 impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
78 fn check_crate(&mut self, cx: &LateContext<'_>) {
79 for (index, conf) in self.conf_disallowed.iter().enumerate() {
80 let segs: Vec<_> = conf.path().split("::").collect();
81 for id in clippy_utils::def_path_def_ids(cx, &segs) {
82 self.disallowed.insert(id, index);
87 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
88 let uncalled_path = if let Some(parent) = get_parent_expr(cx, expr)
89 && let ExprKind::Call(receiver, _) = parent.kind
90 && receiver.hir_id == expr.hir_id
96 let Some(def_id) = uncalled_path.or_else(|| fn_def_id(cx, expr)) else {
99 let conf = match self.disallowed.get(&def_id) {
100 Some(&index) => &self.conf_disallowed[index],
103 let msg = format!("use of a disallowed method `{}`", conf.path());
104 span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, &msg, |diag| {
105 if let Some(reason) = conf.reason() {