From 6669567f299bbc89c08b50088c7b574b9e79cfc5 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 1/2] Place unused_imports on unstable reexports --- CHANGELOG.md | 2 ++ example/src/lib.rs | 2 -- src/item_like.rs | 56 +++++++++++++++++++++++++++++++--------------- src/unstable.rs | 9 ++++++-- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58b3f52..2df6398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. ## Unreleased +- Apply `#[allow(unused_imports)]` to unstable reexports. + ## [0.3.6](https://github.com/ratatui/instability/compare/instability-v0.3.5...instability-v0.3.6) - 2025-01-04 ### Other 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..02ec7ad 100644 --- a/src/item_like.rs +++ b/src/item_like.rs @@ -15,34 +15,50 @@ pub trait ItemLike: Stability { fn is_public(&self) -> bool { matches!(self.visibility(), Visibility::Public(_)) } + + fn allowed_lints(&self) -> Vec; } 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[$($allows: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 allowed_lints(&self) -> Vec { + vec![ + $(syn::Ident::new(stringify!($allows), proc_macro2::Span::call_site()),)* + ] } + } +}; +($ty:ty) => { + $crate::item_like::impl_has_visibility!($ty [dead_code]); +}; +($($ty:ty $([$($allows:ident),*])?),+ $(,)?) => { + $( + $crate::item_like::impl_has_visibility!($ty $([$($allows),*])?); )* }; } +pub(crate) use impl_has_visibility; + impl_has_visibility!( syn::ItemType, syn::ItemEnum, @@ -51,7 +67,7 @@ impl_has_visibility!( syn::ItemTrait, syn::ItemConst, syn::ItemStatic, - syn::ItemUse, + syn::ItemUse[unused_imports], ); impl Stability for syn::ItemStruct { @@ -79,6 +95,10 @@ impl ItemLike for syn::ItemStruct { self.vis = visibility; } + + fn allowed_lints(&self) -> Vec { + vec![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..2bbc7d7 100644 --- a/src/unstable.rs +++ b/src/unstable.rs @@ -59,13 +59,18 @@ impl UnstableAttribute { let mut hidden_item = item.clone(); hidden_item.set_visibility(parse_quote! { pub(crate) }); + let allows = item + .allowed_lints() + .into_iter() + .map(|ident| quote! { #[allow(#ident)] }); + 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)] + #(#allows)* #hidden_item } } @@ -383,7 +388,7 @@ mod tests { pub use crate::foo::bar; #[cfg(not(any(doc, feature = "unstable")))] - #[allow(dead_code)] + #[allow(unused_imports)] #[doc = #DEFAULT_DOC] pub(crate) use crate::foo::bar; }; From 0063cc1b208eeb92e5566168bbdd4266c4c8fb45 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Fri, 10 Jan 2025 05:48:12 -0800 Subject: [PATCH 2/2] Tweak the impl for review - renamed impl_has_visibility to impl_item_like - reordered matches so they flow better top to bottom - added back whitespace (makes things a bit easier to read) - added comments to document what's happening - renamed allows to lin - made the syntax for specifying allowed lints like a normal attribute --- CHANGELOG.md | 2 -- src/item_like.rs | 81 ++++++++++++++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2df6398..58b3f52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,6 @@ All notable changes to this project will be documented in this file. ## Unreleased -- Apply `#[allow(unused_imports)]` to unstable reexports. - ## [0.3.6](https://github.com/ratatui/instability/compare/instability-v0.3.5...instability-v0.3.6) - 2025-01-04 ### Other diff --git a/src/item_like.rs b/src/item_like.rs index 02ec7ad..cfc1e1c 100644 --- a/src/item_like.rs +++ b/src/item_like.rs @@ -19,47 +19,59 @@ pub trait ItemLike: Stability { fn allowed_lints(&self) -> Vec; } -macro_rules! impl_has_visibility { -($ty:ty[$($allows:ident),*]) => { - impl Stability for $ty { - fn attrs(&self) -> &[syn::Attribute] { - &self.attrs +/// Implement `ItemLike` for the given type. +/// +/// This makes each of the syn::Item* types implement our `ItemLike` trait to make it possible to +/// work with them in a more uniform way. +/// +/// A single type can be passed to this macro, or multiple types can be passed at once. +/// Each type can be passed with a list of lints that are allowed for that type (defaulting to +/// `dead_code` if not specified). +macro_rules! impl_item_like { + // run impl_item_like for each item in a list of items + ($($(#[allow($($lint:ident),*)])? $ty:ty ),+ ,) => { + $( + impl_item_like!($(#[allow($($lint),*)])? $ty ); + )* + }; + + // run impl_item_like for a single item without any lints + ($ty:ty) => { + impl_item_like!(#[allow(dead_code)] $ty ); + }; + + // Implement `ItemLike` for the given type. + (#[allow($($lint:ident),*)] $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); + } } - 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 allowed_lints(&self) -> Vec { + vec![ + $(syn::Ident::new(stringify!($lint), proc_macro2::Span::call_site()),)* + ] + } } + }; - fn allowed_lints(&self) -> Vec { - vec![ - $(syn::Ident::new(stringify!($allows), proc_macro2::Span::call_site()),)* - ] - } - } -}; -($ty:ty) => { - $crate::item_like::impl_has_visibility!($ty [dead_code]); -}; -($($ty:ty $([$($allows:ident),*])?),+ $(,)?) => { - $( - $crate::item_like::impl_has_visibility!($ty $([$($allows),*])?); - )* -}; } -pub(crate) use impl_has_visibility; - -impl_has_visibility!( +impl_item_like!( syn::ItemType, syn::ItemEnum, syn::ItemFn, @@ -67,7 +79,8 @@ impl_has_visibility!( syn::ItemTrait, syn::ItemConst, syn::ItemStatic, - syn::ItemUse[unused_imports], + #[allow(unused_imports)] + syn::ItemUse, ); impl Stability for syn::ItemStruct {