]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_key.rs
Auto merge of #4947 - rust-lang:doc-main-extern-crate, r=flip1995
[rust.git] / clippy_lints / src / mut_key.rs
1 use crate::utils::{match_def_path, paths, span_lint, trait_ref_of_method, walk_ptrs_ty};
2 use rustc::declare_lint_pass;
3 use rustc::hir;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::ty::{Adt, Dynamic, Opaque, Param, RawPtr, Ref, Ty, TypeAndMut};
6 use rustc_session::declare_tool_lint;
7 use syntax::source_map::Span;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for sets/maps with mutable key types.
11     ///
12     /// **Why is this bad?** All of `HashMap`, `HashSet`, `BTreeMap` and
13     /// `BtreeSet` rely on either the hash or the order of keys be unchanging,
14     /// so having types with interior mutability is a bad idea.
15     ///
16     /// **Known problems:** We don't currently account for `Rc` or `Arc`, so
17     /// this may yield false positives.
18     ///
19     /// **Example:**
20     /// ```rust
21     /// use std::cmp::{PartialEq, Eq};
22     /// use std::collections::HashSet;
23     /// use std::hash::{Hash, Hasher};
24     /// use std::sync::atomic::AtomicUsize;
25     ///# #[allow(unused)]
26     ///
27     /// struct Bad(AtomicUsize);
28     /// impl PartialEq for Bad {
29     ///     fn eq(&self, rhs: &Self) -> bool {
30     ///          ..
31     /// ; unimplemented!();
32     ///     }
33     /// }
34     ///
35     /// impl Eq for Bad {}
36     ///
37     /// impl Hash for Bad {
38     ///     fn hash<H: Hasher>(&self, h: &mut H) {
39     ///         ..
40     /// ; unimplemented!();
41     ///     }
42     /// }
43     ///
44     /// fn main() {
45     ///     let _: HashSet<Bad> = HashSet::new();
46     /// }
47     /// ```
48     pub MUTABLE_KEY_TYPE,
49     correctness,
50     "Check for mutable Map/Set key type"
51 }
52
53 declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]);
54
55 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableKeyType {
56     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'tcx>) {
57         if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
58             check_sig(cx, item.hir_id, &sig.decl);
59         }
60     }
61
62     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
63         if let hir::ImplItemKind::Method(ref sig, ..) = item.kind {
64             if trait_ref_of_method(cx, item.hir_id).is_none() {
65                 check_sig(cx, item.hir_id, &sig.decl);
66             }
67         }
68     }
69
70     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'tcx>) {
71         if let hir::TraitItemKind::Method(ref sig, ..) = item.kind {
72             check_sig(cx, item.hir_id, &sig.decl);
73         }
74     }
75
76     fn check_local(&mut self, cx: &LateContext<'_, '_>, local: &hir::Local) {
77         if let hir::PatKind::Wild = local.pat.kind {
78             return;
79         }
80         check_ty(cx, local.span, cx.tables.pat_ty(&*local.pat));
81     }
82 }
83
84 fn check_sig<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl) {
85     let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
86     let fn_sig = cx.tcx.fn_sig(fn_def_id);
87     for (hir_ty, ty) in decl.inputs.iter().zip(fn_sig.inputs().skip_binder().iter()) {
88         check_ty(cx, hir_ty.span, ty);
89     }
90     check_ty(
91         cx,
92         decl.output.span(),
93         cx.tcx.erase_late_bound_regions(&fn_sig.output()),
94     );
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<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, ty: Ty<'tcx>) {
100     let ty = walk_ptrs_ty(ty);
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         {
106             let key_type = substs.type_at(0);
107             if is_concrete_type(key_type) && !key_type.is_freeze(cx.tcx, cx.param_env, span) {
108                 span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
109             }
110         }
111     }
112 }
113
114 fn is_concrete_type(ty: Ty<'_>) -> bool {
115     match ty.kind {
116         RawPtr(TypeAndMut { ty: inner_ty, .. }) | Ref(_, inner_ty, _) => is_concrete_type(inner_ty),
117         Dynamic(..) | Opaque(..) | Param(..) => false,
118         _ => true,
119     }
120 }