2023-10-19 14:59:44 +00:00
|
|
|
use clippy_config::types::DisallowedPath;
|
2021-09-28 17:03:12 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2022-05-21 11:24:00 +00:00
|
|
|
use clippy_utils::{fn_def_id, get_parent_expr, path_def_id};
|
2022-10-06 07:44:38 +00:00
|
|
|
use rustc_hir::def_id::DefIdMap;
|
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-10-09 10:45:29 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::impl_lint_pass;
|
2021-09-28 17:03:12 +00:00
|
|
|
|
2020-10-09 10:45:29 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Denies the configured methods and functions in clippy.toml
|
2020-10-09 10:45:29 +00:00
|
|
|
///
|
2022-01-13 12:18:19 +00:00
|
|
|
/// Note: Even though this lint is warn-by-default, it will only trigger if
|
|
|
|
/// methods are defined in the clippy.toml file.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
2021-09-28 17:03:12 +00:00
|
|
|
/// Some methods are undesirable in certain contexts, and it's beneficial to
|
|
|
|
/// lint for them as needed.
|
2020-10-09 10:45:29 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-02-11 14:04:38 +00:00
|
|
|
/// An example clippy.toml configuration:
|
|
|
|
/// ```toml
|
|
|
|
/// # clippy.toml
|
2021-09-28 17:03:12 +00:00
|
|
|
/// disallowed-methods = [
|
|
|
|
/// # Can use a string as the path of the disallowed method.
|
|
|
|
/// "std::boxed::Box::new",
|
|
|
|
/// # Can also use an inline table with a `path` key.
|
|
|
|
/// { path = "std::time::Instant::now" },
|
|
|
|
/// # When using an inline table, can add a `reason` for why the method
|
|
|
|
/// # is disallowed.
|
|
|
|
/// { path = "std::vec::Vec::leak", reason = "no leaking memory" },
|
|
|
|
/// ]
|
2021-02-11 14:04:38 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2020-10-09 10:45:29 +00:00
|
|
|
/// ```rust,ignore
|
2021-02-11 14:04:38 +00:00
|
|
|
/// // Example code where clippy issues a warning
|
|
|
|
/// let xs = vec![1, 2, 3, 4];
|
|
|
|
/// xs.leak(); // Vec::leak is disallowed in the config.
|
2021-09-28 17:03:12 +00:00
|
|
|
/// // The diagnostic contains the message "no leaking memory".
|
2021-02-11 14:04:38 +00:00
|
|
|
///
|
|
|
|
/// let _now = Instant::now(); // Instant::now is disallowed in the config.
|
2021-09-28 17:03:12 +00:00
|
|
|
///
|
|
|
|
/// let _box = Box::new(3); // Box::new is disallowed in the config.
|
2020-10-09 10:45:29 +00:00
|
|
|
/// ```
|
2021-02-11 14:04:38 +00:00
|
|
|
///
|
2020-10-09 10:45:29 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust,ignore
|
2021-02-11 14:04:38 +00:00
|
|
|
/// // Example code which does not raise clippy warning
|
|
|
|
/// let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config.
|
|
|
|
/// xs.push(123); // Vec::push is _not_ disallowed in the config.
|
2020-10-09 10:45:29 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.49.0"]
|
|
|
|
pub DISALLOWED_METHODS,
|
2022-01-13 12:18:19 +00:00
|
|
|
style,
|
2020-10-09 10:45:29 +00:00
|
|
|
"use of a disallowed method call"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-12-06 11:33:31 +00:00
|
|
|
pub struct DisallowedMethods {
|
2023-10-19 14:59:44 +00:00
|
|
|
conf_disallowed: Vec<DisallowedPath>,
|
2022-01-13 12:18:19 +00:00
|
|
|
disallowed: DefIdMap<usize>,
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
impl DisallowedMethods {
|
2023-10-19 14:59:44 +00:00
|
|
|
pub fn new(conf_disallowed: Vec<DisallowedPath>) -> Self {
|
2020-10-09 10:45:29 +00:00
|
|
|
Self {
|
2021-09-28 17:03:12 +00:00
|
|
|
conf_disallowed,
|
|
|
|
disallowed: DefIdMap::default(),
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
impl_lint_pass!(DisallowedMethods => [DISALLOWED_METHODS]);
|
2020-10-09 10:45:29 +00:00
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
|
2021-09-12 09:58:27 +00:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
2022-01-13 12:18:19 +00:00
|
|
|
for (index, conf) in self.conf_disallowed.iter().enumerate() {
|
|
|
|
let segs: Vec<_> = conf.path().split("::").collect();
|
2022-11-21 19:34:47 +00:00
|
|
|
for id in clippy_utils::def_path_def_ids(cx, &segs) {
|
2022-01-13 12:18:19 +00:00
|
|
|
self.disallowed.insert(id, index);
|
2021-07-01 16:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:45:29 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-05-21 11:24:00 +00:00
|
|
|
let uncalled_path = if let Some(parent) = get_parent_expr(cx, expr)
|
|
|
|
&& let ExprKind::Call(receiver, _) = parent.kind
|
|
|
|
&& receiver.hir_id == expr.hir_id
|
|
|
|
{
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
path_def_id(cx, expr)
|
|
|
|
};
|
2022-10-23 13:18:45 +00:00
|
|
|
let Some(def_id) = uncalled_path.or_else(|| fn_def_id(cx, expr)) else {
|
2023-07-14 11:27:56 +00:00
|
|
|
return;
|
2021-09-28 17:03:12 +00:00
|
|
|
};
|
2022-01-13 12:18:19 +00:00
|
|
|
let conf = match self.disallowed.get(&def_id) {
|
|
|
|
Some(&index) => &self.conf_disallowed[index],
|
2021-09-28 17:03:12 +00:00
|
|
|
None => return,
|
|
|
|
};
|
2022-01-13 12:18:19 +00:00
|
|
|
let msg = format!("use of a disallowed method `{}`", conf.path());
|
2021-12-06 11:33:31 +00:00
|
|
|
span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, &msg, |diag| {
|
2022-10-06 07:44:38 +00:00
|
|
|
if let Some(reason) = conf.reason() {
|
2022-11-21 19:34:47 +00:00
|
|
|
diag.note(reason);
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
});
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
|
|
|
}
|