From dcffa4f12c6bbc5330ab2b44d34b6fe3ee209852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 10 Jan 2025 13:11:13 +0100 Subject: [PATCH] Place unused_imports on unstable reexports --- example/src/lib.rs | 2 -- src/item_like.rs | 54 ++++++++++++++++++++++++++++++---------------- src/unstable.rs | 4 +++- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/example/src/lib.rs b/example/src/lib.rs index d2c1489..95ab695 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -265,7 +265,6 @@ pub use private::private_function as stable_reexport; /// /// This re-export is unstable. #[instability::unstable(feature = "reexport")] -#[allow(unused_imports)] pub use private::private_function as unstable_reexport; // This does not work as the unstable_private_function is only public within the crate and cannot @@ -281,5 +280,4 @@ pub use private::private_function as unstable_reexport; /// section of the unstable_private_function, which will look odd. Consider avoiding re-exporting /// unstable items like this, and instead only mark the re-export itself as unstable. #[instability::unstable(feature = "reexport")] -#[allow(unused_imports)] pub use private::unstable_private_function as unstable_unstable_export; diff --git a/src/item_like.rs b/src/item_like.rs index e95d6fb..ca87939 100644 --- a/src/item_like.rs +++ b/src/item_like.rs @@ -15,34 +15,48 @@ pub trait ItemLike: Stability { fn is_public(&self) -> bool { matches!(self.visibility(), Visibility::Public(_)) } + + fn unused_category(&self) -> syn::Ident; } macro_rules! impl_has_visibility { -($($ty:ty),+ $(,)?) => { - $( - impl Stability for $ty { - fn attrs(&self) -> &[syn::Attribute] { - &self.attrs - } - - fn push_attr(&mut self, attr: syn::Attribute) { - self.attrs.push(attr); - } +($ty:ty[$unused:ident]) => { + impl Stability for $ty { + fn attrs(&self) -> &[syn::Attribute] { + &self.attrs + } + + fn push_attr(&mut self, attr: syn::Attribute) { + self.attrs.push(attr); + } + } + + impl ItemLike for $ty { + fn visibility(&self) -> &Visibility { + &self.vis } - impl ItemLike for $ty { - fn visibility(&self) -> &Visibility { - &self.vis - } + fn set_visibility(&mut self, visibility: Visibility) { + self.vis = visibility; + } - fn set_visibility(&mut self, visibility: Visibility) { - self.vis = visibility; - } + fn unused_category(&self) -> syn::Ident { + syn::Ident::new(stringify!($unused), proc_macro2::Span::call_site()) } + } +}; +($ty:ty) => { + $crate::item_like::impl_has_visibility!($ty [dead_code]); +}; +($($ty:ty $([$unused:ident])?),+ $(,)?) => { + $( + $crate::item_like::impl_has_visibility!($ty $([$unused])?); )* }; } +pub(crate) use impl_has_visibility; + impl_has_visibility!( syn::ItemType, syn::ItemEnum, @@ -51,7 +65,7 @@ impl_has_visibility!( syn::ItemTrait, syn::ItemConst, syn::ItemStatic, - syn::ItemUse, + syn::ItemUse[unused_imports], ); impl Stability for syn::ItemStruct { @@ -79,6 +93,10 @@ impl ItemLike for syn::ItemStruct { self.vis = visibility; } + + fn unused_category(&self) -> syn::Ident { + syn::Ident::new("dead_code", proc_macro2::Span::call_site()) + } } impl Stability for syn::ItemImpl { diff --git a/src/unstable.rs b/src/unstable.rs index 70bd595..4f7818c 100644 --- a/src/unstable.rs +++ b/src/unstable.rs @@ -59,13 +59,15 @@ impl UnstableAttribute { let mut hidden_item = item.clone(); hidden_item.set_visibility(parse_quote! { pub(crate) }); + let unused = item.unused_category(); + quote! { #[cfg(any(doc, feature = #feature_flag))] #[cfg_attr(docsrs, doc(cfg(feature = #feature_flag)))] #item #[cfg(not(any(doc, feature = #feature_flag)))] - #[allow(dead_code)] + #[allow(#unused)] #hidden_item } }