Skip to content

Commit

Permalink
Place unused_imports on unstable reexports
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Jan 10, 2025
1 parent cf84fbe commit 6669567
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
56 changes: 38 additions & 18 deletions src/item_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,50 @@ pub trait ItemLike: Stability {
fn is_public(&self) -> bool {
matches!(self.visibility(), Visibility::Public(_))
}

fn allowed_lints(&self) -> Vec<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[$($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<syn::Ident> {
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,
Expand All @@ -51,7 +67,7 @@ impl_has_visibility!(
syn::ItemTrait,
syn::ItemConst,
syn::ItemStatic,
syn::ItemUse,
syn::ItemUse[unused_imports],
);

impl Stability for syn::ItemStruct {
Expand Down Expand Up @@ -79,6 +95,10 @@ impl ItemLike for syn::ItemStruct {

self.vis = visibility;
}

fn allowed_lints(&self) -> Vec<syn::Ident> {
vec![syn::Ident::new("dead_code", proc_macro2::Span::call_site())]
}
}

impl Stability for syn::ItemImpl {
Expand Down
9 changes: 7 additions & 2 deletions src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 6669567

Please sign in to comment.