1 use crate::utils::{match_def_path, paths, span_lint, trait_ref_of_method, walk_ptrs_ty};
2 use rustc::declare_lint_pass;
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;
10 /// **What it does:** Checks for sets/maps with mutable key types.
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.
16 /// **Known problems:** We don't currently account for `Rc` or `Arc`, so
17 /// this may yield false positives.
21 /// use std::cmp::{PartialEq, Eq};
22 /// use std::collections::HashSet;
23 /// use std::hash::{Hash, Hasher};
24 /// use std::sync::atomic::AtomicUsize;
27 /// struct Bad(AtomicUsize);
28 /// impl PartialEq for Bad {
29 /// fn eq(&self, rhs: &Self) -> bool {
31 /// ; unimplemented!();
35 /// impl Eq for Bad {}
37 /// impl Hash for Bad {
38 /// fn hash<H: Hasher>(&self, h: &mut H) {
40 /// ; unimplemented!();
45 /// let _: HashSet<Bad> = HashSet::new();
50 "Check for mutable Map/Set key type"
53 declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]);
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);
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);
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);
76 fn check_local(&mut self, cx: &LateContext<'_, '_>, local: &hir::Local<'_>) {
77 if let hir::PatKind::Wild = local.pat.kind {
80 check_ty(cx, local.span, cx.tables.pat_ty(&*local.pat));
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);
93 cx.tcx.erase_late_bound_regions(&fn_sig.output()),
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]
104 .any(|path| match_def_path(cx, def.did, &**path))
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");
114 fn is_concrete_type(ty: Ty<'_>) -> bool {
116 RawPtr(TypeAndMut { ty: inner_ty, .. }) | Ref(_, inner_ty, _) => is_concrete_type(inner_ty),
117 Dynamic(..) | Opaque(..) | Param(..) => false,