Skip to content

Commit

Permalink
Auto merge of #528 - ToMe25:clippy_fixes, r=Amanieu
Browse files Browse the repository at this point in the history
Fix clippy multiple_bound_location warnings

This PR moves the `?Sized` generics bound used in the map and set source files from the generics predicate to the where clause, if a where clause already exists.

According to what I could find online that `?Sized` bound had to be there until rustc 1.15, but since `?Sized` is already specified in where clauses in these files I don't think thats an issue.

I always put the `?Sized` bound last, because that is where it already appears in the docs, but if `?Sized` first is preferred, I'll change that.

This PR also changes the `Debug` impls for *EntryRef to use a where clause, because they currently have a linebreak in "impl X for Y" line.
If this last change isn't wanted or considered something separate, please let me know :)
  • Loading branch information
bors committed May 30, 2024
2 parents 8359365 + 1c8b695 commit f540cb7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 67 deletions.
118 changes: 63 additions & 55 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where
#[cfg_attr(feature = "inline-more", inline)]
fn equivalent_key<Q, K, V>(k: &Q) -> impl Fn(&(K, V)) -> bool + '_
where
Q: ?Sized + Equivalent<K>,
Q: Equivalent<K> + ?Sized,
{
move |x| k.equivalent(&x.0)
}
Expand All @@ -234,7 +234,7 @@ where
#[cfg_attr(feature = "inline-more", inline)]
fn equivalent<Q, K>(k: &Q) -> impl Fn(&K) -> bool + '_
where
Q: ?Sized + Equivalent<K>,
Q: Equivalent<K> + ?Sized,
{
move |x| k.equivalent(x)
}
Expand Down Expand Up @@ -1264,9 +1264,9 @@ where
/// assert_eq!(words["horseyland"], 1);
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn entry_ref<'a, 'b, Q: ?Sized>(&'a mut self, key: &'b Q) -> EntryRef<'a, 'b, K, Q, V, S, A>
pub fn entry_ref<'a, 'b, Q>(&'a mut self, key: &'b Q) -> EntryRef<'a, 'b, K, Q, V, S, A>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let hash = make_hash::<Q, S>(&self.hash_builder, key);
if let Some(elem) = self.table.find(hash, equivalent_key(key)) {
Expand Down Expand Up @@ -1305,9 +1305,9 @@ where
/// assert_eq!(map.get(&2), None);
/// ```
#[inline]
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
// Avoid `Option::map` because it bloats LLVM IR.
match self.get_inner(k) {
Expand Down Expand Up @@ -1336,9 +1336,9 @@ where
/// assert_eq!(map.get_key_value(&2), None);
/// ```
#[inline]
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
// Avoid `Option::map` because it bloats LLVM IR.
match self.get_inner(k) {
Expand All @@ -1348,9 +1348,9 @@ where
}

#[inline]
fn get_inner<Q: ?Sized>(&self, k: &Q) -> Option<&(K, V)>
fn get_inner<Q>(&self, k: &Q) -> Option<&(K, V)>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
if self.table.is_empty() {
None
Expand Down Expand Up @@ -1384,9 +1384,9 @@ where
/// assert_eq!(map.get_key_value_mut(&2), None);
/// ```
#[inline]
pub fn get_key_value_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<(&K, &mut V)>
pub fn get_key_value_mut<Q>(&mut self, k: &Q) -> Option<(&K, &mut V)>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
// Avoid `Option::map` because it bloats LLVM IR.
match self.get_inner_mut(k) {
Expand Down Expand Up @@ -1415,9 +1415,9 @@ where
/// assert_eq!(map.contains_key(&2), false);
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_inner(k).is_some()
}
Expand Down Expand Up @@ -1446,9 +1446,9 @@ where
/// assert_eq!(map.get_mut(&2), None);
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
// Avoid `Option::map` because it bloats LLVM IR.
match self.get_inner_mut(k) {
Expand All @@ -1458,9 +1458,9 @@ where
}

#[inline]
fn get_inner_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut (K, V)>
fn get_inner_mut<Q>(&mut self, k: &Q) -> Option<&mut (K, V)>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
if self.table.is_empty() {
None
Expand Down Expand Up @@ -1513,9 +1513,9 @@ where
/// ]);
/// assert_eq!(got, None);
/// ```
pub fn get_many_mut<Q: ?Sized, const N: usize>(&mut self, ks: [&Q; N]) -> Option<[&'_ mut V; N]>
pub fn get_many_mut<Q, const N: usize>(&mut self, ks: [&Q; N]) -> Option<[&'_ mut V; N]>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_many_mut_inner(ks).map(|res| res.map(|(_, v)| v))
}
Expand Down Expand Up @@ -1565,12 +1565,12 @@ where
/// ]);
/// assert_eq!(got, None);
/// ```
pub unsafe fn get_many_unchecked_mut<Q: ?Sized, const N: usize>(
pub unsafe fn get_many_unchecked_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> Option<[&'_ mut V; N]>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_many_unchecked_mut_inner(ks)
.map(|res| res.map(|(_, v)| v))
Expand Down Expand Up @@ -1620,12 +1620,12 @@ where
/// ]);
/// assert_eq!(got, None);
/// ```
pub fn get_many_key_value_mut<Q: ?Sized, const N: usize>(
pub fn get_many_key_value_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> Option<[(&'_ K, &'_ mut V); N]>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_many_mut_inner(ks)
.map(|res| res.map(|(k, v)| (&*k, v)))
Expand Down Expand Up @@ -1675,44 +1675,41 @@ where
/// ]);
/// assert_eq!(got, None);
/// ```
pub unsafe fn get_many_key_value_unchecked_mut<Q: ?Sized, const N: usize>(
pub unsafe fn get_many_key_value_unchecked_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> Option<[(&'_ K, &'_ mut V); N]>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_many_unchecked_mut_inner(ks)
.map(|res| res.map(|(k, v)| (&*k, v)))
}

fn get_many_mut_inner<Q: ?Sized, const N: usize>(
&mut self,
ks: [&Q; N],
) -> Option<[&'_ mut (K, V); N]>
fn get_many_mut_inner<Q, const N: usize>(&mut self, ks: [&Q; N]) -> Option<[&'_ mut (K, V); N]>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let hashes = self.build_hashes_inner(ks);
self.table
.get_many_mut(hashes, |i, (k, _)| ks[i].equivalent(k))
}

unsafe fn get_many_unchecked_mut_inner<Q: ?Sized, const N: usize>(
unsafe fn get_many_unchecked_mut_inner<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> Option<[&'_ mut (K, V); N]>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let hashes = self.build_hashes_inner(ks);
self.table
.get_many_unchecked_mut(hashes, |i, (k, _)| ks[i].equivalent(k))
}

fn build_hashes_inner<Q: ?Sized, const N: usize>(&self, ks: [&Q; N]) -> [u64; N]
fn build_hashes_inner<Q, const N: usize>(&self, ks: [&Q; N]) -> [u64; N]
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let mut hashes = [0_u64; N];
for i in 0..N {
Expand Down Expand Up @@ -1892,9 +1889,9 @@ where
/// assert!(map.is_empty());
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
// Avoid `Option::map` because it bloats LLVM IR.
match self.remove_entry(k) {
Expand Down Expand Up @@ -1931,9 +1928,9 @@ where
/// assert!(map.is_empty());
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn remove_entry<Q: ?Sized>(&mut self, k: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
where
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let hash = make_hash::<Q, S>(&self.hash_builder, k);
self.table.remove_entry(hash, equivalent_key(k))
Expand Down Expand Up @@ -2229,10 +2226,10 @@ where
}
}

impl<K, Q: ?Sized, V, S, A> Index<&Q> for HashMap<K, V, S, A>
impl<K, Q, V, S, A> Index<&Q> for HashMap<K, V, S, A>
where
K: Eq + Hash,
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
S: BuildHasher,
A: Allocator,
{
Expand Down Expand Up @@ -3160,10 +3157,10 @@ impl<'a, K, V, S, A: Allocator> RawEntryBuilderMut<'a, K, V, S, A> {
/// ```
#[cfg_attr(feature = "inline-more", inline)]
#[allow(clippy::wrong_self_convention)]
pub fn from_key<Q: ?Sized>(self, k: &Q) -> RawEntryMut<'a, K, V, S, A>
pub fn from_key<Q>(self, k: &Q) -> RawEntryMut<'a, K, V, S, A>
where
S: BuildHasher,
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let hash = make_hash::<Q, S>(&self.map.hash_builder, k);
self.from_key_hashed_nocheck(hash, k)
Expand Down Expand Up @@ -3193,9 +3190,9 @@ impl<'a, K, V, S, A: Allocator> RawEntryBuilderMut<'a, K, V, S, A> {
/// ```
#[inline]
#[allow(clippy::wrong_self_convention)]
pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> RawEntryMut<'a, K, V, S, A>
pub fn from_key_hashed_nocheck<Q>(self, hash: u64, k: &Q) -> RawEntryMut<'a, K, V, S, A>
where
Q: Equivalent<K>,
Q: Equivalent<K> + ?Sized,
{
self.from_hash(hash, equivalent(k))
}
Expand Down Expand Up @@ -3266,10 +3263,10 @@ impl<'a, K, V, S, A: Allocator> RawEntryBuilder<'a, K, V, S, A> {
/// ```
#[cfg_attr(feature = "inline-more", inline)]
#[allow(clippy::wrong_self_convention)]
pub fn from_key<Q: ?Sized>(self, k: &Q) -> Option<(&'a K, &'a V)>
pub fn from_key<Q>(self, k: &Q) -> Option<(&'a K, &'a V)>
where
S: BuildHasher,
Q: Hash + Equivalent<K>,
Q: Hash + Equivalent<K> + ?Sized,
{
let hash = make_hash::<Q, S>(&self.map.hash_builder, k);
self.from_key_hashed_nocheck(hash, k)
Expand Down Expand Up @@ -3297,9 +3294,9 @@ impl<'a, K, V, S, A: Allocator> RawEntryBuilder<'a, K, V, S, A> {
/// ```
#[cfg_attr(feature = "inline-more", inline)]
#[allow(clippy::wrong_self_convention)]
pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> Option<(&'a K, &'a V)>
pub fn from_key_hashed_nocheck<Q>(self, hash: u64, k: &Q) -> Option<(&'a K, &'a V)>
where
Q: Equivalent<K>,
Q: Equivalent<K> + ?Sized,
{
self.from_hash(hash, equivalent(k))
}
Expand Down Expand Up @@ -4409,8 +4406,12 @@ where
Vacant(VacantEntryRef<'a, 'b, K, Q, V, S, A>),
}

impl<K: Borrow<Q>, Q: ?Sized + Debug, V: Debug, S, A: Allocator> Debug
for EntryRef<'_, '_, K, Q, V, S, A>
impl<K, Q, V, S, A> Debug for EntryRef<'_, '_, K, Q, V, S, A>
where
K: Borrow<Q>,
Q: Debug + ?Sized,
V: Debug,
A: Allocator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -4513,8 +4514,12 @@ where
{
}

impl<K: Borrow<Q>, Q: ?Sized + Debug, V: Debug, S, A: Allocator> Debug
for OccupiedEntryRef<'_, '_, K, Q, V, S, A>
impl<K, Q, V, S, A> Debug for OccupiedEntryRef<'_, '_, K, Q, V, S, A>
where
K: Borrow<Q>,
Q: Debug + ?Sized,
V: Debug,
A: Allocator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntryRef")
Expand Down Expand Up @@ -4560,8 +4565,11 @@ pub struct VacantEntryRef<'a, 'b, K, Q: ?Sized, V, S, A: Allocator = Global> {
table: &'a mut HashMap<K, V, S, A>,
}

impl<K: Borrow<Q>, Q: ?Sized + Debug, V, S, A: Allocator> Debug
for VacantEntryRef<'_, '_, K, Q, V, S, A>
impl<K, Q, V, S, A> Debug for VacantEntryRef<'_, '_, K, Q, V, S, A>
where
K: Borrow<Q>,
Q: Debug + ?Sized,
A: Allocator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("VacantEntryRef").field(&self.key()).finish()
Expand Down
Loading

0 comments on commit f540cb7

Please sign in to comment.