2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2021-03-16 16:06:34 +00:00
|
|
|
use clippy_utils::fn_def_id;
|
2020-09-24 20:32:03 +00:00
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2021-06-11 21:25:32 +00:00
|
|
|
use rustc_hir::{def::Res, def_id::DefId, Crate, Expr};
|
2020-09-24 21:00:46 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-09-24 20:32:03 +00:00
|
|
|
use rustc_span::Symbol;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-01-30 06:18:56 +00:00
|
|
|
/// **What it does:** Denies the configured methods and functions in clippy.toml
|
2020-09-24 20:32:03 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Some methods are undesirable in certain contexts,
|
2021-01-30 06:18:56 +00:00
|
|
|
/// and it's beneficial to lint for them as needed.
|
2020-09-24 20:32:03 +00:00
|
|
|
///
|
2021-06-13 19:52:54 +00:00
|
|
|
/// **Known problems:** None.
|
2020-09-24 20:32:03 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
2021-01-30 06:18:56 +00:00
|
|
|
/// An example clippy.toml configuration:
|
|
|
|
/// ```toml
|
|
|
|
/// # clippy.toml
|
2021-06-13 19:52:54 +00:00
|
|
|
/// disallowed-methods = ["std::vec::Vec::leak", "std::time::Instant::now"]
|
2021-01-30 06:18:56 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2020-09-24 21:43:29 +00:00
|
|
|
/// ```rust,ignore
|
2021-01-30 06:18:56 +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.
|
|
|
|
///
|
|
|
|
/// let _now = Instant::now(); // Instant::now is disallowed in the config.
|
2020-09-24 20:32:03 +00:00
|
|
|
/// ```
|
2021-01-30 06:18:56 +00:00
|
|
|
///
|
2020-09-24 20:32:03 +00:00
|
|
|
/// Use instead:
|
2020-09-24 21:43:29 +00:00
|
|
|
/// ```rust,ignore
|
2021-01-30 06:18:56 +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-09-24 20:32:03 +00:00
|
|
|
/// ```
|
|
|
|
pub DISALLOWED_METHOD,
|
|
|
|
nursery,
|
2020-09-25 14:38:19 +00:00
|
|
|
"use of a disallowed method call"
|
2020-09-24 20:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct DisallowedMethod {
|
|
|
|
disallowed: FxHashSet<Vec<Symbol>>,
|
2021-06-11 21:25:32 +00:00
|
|
|
def_ids: FxHashSet<(DefId, Vec<Symbol>)>,
|
2020-09-24 20:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DisallowedMethod {
|
2020-09-24 21:26:29 +00:00
|
|
|
pub fn new(disallowed: &FxHashSet<String>) -> Self {
|
2020-09-24 20:32:03 +00:00
|
|
|
Self {
|
2020-09-24 21:00:46 +00:00
|
|
|
disallowed: disallowed
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
|
2020-09-24 20:32:03 +00:00
|
|
|
.collect(),
|
2021-06-11 21:25:32 +00:00
|
|
|
def_ids: FxHashSet::default(),
|
2020-09-24 20:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
|
|
|
|
|
2020-09-24 21:00:46 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
|
2021-06-11 21:25:32 +00:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
|
|
|
for path in &self.disallowed {
|
|
|
|
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
|
|
|
|
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>())
|
|
|
|
{
|
|
|
|
self.def_ids.insert((id, path.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:32:03 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-01-30 06:18:56 +00:00
|
|
|
if let Some(def_id) = fn_def_id(cx, expr) {
|
2021-06-11 21:25:32 +00:00
|
|
|
if self.def_ids.iter().any(|(id, _)| def_id == *id) {
|
|
|
|
let func_path = cx.get_def_path(def_id);
|
2021-01-30 06:18:56 +00:00
|
|
|
let func_path_string = func_path
|
|
|
|
.into_iter()
|
|
|
|
.map(Symbol::to_ident_string)
|
2020-09-25 14:38:19 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("::");
|
|
|
|
|
2020-09-24 20:32:03 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DISALLOWED_METHOD,
|
|
|
|
expr.span,
|
2021-01-30 06:18:56 +00:00
|
|
|
&format!("use of a disallowed method `{}`", func_path_string),
|
2020-09-24 20:32:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|