Skip to content

Commit

Permalink
Handle unused_import lint warnings around macro_rules re-exports
Browse files Browse the repository at this point in the history
The bug rust#78894 in rustc is now a problem, since more warnings are
issued.

rust-lang/rust#78894
rust-lang/rust#117772
  • Loading branch information
jonasbb committed Feb 19, 2024
1 parent da57d86 commit 640e4aa
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 106 deletions.
2 changes: 1 addition & 1 deletion serde_with/src/de/duplicates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::impls::{foreach_map, foreach_set};
use super::impls::macros::{foreach_map, foreach_set};
use crate::{
duplicate_key_impls::{
DuplicateInsertsFirstWinsMap, DuplicateInsertsLastWinsSet, PreventDuplicateInsertsMap,
Expand Down
170 changes: 90 additions & 80 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) use self::macros::*;
use crate::{formats::*, prelude::*};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
Expand All @@ -12,89 +13,98 @@ use indexmap_2::{IndexMap as IndexMap2, IndexSet as IndexSet2};
#[cfg(feature = "alloc")]
type BoxedSlice<T> = Box<[T]>;

macro_rules! foreach_map {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeMap<K: Ord, V>, (|_size| BTreeMap::new()));
#[cfg(feature = "std")]
$m!(
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownMap014<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashbrownMap014::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexMap2<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap2::with_capacity_and_hasher(size, Default::default()))
);
};
}
pub(crate) use foreach_map;
pub(crate) mod macros {
// The unused_import lint has false-positives around macros
// https://github.com/rust-lang/rust/issues/78894
#![allow(unused_import)]

macro_rules! foreach_set {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeSet<T: Ord>, (|_| BTreeSet::new()), insert);
#[cfg(feature = "std")]
$m!(
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownSet014<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashbrownSet014::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexSet2<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet2::with_capacity_and_hasher(size, S::default())),
insert
);
};
}
pub(crate) use foreach_set;
macro_rules! foreach_map {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeMap<K: Ord, V>, (|_size| BTreeMap::new()));
#[cfg(feature = "std")]
$m!(
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownMap014<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashbrownMap014::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexMap2<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap2::with_capacity_and_hasher(size, Default::default()))
);
};
}

macro_rules! foreach_seq {
($m:ident) => {
foreach_set!($m);
macro_rules! foreach_set {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeSet<T: Ord>, (|_| BTreeSet::new()), insert);
#[cfg(feature = "std")]
$m!(
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownSet014<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashbrownSet014::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexSet2<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet2::with_capacity_and_hasher(size, S::default())),
insert
);
};
}

#[cfg(feature = "alloc")]
$m!(
BinaryHeap<T: Ord>,
(|size| BinaryHeap::with_capacity(size)),
push
);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(LinkedList<T>, (|_| LinkedList::new()), push_back);
#[cfg(feature = "alloc")]
$m!(Vec<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(
VecDeque<T>,
(|size| VecDeque::with_capacity(size)),
push_back
);
};
macro_rules! foreach_seq {
($m:ident) => {
foreach_set!($m);

#[cfg(feature = "alloc")]
$m!(
BinaryHeap<T: Ord>,
(|size| BinaryHeap::with_capacity(size)),
push
);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(LinkedList<T>, (|_| LinkedList::new()), push_back);
#[cfg(feature = "alloc")]
$m!(Vec<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(
VecDeque<T>,
(|size| VecDeque::with_capacity(size)),
push_back
);
};
}

// Make the macros available to the rest of the crate
pub(crate) use foreach_map;
pub(crate) use foreach_seq;
pub(crate) use foreach_set;
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/ser/duplicates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::impls::{foreach_map, foreach_set};
use super::impls::macros::{foreach_map, foreach_set};
use crate::prelude::*;
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
Expand Down
59 changes: 35 additions & 24 deletions serde_with/src/ser/impls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) use self::macros::*;
use crate::{formats::Strictness, prelude::*};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
Expand All @@ -13,7 +14,12 @@ use indexmap_2::{IndexMap as IndexMap2, IndexSet as IndexSet2};
type BoxedSlice<T> = Box<[T]>;
type Slice<T> = [T];

macro_rules! foreach_map {
pub(crate) mod macros {
// The unused_import lint has false-positives around macros
// https://github.com/rust-lang/rust/issues/78894
#![allow(unused_import)]

macro_rules! foreach_map {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeMap<K, V>);
Expand All @@ -27,9 +33,8 @@ macro_rules! foreach_map {
$m!(IndexMap2<K, V, H: Sized>);
};
}
pub(crate) use foreach_map;

macro_rules! foreach_set {
macro_rules! foreach_set {
($m:ident, $T:tt) => {
#[cfg(feature = "alloc")]
$m!(BTreeSet<$T>);
Expand All @@ -46,28 +51,34 @@ macro_rules! foreach_set {
foreach_set!($m, T);
};
}
pub(crate) use foreach_set;

macro_rules! foreach_seq {
($m:ident, $T:tt) => {
foreach_set!($m, $T);

$m!(Slice<$T>);

#[cfg(feature = "alloc")]
$m!(BinaryHeap<$T>);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<$T>);
#[cfg(feature = "alloc")]
$m!(LinkedList<$T>);
#[cfg(feature = "alloc")]
$m!(Vec<$T>);
#[cfg(feature = "alloc")]
$m!(VecDeque<$T>);
};
($m:ident) => {
foreach_seq!($m, T);
};
macro_rules! foreach_seq {
($m:ident, $T:tt) => {
foreach_set!($m, $T);

$m!(Slice<$T>);

#[cfg(feature = "alloc")]
$m!(BinaryHeap<$T>);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<$T>);
#[cfg(feature = "alloc")]
$m!(LinkedList<$T>);
#[cfg(feature = "alloc")]
$m!(Vec<$T>);
#[cfg(feature = "alloc")]
$m!(VecDeque<$T>);
};
($m:
ident) => {
foreach_seq!($m, T);
};
}

// Make the macros available to the rest of the crate
pub(crate) use foreach_map;
pub(crate) use foreach_seq;
pub(crate) use foreach_set;
}

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 640e4aa

Please sign in to comment.