1 use crate::utils::{match_def_path, paths, span_lint, trait_ref_of_method, walk_ptrs_ty};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::Span;
9 /// **What it does:** Checks for sets/maps with mutable key types.
11 /// **Why is this bad?** All of `HashMap`, `HashSet`, `BTreeMap` and
12 /// `BtreeSet` rely on either the hash or the order of keys be unchanging,
13 /// so having types with interior mutability is a bad idea.
15 /// **Known problems:** We don't currently account for `Rc` or `Arc`, so
16 /// this may yield false positives.
20 /// use std::cmp::{PartialEq, Eq};
21 /// use std::collections::HashSet;
22 /// use std::hash::{Hash, Hasher};
23 /// use std::sync::atomic::AtomicUsize;
26 /// struct Bad(AtomicUsize);
27 /// impl PartialEq for Bad {
28 /// fn eq(&self, rhs: &Self) -> bool {
30 /// ; unimplemented!();
34 /// impl Eq for Bad {}
36 /// impl Hash for Bad {
37 /// fn hash<H: Hasher>(&self, h: &mut H) {
39 /// ; unimplemented!();
44 /// let _: HashSet<Bad> = HashSet::new();
49 "Check for mutable `Map`/`Set` key type"
52 declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]);
54 impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
55 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
56 if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
57 check_sig(cx, item.hir_id, &sig.decl);
61 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
62 if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind {
63 if trait_ref_of_method(cx, item.hir_id).is_none() {
64 check_sig(cx, item.hir_id, &sig.decl);
69 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) {
70 if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
71 check_sig(cx, item.hir_id, &sig.decl);
75 fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
76 if let hir::PatKind::Wild = local.pat.kind {
79 check_ty(cx, local.span, cx.typeck_results().pat_ty(&*local.pat));
83 fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) {
84 let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
85 let fn_sig = cx.tcx.fn_sig(fn_def_id);
86 for (hir_ty, ty) in decl.inputs.iter().zip(fn_sig.inputs().skip_binder().iter()) {
87 check_ty(cx, hir_ty.span, ty);
92 cx.tcx.erase_late_bound_regions(&fn_sig.output()),
96 // We want to lint 1. sets or maps with 2. not immutable key types and 3. no unerased
97 // generics (because the compiler cannot ensure immutability for unknown types).
98 fn check_ty<'tcx>(cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) {
99 let ty = walk_ptrs_ty(ty);
100 if let Adt(def, substs) = ty.kind {
101 if [&paths::HASHMAP, &paths::BTREEMAP, &paths::HASHSET, &paths::BTREESET]
103 .any(|path| match_def_path(cx, def.did, &**path))
104 && is_mutable_type(cx, substs.type_at(0), span)
106 span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
111 fn is_mutable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span) -> bool {
113 RawPtr(TypeAndMut { ty: inner_ty, mutbl }) | Ref(_, inner_ty, mutbl) => {
114 mutbl == hir::Mutability::Mut || is_mutable_type(cx, inner_ty, span)
116 Slice(inner_ty) => is_mutable_type(cx, inner_ty, span),
117 Array(inner_ty, size) => {
118 size.try_eval_usize(cx.tcx, cx.param_env).map_or(true, |u| u != 0) && is_mutable_type(cx, inner_ty, span)
120 Tuple(..) => ty.tuple_fields().any(|ty| is_mutable_type(cx, ty, span)),
121 Adt(..) => cx.tcx.layout_of(cx.param_env.and(ty)).is_ok() && !ty.is_freeze(cx.tcx.at(span), cx.param_env),