1 use clippy_utils::diagnostics::span_lint_and_then;
3 use rustc_data_structures::fx::FxHashMap;
5 def::Res, def_id::DefId, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind,
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
11 use crate::utils::conf;
13 declare_clippy_lint! {
15 /// Denies the configured types in clippy.toml.
17 /// ### Why is this bad?
18 /// Some types are undesirable in certain contexts.
21 /// An example clippy.toml configuration:
24 /// disallowed-types = [
25 /// # Can use a string as the path of the disallowed type.
26 /// "std::collections::BTreeMap",
27 /// # Can also use an inline table with a `path` key.
28 /// { path = "std::net::TcpListener" },
29 /// # When using an inline table, can add a `reason` for why the type
31 /// { path = "std::net::Ipv4Addr", reason = "no IPv4 allowed" },
36 /// use std::collections::BTreeMap;
38 /// let x = std::collections::BTreeMap::new();
42 /// // A similar type that is allowed by the config
43 /// use std::collections::HashMap;
47 "use of a disallowed type"
49 #[derive(Clone, Debug)]
50 pub struct DisallowedType {
51 conf_disallowed: Vec<conf::DisallowedType>,
52 def_ids: FxHashMap<DefId, Option<String>>,
53 prim_tys: FxHashMap<PrimTy, Option<String>>,
57 pub fn new(conf_disallowed: Vec<conf::DisallowedType>) -> Self {
60 def_ids: FxHashMap::default(),
61 prim_tys: FxHashMap::default(),
65 fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) {
68 if let Some(reason) = self.def_ids.get(did) {
69 emit(cx, &cx.tcx.def_path_str(*did), span, reason.as_deref());
72 Res::PrimTy(prim) => {
73 if let Some(reason) = self.prim_tys.get(prim) {
74 emit(cx, prim.name_str(), span, reason.as_deref());
82 impl_lint_pass!(DisallowedType => [DISALLOWED_TYPE]);
84 impl<'tcx> LateLintPass<'tcx> for DisallowedType {
85 fn check_crate(&mut self, cx: &LateContext<'_>) {
86 for conf in &self.conf_disallowed {
87 let (path, reason) = match conf {
88 conf::DisallowedType::Simple(path) => (path, None),
89 conf::DisallowedType::WithReason { path, reason } => (
91 reason.as_ref().map(|reason| format!("{} (from clippy.toml)", reason)),
94 let segs: Vec<_> = path.split("::").collect();
95 match clippy_utils::path_to_res(cx, &segs) {
97 self.def_ids.insert(id, reason);
100 self.prim_tys.insert(ty, reason);
107 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
108 if let ItemKind::Use(path, UseKind::Single) = &item.kind {
109 self.check_res_emit(cx, &path.res, item.span);
113 fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
114 if let TyKind::Path(path) = &ty.kind {
115 self.check_res_emit(cx, &cx.qpath_res(path, ty.hir_id), ty.span);
119 fn check_poly_trait_ref(&mut self, cx: &LateContext<'tcx>, poly: &'tcx PolyTraitRef<'tcx>, _: TraitBoundModifier) {
120 self.check_res_emit(cx, &poly.trait_ref.path.res, poly.trait_ref.path.span);
124 fn emit(cx: &LateContext<'_>, name: &str, span: Span, reason: Option<&str>) {
129 &format!("`{}` is not allowed according to config", name),
131 if let Some(reason) = reason {