]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/disallowed_types.rs
rustc_hir: Change representation of import paths to support multiple resolutions
[rust.git] / clippy_lints / src / disallowed_types.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_hir::def::Res;
5 use rustc_hir::def_id::DefId;
6 use rustc_hir::{Item, ItemKind, PolyTraitRef, PrimTy, Ty, TyKind, UseKind};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::Span;
10
11 use crate::utils::conf;
12
13 declare_clippy_lint! {
14     /// ### What it does
15     /// Denies the configured types in clippy.toml.
16     ///
17     /// Note: Even though this lint is warn-by-default, it will only trigger if
18     /// types are defined in the clippy.toml file.
19     ///
20     /// ### Why is this bad?
21     /// Some types are undesirable in certain contexts.
22     ///
23     /// ### Example:
24     /// An example clippy.toml configuration:
25     /// ```toml
26     /// # clippy.toml
27     /// disallowed-types = [
28     ///     # Can use a string as the path of the disallowed type.
29     ///     "std::collections::BTreeMap",
30     ///     # Can also use an inline table with a `path` key.
31     ///     { path = "std::net::TcpListener" },
32     ///     # When using an inline table, can add a `reason` for why the type
33     ///     # is disallowed.
34     ///     { path = "std::net::Ipv4Addr", reason = "no IPv4 allowed" },
35     /// ]
36     /// ```
37     ///
38     /// ```rust,ignore
39     /// use std::collections::BTreeMap;
40     /// // or its use
41     /// let x = std::collections::BTreeMap::new();
42     /// ```
43     /// Use instead:
44     /// ```rust,ignore
45     /// // A similar type that is allowed by the config
46     /// use std::collections::HashMap;
47     /// ```
48     #[clippy::version = "1.55.0"]
49     pub DISALLOWED_TYPES,
50     style,
51     "use of disallowed types"
52 }
53 #[derive(Clone, Debug)]
54 pub struct DisallowedTypes {
55     conf_disallowed: Vec<conf::DisallowedPath>,
56     def_ids: FxHashMap<DefId, usize>,
57     prim_tys: FxHashMap<PrimTy, usize>,
58 }
59
60 impl DisallowedTypes {
61     pub fn new(conf_disallowed: Vec<conf::DisallowedPath>) -> Self {
62         Self {
63             conf_disallowed,
64             def_ids: FxHashMap::default(),
65             prim_tys: FxHashMap::default(),
66         }
67     }
68
69     fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) {
70         match res {
71             Res::Def(_, did) => {
72                 if let Some(&index) = self.def_ids.get(did) {
73                     emit(cx, &cx.tcx.def_path_str(*did), span, &self.conf_disallowed[index]);
74                 }
75             },
76             Res::PrimTy(prim) => {
77                 if let Some(&index) = self.prim_tys.get(prim) {
78                     emit(cx, prim.name_str(), span, &self.conf_disallowed[index]);
79                 }
80             },
81             _ => {},
82         }
83     }
84 }
85
86 impl_lint_pass!(DisallowedTypes => [DISALLOWED_TYPES]);
87
88 impl<'tcx> LateLintPass<'tcx> for DisallowedTypes {
89     fn check_crate(&mut self, cx: &LateContext<'_>) {
90         for (index, conf) in self.conf_disallowed.iter().enumerate() {
91             let segs: Vec<_> = conf.path().split("::").collect();
92
93             for res in clippy_utils::def_path_res(cx, &segs) {
94                 match res {
95                     Res::Def(_, id) => {
96                         self.def_ids.insert(id, index);
97                     },
98                     Res::PrimTy(ty) => {
99                         self.prim_tys.insert(ty, index);
100                     },
101                     _ => {},
102                 }
103             }
104         }
105     }
106
107     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
108         if let ItemKind::Use(path, UseKind::Single) = &item.kind {
109             for res in &path.res {
110                 self.check_res_emit(cx, res, item.span);
111             }
112         }
113     }
114
115     fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
116         if let TyKind::Path(path) = &ty.kind {
117             self.check_res_emit(cx, &cx.qpath_res(path, ty.hir_id), ty.span);
118         }
119     }
120
121     fn check_poly_trait_ref(&mut self, cx: &LateContext<'tcx>, poly: &'tcx PolyTraitRef<'tcx>) {
122         self.check_res_emit(cx, &poly.trait_ref.path.res, poly.trait_ref.path.span);
123     }
124 }
125
126 fn emit(cx: &LateContext<'_>, name: &str, span: Span, conf: &conf::DisallowedPath) {
127     span_lint_and_then(
128         cx,
129         DISALLOWED_TYPES,
130         span,
131         &format!("`{name}` is not allowed according to config"),
132         |diag| {
133             if let Some(reason) = conf.reason() {
134                 diag.note(reason);
135             }
136         },
137     );
138 }