|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | + |
| 3 | +use rustc_data_structures::fx::FxHashSet; |
| 4 | +use rustc_hir::{def::Res, Item, ItemKind, PolyTraitRef, TraitBoundModifier, Ty, TyKind, UseKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 7 | +use rustc_span::{Span, Symbol}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// **What it does:** Denies the configured types in clippy.toml. |
| 11 | + /// |
| 12 | + /// **Why is this bad?** Some types are undesirable in certain contexts. |
| 13 | + /// |
| 14 | + /// **Known problems:** The fully qualified path must be used. This lint |
| 15 | + /// doesn't support aliases or reexported names; be aware that many types |
| 16 | + /// in `std` are actually reexports. |
| 17 | + /// |
| 18 | + /// For example, if you want to disallow `BTreeMap`, your clippy.toml |
| 19 | + /// configuration would look like |
| 20 | + /// `disallowed-methods = ["alloc::collections::btree::map::BTreeMap"]` and not |
| 21 | + /// `disallowed-methods = ["std::collections::BTreeMap"]` as you might expect. |
| 22 | + /// |
| 23 | + /// N.B. There is no way to ban primitive types. |
| 24 | + /// |
| 25 | + /// **Example:** |
| 26 | + /// |
| 27 | + /// An example clippy.toml configuration: |
| 28 | + /// ```toml |
| 29 | + /// # clippy.toml |
| 30 | + /// disallowed-methods = ["alloc::collections::btree::map::BTreeMap"] |
| 31 | + /// ``` |
| 32 | + /// |
| 33 | + /// ```rust,ignore |
| 34 | + /// use std::collections::BTreeMap; |
| 35 | + /// // or its use |
| 36 | + /// let x = std::collections::BTreeMap::new(); |
| 37 | + /// ``` |
| 38 | + /// Use instead: |
| 39 | + /// ```rust,ignore |
| 40 | + /// // A similar type that is allowed by the config |
| 41 | + /// use std::collections::HashMap; |
| 42 | + /// ``` |
| 43 | + pub DISALLOWED_TYPE, |
| 44 | + nursery, |
| 45 | + "use of a disallowed type" |
| 46 | +} |
| 47 | +#[derive(Clone, Debug)] |
| 48 | +pub struct DisallowedType { |
| 49 | + disallowed: FxHashSet<Vec<Symbol>>, |
| 50 | +} |
| 51 | + |
| 52 | +impl DisallowedType { |
| 53 | + pub fn new(disallowed: &FxHashSet<String>) -> Self { |
| 54 | + Self { |
| 55 | + disallowed: disallowed |
| 56 | + .iter() |
| 57 | + .map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>()) |
| 58 | + .collect(), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl_lint_pass!(DisallowedType => [DISALLOWED_TYPE]); |
| 64 | + |
| 65 | +impl<'tcx> LateLintPass<'tcx> for DisallowedType { |
| 66 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 67 | + if_chain! { |
| 68 | + if let ItemKind::Use(path, UseKind::Single) = &item.kind; |
| 69 | + if let Res::Def(_, id) = path.res; |
| 70 | + let use_path = cx.get_def_path(id); |
| 71 | + if let Some(name) = self.disallowed.iter().find(|path| **path == use_path); |
| 72 | + then { |
| 73 | + emit(cx, name, item.span,); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) { |
| 79 | + if_chain! { |
| 80 | + if let TyKind::Path(path) = &ty.kind; |
| 81 | + if let Some(did) = cx.qpath_res(path, ty.hir_id).opt_def_id(); |
| 82 | + let use_path = cx.get_def_path(did); |
| 83 | + if let Some(name) = self.disallowed.iter().find(|path| **path == use_path); |
| 84 | + then { |
| 85 | + emit(cx, name, path.span()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn check_poly_trait_ref(&mut self, cx: &LateContext<'tcx>, poly: &'tcx PolyTraitRef<'tcx>, _: TraitBoundModifier) { |
| 91 | + if_chain! { |
| 92 | + if let Res::Def(_, did) = poly.trait_ref.path.res; |
| 93 | + let use_path = cx.get_def_path(did); |
| 94 | + if let Some(name) = self.disallowed.iter().find(|path| **path == use_path); |
| 95 | + then { |
| 96 | + emit(cx, name, poly.trait_ref.path.span); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // TODO: if non primitive const generics are a thing |
| 102 | + // fn check_generic_arg(&mut self, cx: &LateContext<'tcx>, arg: &'tcx GenericArg<'tcx>) { |
| 103 | + // match arg { |
| 104 | + // GenericArg::Const(c) => {}, |
| 105 | + // } |
| 106 | + // } |
| 107 | + // fn check_generic_param(&mut self, cx: &LateContext<'tcx>, param: &'tcx GenericParam<'tcx>) { |
| 108 | + // match param.kind { |
| 109 | + // GenericParamKind::Const { .. } => {}, |
| 110 | + // } |
| 111 | + // } |
| 112 | +} |
| 113 | + |
| 114 | +fn emit(cx: &LateContext<'_>, name: &[Symbol], span: Span) { |
| 115 | + let name = name.iter().map(|s| s.to_ident_string()).collect::<Vec<_>>().join("::"); |
| 116 | + span_lint( |
| 117 | + cx, |
| 118 | + DISALLOWED_TYPE, |
| 119 | + span, |
| 120 | + &format!("`{}` is not allowed according to config", name), |
| 121 | + ); |
| 122 | +} |
0 commit comments