Skip to content

Commit cf3e49a

Browse files
bugadanijoshka
andauthored
Add #[allow(unused_imports)] lint to unstable reexports (#21)
Reexports marked as unstable no longer need to explicitly allow the unused_imports lint. --------- Co-authored-by: Josh McKinney <[email protected]>
1 parent cf84fbe commit cf3e49a

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

example/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ pub use private::private_function as stable_reexport;
265265
///
266266
/// This re-export is unstable.
267267
#[instability::unstable(feature = "reexport")]
268-
#[allow(unused_imports)]
269268
pub use private::private_function as unstable_reexport;
270269

271270
// 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;
281280
/// section of the unstable_private_function, which will look odd. Consider avoiding re-exporting
282281
/// unstable items like this, and instead only mark the re-export itself as unstable.
283282
#[instability::unstable(feature = "reexport")]
284-
#[allow(unused_imports)]
285283
pub use private::unstable_private_function as unstable_unstable_export;

src/item_like.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,33 @@ pub trait ItemLike: Stability {
1515
fn is_public(&self) -> bool {
1616
matches!(self.visibility(), Visibility::Public(_))
1717
}
18+
19+
fn allowed_lints(&self) -> Vec<syn::Ident>;
1820
}
1921

20-
macro_rules! impl_has_visibility {
21-
($($ty:ty),+ $(,)?) => {
22-
$(
22+
/// Implement `ItemLike` for the given type.
23+
///
24+
/// This makes each of the syn::Item* types implement our `ItemLike` trait to make it possible to
25+
/// work with them in a more uniform way.
26+
///
27+
/// A single type can be passed to this macro, or multiple types can be passed at once.
28+
/// Each type can be passed with a list of lints that are allowed for that type (defaulting to
29+
/// `dead_code` if not specified).
30+
macro_rules! impl_item_like {
31+
// run impl_item_like for each item in a list of items
32+
($($(#[allow($($lint:ident),*)])? $ty:ty ),+ ,) => {
33+
$(
34+
impl_item_like!($(#[allow($($lint),*)])? $ty );
35+
)*
36+
};
37+
38+
// run impl_item_like for a single item without any lints
39+
($ty:ty) => {
40+
impl_item_like!(#[allow(dead_code)] $ty );
41+
};
42+
43+
// Implement `ItemLike` for the given type.
44+
(#[allow($($lint:ident),*)] $ty:ty) => {
2345
impl Stability for $ty {
2446
fn attrs(&self) -> &[syn::Attribute] {
2547
&self.attrs
@@ -38,19 +60,26 @@ macro_rules! impl_has_visibility {
3860
fn set_visibility(&mut self, visibility: Visibility) {
3961
self.vis = visibility;
4062
}
63+
64+
fn allowed_lints(&self) -> Vec<syn::Ident> {
65+
vec![
66+
$(syn::Ident::new(stringify!($lint), proc_macro2::Span::call_site()),)*
67+
]
68+
}
4169
}
42-
)*
43-
};
70+
};
71+
4472
}
4573

46-
impl_has_visibility!(
74+
impl_item_like!(
4775
syn::ItemType,
4876
syn::ItemEnum,
4977
syn::ItemFn,
5078
syn::ItemMod,
5179
syn::ItemTrait,
5280
syn::ItemConst,
5381
syn::ItemStatic,
82+
#[allow(unused_imports)]
5483
syn::ItemUse,
5584
);
5685

@@ -79,6 +108,10 @@ impl ItemLike for syn::ItemStruct {
79108

80109
self.vis = visibility;
81110
}
111+
112+
fn allowed_lints(&self) -> Vec<syn::Ident> {
113+
vec![syn::Ident::new("dead_code", proc_macro2::Span::call_site())]
114+
}
82115
}
83116

84117
impl Stability for syn::ItemImpl {

src/unstable.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ impl UnstableAttribute {
5959
let mut hidden_item = item.clone();
6060
hidden_item.set_visibility(parse_quote! { pub(crate) });
6161

62+
let allows = item
63+
.allowed_lints()
64+
.into_iter()
65+
.map(|ident| quote! { #[allow(#ident)] });
66+
6267
quote! {
6368
#[cfg(any(doc, feature = #feature_flag))]
6469
#[cfg_attr(docsrs, doc(cfg(feature = #feature_flag)))]
6570
#item
6671

6772
#[cfg(not(any(doc, feature = #feature_flag)))]
68-
#[allow(dead_code)]
73+
#(#allows)*
6974
#hidden_item
7075
}
7176
}
@@ -383,7 +388,7 @@ mod tests {
383388
pub use crate::foo::bar;
384389

385390
#[cfg(not(any(doc, feature = "unstable")))]
386-
#[allow(dead_code)]
391+
#[allow(unused_imports)]
387392
#[doc = #DEFAULT_DOC]
388393
pub(crate) use crate::foo::bar;
389394
};

0 commit comments

Comments
 (0)