Skip to content

Commit

Permalink
Add #[allow(unused_imports)] lint to unstable reexports (#21)
Browse files Browse the repository at this point in the history
Reexports marked as unstable no longer need to explicitly allow
the unused_imports lint.

---------

Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
  • Loading branch information
bugadani and joshka authored Jan 10, 2025
1 parent cf84fbe commit cf3e49a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
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;
45 changes: 39 additions & 6 deletions src/item_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,33 @@ 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),+ $(,)?) => {
$(
/// 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
Expand All @@ -38,19 +60,26 @@ macro_rules! impl_has_visibility {
fn set_visibility(&mut self, visibility: Visibility) {
self.vis = visibility;
}

fn allowed_lints(&self) -> Vec<syn::Ident> {
vec![
$(syn::Ident::new(stringify!($lint), proc_macro2::Span::call_site()),)*
]
}
}
)*
};
};

}

impl_has_visibility!(
impl_item_like!(
syn::ItemType,
syn::ItemEnum,
syn::ItemFn,
syn::ItemMod,
syn::ItemTrait,
syn::ItemConst,
syn::ItemStatic,
#[allow(unused_imports)]
syn::ItemUse,
);

Expand Down Expand Up @@ -79,6 +108,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 cf3e49a

Please sign in to comment.