]> git.lizzy.rs Git - rust.git/blob - tests/ui/mut_key.rs
Move MSRV tests into the lint specific test files
[rust.git] / tests / ui / mut_key.rs
1 use std::cell::Cell;
2 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
3 use std::hash::{Hash, Hasher};
4 use std::rc::Rc;
5 use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
6 use std::sync::Arc;
7
8 struct Key(AtomicUsize);
9
10 impl Clone for Key {
11     fn clone(&self) -> Self {
12         Key(AtomicUsize::new(self.0.load(Relaxed)))
13     }
14 }
15
16 impl PartialEq for Key {
17     fn eq(&self, other: &Self) -> bool {
18         self.0.load(Relaxed) == other.0.load(Relaxed)
19     }
20 }
21
22 impl Eq for Key {}
23
24 impl Hash for Key {
25     fn hash<H: Hasher>(&self, h: &mut H) {
26         self.0.load(Relaxed).hash(h);
27     }
28 }
29
30 fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
31     let _other: HashMap<Key, bool> = HashMap::new();
32     m.keys().cloned().collect()
33 }
34
35 fn this_is_ok(_m: &mut HashMap<usize, Key>) {}
36
37 // Raw pointers are hashed by the address they point to, so it doesn't matter if they point to a
38 // type with interior mutability.  See:
39 // - clippy issue: https://github.com/rust-lang/rust-clippy/issues/6745
40 // - std lib: https://github.com/rust-lang/rust/blob/1.54.0/library/core/src/hash/mod.rs#L717-L736
41 // So these are OK:
42 fn raw_ptr_is_ok(_m: &mut HashMap<*const Key, ()>) {}
43 fn raw_mut_ptr_is_ok(_m: &mut HashMap<*mut Key, ()>) {}
44
45 #[allow(unused)]
46 trait Trait {
47     type AssociatedType;
48
49     fn trait_fn(&self, set: HashSet<Self::AssociatedType>);
50 }
51
52 fn generics_are_ok_too<K>(_m: &mut HashSet<K>) {
53     // nothing to see here, move along
54 }
55
56 fn tuples<U>(_m: &mut HashMap<((), U), ()>) {}
57
58 fn tuples_bad<U>(_m: &mut HashMap<(Key, U), bool>) {}
59
60 fn main() {
61     let _ = should_not_take_this_arg(&mut HashMap::new(), 1);
62     this_is_ok(&mut HashMap::new());
63     tuples::<Key>(&mut HashMap::new());
64     tuples::<()>(&mut HashMap::new());
65     tuples_bad::<()>(&mut HashMap::new());
66
67     raw_ptr_is_ok(&mut HashMap::new());
68     raw_mut_ptr_is_ok(&mut HashMap::new());
69
70     let _map = HashMap::<Cell<usize>, usize>::new();
71     let _map = HashMap::<&mut Cell<usize>, usize>::new();
72     let _map = HashMap::<&mut usize, usize>::new();
73     // Collection types from `std` who's impl of `Hash` or `Ord` delegate their type parameters
74     let _map = HashMap::<Vec<Cell<usize>>, usize>::new();
75     let _map = HashMap::<BTreeMap<Cell<usize>, ()>, usize>::new();
76     let _map = HashMap::<BTreeMap<(), Cell<usize>>, usize>::new();
77     let _map = HashMap::<BTreeSet<Cell<usize>>, usize>::new();
78     let _map = HashMap::<Option<Cell<usize>>, usize>::new();
79     let _map = HashMap::<Option<Vec<Cell<usize>>>, usize>::new();
80     let _map = HashMap::<Result<&mut usize, ()>, usize>::new();
81     // Smart pointers from `std` who's impl of `Hash` or `Ord` delegate their type parameters
82     let _map = HashMap::<Box<Cell<usize>>, usize>::new();
83     let _map = HashMap::<Rc<Cell<usize>>, usize>::new();
84     let _map = HashMap::<Arc<Cell<usize>>, usize>::new();
85 }