1 use crate::utils::span_lint;
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_tool_lint, impl_lint_pass};
7 use rustc_span::Symbol;
10 /// **What it does:** Lints for specific trait methods defined in clippy.toml
12 /// **Why is this bad?** Some methods are undesirable in certain contexts,
13 /// and it would be beneficial to lint for them as needed.
15 /// **Known problems:** None.
20 /// // example code where clippy issues a warning
21 /// foo.bad_method(); // Foo::bad_method is disallowed in the configuration
25 /// // example code which does not raise clippy warning
26 /// goodStruct.bad_method(); // GoodStruct::bad_method is not disallowed
28 pub DISALLOWED_METHOD,
30 "use of a disallowed method call"
33 #[derive(Clone, Debug)]
34 pub struct DisallowedMethod {
35 disallowed: FxHashSet<Vec<Symbol>>,
38 impl DisallowedMethod {
39 pub fn new(disallowed: &FxHashSet<String>) -> Self {
41 disallowed: disallowed
43 .map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
49 impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
51 impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
52 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
53 if let ExprKind::MethodCall(_path, _, _args, _) = &expr.kind {
54 let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
56 let method_call = cx.get_def_path(def_id);
57 if self.disallowed.contains(&method_call) {
58 let method = method_call
60 .map(|s| s.to_ident_string())
68 &format!("use of a disallowed method `{}`", method),