]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/mut_key.rs
feat(rustdoc): open sidebar menu when links inside it are focused
[rust.git] / src / tools / clippy / clippy_lints / src / mut_key.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{match_def_path, paths, trait_ref_of_method};
3 use rustc_hir as hir;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_middle::ty::TypeFoldable;
6 use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::Span;
9 use std::iter;
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks for sets/maps with mutable key types.
13     ///
14     /// **Why is this bad?** All of `HashMap`, `HashSet`, `BTreeMap` and
15     /// `BtreeSet` rely on either the hash or the order of keys be unchanging,
16     /// so having types with interior mutability is a bad idea.
17     ///
18     /// **Known problems:** It's correct to use a struct, that contains interior mutability
19     /// as a key, when its `Hash` implementation doesn't access any of the interior mutable types.
20     /// However, this lint is unable to recognize this, so it causes a false positive in theses cases.
21     /// The `bytes` crate is a great example of this.
22     ///
23     /// **Example:**
24     /// ```rust
25     /// use std::cmp::{PartialEq, Eq};
26     /// use std::collections::HashSet;
27     /// use std::hash::{Hash, Hasher};
28     /// use std::sync::atomic::AtomicUsize;
29     ///# #[allow(unused)]
30     ///
31     /// struct Bad(AtomicUsize);
32     /// impl PartialEq for Bad {
33     ///     fn eq(&self, rhs: &Self) -> bool {
34     ///          ..
35     /// ; unimplemented!();
36     ///     }
37     /// }
38     ///
39     /// impl Eq for Bad {}
40     ///
41     /// impl Hash for Bad {
42     ///     fn hash<H: Hasher>(&self, h: &mut H) {
43     ///         ..
44     /// ; unimplemented!();
45     ///     }
46     /// }
47     ///
48     /// fn main() {
49     ///     let _: HashSet<Bad> = HashSet::new();
50     /// }
51     /// ```
52     pub MUTABLE_KEY_TYPE,
53     suspicious,
54     "Check for mutable `Map`/`Set` key type"
55 }
56
57 declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]);
58
59 impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
60     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
61         if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
62             check_sig(cx, item.hir_id(), sig.decl);
63         }
64     }
65
66     fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
67         if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind {
68             if trait_ref_of_method(cx, item.hir_id()).is_none() {
69                 check_sig(cx, item.hir_id(), sig.decl);
70             }
71         }
72     }
73
74     fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) {
75         if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
76             check_sig(cx, item.hir_id(), sig.decl);
77         }
78     }
79
80     fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
81         if let hir::PatKind::Wild = local.pat.kind {
82             return;
83         }
84         check_ty(cx, local.span, cx.typeck_results().pat_ty(&*local.pat));
85     }
86 }
87
88 fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) {
89     let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
90     let fn_sig = cx.tcx.fn_sig(fn_def_id);
91     for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) {
92         check_ty(cx, hir_ty.span, ty);
93     }
94     check_ty(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output()));
95 }
96
97 // We want to lint 1. sets or maps with 2. not immutable key types and 3. no unerased
98 // generics (because the compiler cannot ensure immutability for unknown types).
99 fn check_ty<'tcx>(cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) {
100     let ty = ty.peel_refs();
101     if let Adt(def, substs) = ty.kind() {
102         if [&paths::HASHMAP, &paths::BTREEMAP, &paths::HASHSET, &paths::BTREESET]
103             .iter()
104             .any(|path| match_def_path(cx, def.did, &**path))
105             && is_mutable_type(cx, substs.type_at(0), span)
106         {
107             span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
108         }
109     }
110 }
111
112 fn is_mutable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span) -> bool {
113     match *ty.kind() {
114         RawPtr(TypeAndMut { ty: inner_ty, mutbl }) | Ref(_, inner_ty, mutbl) => {
115             mutbl == hir::Mutability::Mut || is_mutable_type(cx, inner_ty, span)
116         },
117         Slice(inner_ty) => is_mutable_type(cx, inner_ty, span),
118         Array(inner_ty, size) => {
119             size.try_eval_usize(cx.tcx, cx.param_env).map_or(true, |u| u != 0) && is_mutable_type(cx, inner_ty, span)
120         },
121         Tuple(..) => ty.tuple_fields().any(|ty| is_mutable_type(cx, ty, span)),
122         Adt(..) => {
123             cx.tcx.layout_of(cx.param_env.and(ty)).is_ok()
124                 && !ty.has_escaping_bound_vars()
125                 && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
126         },
127         _ => false,
128     }
129 }