2019-12-06 12:45:33 -06:00
|
|
|
use crate::utils::{match_def_path, paths, span_lint, trait_ref_of_method, walk_ptrs_ty};
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2019-12-06 12:45:33 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for sets/maps with mutable key types.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** All of `HashMap`, `HashSet`, `BTreeMap` and
|
|
|
|
/// `BtreeSet` rely on either the hash or the order of keys be unchanging,
|
|
|
|
/// so having types with interior mutability is a bad idea.
|
|
|
|
///
|
|
|
|
/// **Known problems:** We don't currently account for `Rc` or `Arc`, so
|
|
|
|
/// this may yield false positives.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// use std::cmp::{PartialEq, Eq};
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
/// use std::hash::{Hash, Hasher};
|
|
|
|
/// use std::sync::atomic::AtomicUsize;
|
|
|
|
///# #[allow(unused)]
|
|
|
|
///
|
|
|
|
/// struct Bad(AtomicUsize);
|
|
|
|
/// impl PartialEq for Bad {
|
|
|
|
/// fn eq(&self, rhs: &Self) -> bool {
|
|
|
|
/// ..
|
|
|
|
/// ; unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Eq for Bad {}
|
|
|
|
///
|
|
|
|
/// impl Hash for Bad {
|
|
|
|
/// fn hash<H: Hasher>(&self, h: &mut H) {
|
|
|
|
/// ..
|
|
|
|
/// ; unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let _: HashSet<Bad> = HashSet::new();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub MUTABLE_KEY_TYPE,
|
|
|
|
correctness,
|
2020-01-06 00:30:43 -06:00
|
|
|
"Check for mutable `Map`/`Set` key type"
|
2019-12-06 12:45:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]);
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableKeyType {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'tcx>) {
|
|
|
|
if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
|
|
|
|
check_sig(cx, item.hir_id, &sig.decl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
|
2020-03-16 10:00:16 -05:00
|
|
|
if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind {
|
2019-12-06 12:45:33 -06:00
|
|
|
if trait_ref_of_method(cx, item.hir_id).is_none() {
|
|
|
|
check_sig(cx, item.hir_id, &sig.decl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'tcx>) {
|
2020-03-12 14:56:55 -05:00
|
|
|
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
|
2019-12-06 12:45:33 -06:00
|
|
|
check_sig(cx, item.hir_id, &sig.decl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
fn check_local(&mut self, cx: &LateContext<'_, '_>, local: &hir::Local<'_>) {
|
2019-12-06 12:45:33 -06:00
|
|
|
if let hir::PatKind::Wild = local.pat.kind {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
check_ty(cx, local.span, cx.tables.pat_ty(&*local.pat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
fn check_sig<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) {
|
2019-12-06 12:45:33 -06:00
|
|
|
let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
|
|
|
|
let fn_sig = cx.tcx.fn_sig(fn_def_id);
|
|
|
|
for (hir_ty, ty) in decl.inputs.iter().zip(fn_sig.inputs().skip_binder().iter()) {
|
|
|
|
check_ty(cx, hir_ty.span, ty);
|
|
|
|
}
|
|
|
|
check_ty(
|
|
|
|
cx,
|
|
|
|
decl.output.span(),
|
|
|
|
cx.tcx.erase_late_bound_regions(&fn_sig.output()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to lint 1. sets or maps with 2. not immutable key types and 3. no unerased
|
|
|
|
// generics (because the compiler cannot ensure immutability for unknown types).
|
|
|
|
fn check_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, ty: Ty<'tcx>) {
|
|
|
|
let ty = walk_ptrs_ty(ty);
|
|
|
|
if let Adt(def, substs) = ty.kind {
|
|
|
|
if [&paths::HASHMAP, &paths::BTREEMAP, &paths::HASHSET, &paths::BTREESET]
|
|
|
|
.iter()
|
|
|
|
.any(|path| match_def_path(cx, def.did, &**path))
|
2020-01-16 17:18:37 -06:00
|
|
|
&& is_mutable_type(cx, substs.type_at(0), span)
|
2019-12-06 12:45:33 -06:00
|
|
|
{
|
2020-01-16 17:18:37 -06:00
|
|
|
span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
|
2019-12-06 12:45:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:18:37 -06:00
|
|
|
fn is_mutable_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>, span: Span) -> bool {
|
2019-12-28 07:45:41 -06:00
|
|
|
match ty.kind {
|
2020-01-16 17:18:37 -06:00
|
|
|
RawPtr(TypeAndMut { ty: inner_ty, mutbl }) | Ref(_, inner_ty, mutbl) => {
|
|
|
|
mutbl == hir::Mutability::Mut || is_mutable_type(cx, inner_ty, span)
|
|
|
|
},
|
|
|
|
Slice(inner_ty) => is_mutable_type(cx, inner_ty, span),
|
|
|
|
Array(inner_ty, size) => {
|
|
|
|
size.try_eval_usize(cx.tcx, cx.param_env).map_or(true, |u| u != 0) && is_mutable_type(cx, inner_ty, span)
|
|
|
|
},
|
|
|
|
Tuple(..) => ty.tuple_fields().any(|ty| is_mutable_type(cx, ty, span)),
|
2020-06-21 04:20:48 -05:00
|
|
|
Adt(..) => cx.tcx.layout_of(cx.param_env.and(ty)).is_ok() && !ty.is_freeze(cx.tcx.at(span), cx.param_env),
|
2020-01-16 17:18:37 -06:00
|
|
|
_ => false,
|
2019-12-06 12:45:33 -06:00
|
|
|
}
|
|
|
|
}
|