2020-09-24 20:32:03 +00:00
|
|
|
use crate::utils::span_lint;
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-09-24 21:26:29 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
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! {
|
|
|
|
/// **What it does:** Lints for specific trait methods defined in clippy.toml
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Some methods are undesirable in certain contexts,
|
|
|
|
/// and it would be beneficial to lint for them as needed.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
2020-09-24 21:43:29 +00:00
|
|
|
/// ```rust,ignore
|
2020-09-24 20:32:03 +00:00
|
|
|
/// // example code where clippy issues a warning
|
2020-09-25 16:03:45 +00:00
|
|
|
/// foo.bad_method(); // Foo::bad_method is disallowed in the configuration
|
2020-09-24 20:32:03 +00:00
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2020-09-24 21:43:29 +00:00
|
|
|
/// ```rust,ignore
|
2020-09-24 20:32:03 +00:00
|
|
|
/// // example code which does not raise clippy warning
|
2020-09-25 16:03:45 +00:00
|
|
|
/// goodStruct.bad_method(); // GoodStruct::bad_method is not disallowed
|
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>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
|
|
|
|
|
2020-09-24 21:00:46 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
|
2020-09-24 20:32:03 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2020-09-24 20:36:38 +00:00
|
|
|
if let ExprKind::MethodCall(_path, _, _args, _) = &expr.kind {
|
2020-09-24 20:32:03 +00:00
|
|
|
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
|
|
|
|
|
|
|
|
let method_call = cx.get_def_path(def_id);
|
|
|
|
if self.disallowed.contains(&method_call) {
|
2020-09-25 14:38:19 +00:00
|
|
|
let method = method_call
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.to_ident_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("::");
|
|
|
|
|
2020-09-24 20:32:03 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DISALLOWED_METHOD,
|
|
|
|
expr.span,
|
2020-09-25 14:38:19 +00:00
|
|
|
&format!("use of a disallowed method `{}`", method),
|
2020-09-24 20:32:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|