2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2021-07-29 10:16:06 +00:00
|
|
|
use clippy_utils::trait_ref_of_method;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-10-23 20:16:59 +00:00
|
|
|
use rustc_middle::ty::TypeFoldable;
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2021-07-29 10:16:06 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2021-03-08 23:57:44 +00:00
|
|
|
use std::iter;
|
2019-12-06 18:45:33 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for sets/maps with mutable key types.
|
2019-12-06 18:45:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// All of `HashMap`, `HashSet`, `BTreeMap` and
|
2019-12-06 18:45:33 +00:00
|
|
|
/// `BtreeSet` rely on either the hash or the order of keys be unchanging,
|
|
|
|
/// so having types with interior mutability is a bad idea.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// It's correct to use a struct, that contains interior mutability
|
2020-09-24 12:49:22 +00:00
|
|
|
/// as a key, when its `Hash` implementation doesn't access any of the interior mutable types.
|
|
|
|
/// However, this lint is unable to recognize this, so it causes a false positive in theses cases.
|
|
|
|
/// The `bytes` crate is a great example of this.
|
2019-12-06 18:45:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-12-06 18:45:33 +00:00
|
|
|
/// ```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,
|
2021-07-01 16:17:38 +00:00
|
|
|
suspicious,
|
2020-01-06 06:30:43 +00:00
|
|
|
"Check for mutable `Map`/`Set` key type"
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]);
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
|
2019-12-06 18:45:33 +00:00
|
|
|
if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
|
2021-04-08 15:50:13 +00:00
|
|
|
check_sig(cx, item.hir_id(), sig.decl);
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
|
2020-03-16 15:00:16 +00:00
|
|
|
if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind {
|
2021-01-30 22:25:03 +00:00
|
|
|
if trait_ref_of_method(cx, item.hir_id()).is_none() {
|
2021-04-08 15:50:13 +00:00
|
|
|
check_sig(cx, item.hir_id(), sig.decl);
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) {
|
2020-03-12 19:56:55 +00:00
|
|
|
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
|
2021-04-08 15:50:13 +00:00
|
|
|
check_sig(cx, item.hir_id(), sig.decl);
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
|
2019-12-06 18:45:33 +00:00
|
|
|
if let hir::PatKind::Wild = local.pat.kind {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-17 08:47:04 +00:00
|
|
|
check_ty(cx, local.span, cx.typeck_results().pat_ty(&*local.pat));
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) {
|
2019-12-06 18:45:33 +00:00
|
|
|
let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
|
|
|
|
let fn_sig = cx.tcx.fn_sig(fn_def_id);
|
2021-03-08 23:57:44 +00:00
|
|
|
for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) {
|
2019-12-06 18:45:33 +00:00
|
|
|
check_ty(cx, hir_ty.span, ty);
|
|
|
|
}
|
2020-11-23 12:51:04 +00:00
|
|
|
check_ty(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output()));
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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).
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_ty<'tcx>(cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) {
|
2020-09-24 12:49:22 +00:00
|
|
|
let ty = ty.peel_refs();
|
2020-08-03 22:18:29 +00:00
|
|
|
if let Adt(def, substs) = ty.kind() {
|
2021-07-29 10:16:06 +00:00
|
|
|
if [sym::hashmap_type, sym::BTreeMap, sym::hashset_type, sym::BTreeMap]
|
2019-12-06 18:45:33 +00:00
|
|
|
.iter()
|
2021-07-29 10:16:06 +00:00
|
|
|
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did))
|
2020-01-16 23:18:37 +00:00
|
|
|
&& is_mutable_type(cx, substs.type_at(0), span)
|
2019-12-06 18:45:33 +00:00
|
|
|
{
|
2020-01-16 23:18:37 +00:00
|
|
|
span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_mutable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span) -> bool {
|
2020-08-03 22:18:29 +00:00
|
|
|
match *ty.kind() {
|
2020-01-16 23:18:37 +00: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-10-23 20:16:59 +00:00
|
|
|
Adt(..) => {
|
2021-07-15 08:44:10 +00:00
|
|
|
!ty.has_escaping_bound_vars()
|
|
|
|
&& cx.tcx.layout_of(cx.param_env.and(ty)).is_ok()
|
2020-10-23 20:16:59 +00:00
|
|
|
&& !ty.is_freeze(cx.tcx.at(span), cx.param_env)
|
|
|
|
},
|
2020-01-16 23:18:37 +00:00
|
|
|
_ => false,
|
2019-12-06 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|