1 use crate::utils::span_lint;
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_lint::{LateLintPass, LateContext};
5 use rustc_session::{impl_lint_pass, declare_tool_lint};
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 is disallowed
25 /// // example code which does not raise clippy warning
26 /// GoodStruct.bad_method(); // not disallowed
28 pub DISALLOWED_METHOD,
30 "used 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.iter()
43 s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>()
50 impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
52 impl <'tcx> LateLintPass<'tcx> for DisallowedMethod {
53 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
54 if let ExprKind::MethodCall(_path, _, _args, _) = &expr.kind {
55 let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
57 let method_call = cx.get_def_path(def_id);
58 if self.disallowed.contains(&method_call) {
64 "Use of a disallowed method `{}`",
67 .map(|s| s.to_ident_string())