Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #[allow(unused_imports)] lint to unstable reexports #21

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading