Skip to content

Commit

Permalink
Suppress various lints in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 6, 2025
1 parent 3fa6e92 commit 2fb3d56
Show file tree
Hide file tree
Showing 43 changed files with 1,538 additions and 1,121 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Suppress `unnameable_types`, `clippy::absolute_paths`, `clippy::min_ident_chars`, `clippy::pub_with_shorthand`, `clippy::single_call_fn`, `clippy::single_char_lifetime_names` lints in generated code.

## [1.1.7] - 2024-10-24

- Work around an issue on negative_impls that allows unsound overlapping `Unpin` implementations. ([#357](https://github.com/taiki-e/pin-project/pull/357))
Expand Down
85 changes: 47 additions & 38 deletions pin-project-internal/src/pin_project/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ impl GenerateTokens {
// - https://github.com/rust-lang/rust/issues/63281
// - https://github.com/taiki-e/pin-project/pull/53#issuecomment-525906867
// - https://github.com/taiki-e/pin-project/pull/70
#allowed_lints
#[allow(unused_qualifications)]
#[allow(clippy::needless_lifetimes)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
#[allow(
unused_qualifications,
#allowed_lints
clippy::needless_lifetimes,
clippy::semicolon_if_nothing_returned,
clippy::use_self,
clippy::used_underscore_binding
)]
const _: () = {
#[allow(unused_extern_crates)]
extern crate pin_project as _pin_project;
Expand All @@ -107,52 +109,59 @@ impl GenerateTokens {
/// Returns attributes that should be applied to all generated code.
fn global_allowed_lints() -> TokenStream {
quote! {
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)] // https://github.com/rust-lang/rust/issues/60993
#[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
#[allow(unreachable_pub)] // This lint warns `pub` field in private struct.
#[allow(unused_tuple_struct_fields)]
deprecated,
explicit_outlives_requirements, // https://github.com/rust-lang/rust/issues/60993
single_use_lifetimes, // https://github.com/rust-lang/rust/issues/55058
unreachable_pub, // This lint warns `pub` field in private struct.
unused_tuple_struct_fields,
// This lint warns of `clippy::*` generated by external macros.
// We allow this lint for compatibility with older compilers.
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)] // This lint warns `pub(crate)` field in private struct.
#[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326
clippy::unknown_clippy_lints,
clippy::absolute_paths,
clippy::min_ident_chars,
clippy::pattern_type_mismatch,
clippy::pub_with_shorthand,
clippy::redundant_pub_crate, // This lint warns `pub(crate)` field in private struct.
clippy::single_char_lifetime_names,
clippy::type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/4326
}
}

/// Returns attributes used on projected types.
fn proj_allowed_lints(cx: &Context<'_>) -> (TokenStream, TokenStream, TokenStream) {
let large_enum_variant = if cx.kind == Enum {
Some(quote! {
#[allow(variant_size_differences)]
#[allow(clippy::large_enum_variant)]
})
} else {
None
};
let global_allowed_lints = global_allowed_lints();
let proj_mut_allowed_lints = if cx.project { Some(&global_allowed_lints) } else { None };
let proj_mut = quote! {
#proj_mut_allowed_lints
#[allow(dead_code)] // This lint warns unused fields/variants.
#[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`.
#[allow(clippy::missing_docs_in_private_items)]
#[allow(
dead_code, // This lint warns unused fields/variants.
#proj_mut_allowed_lints
clippy::missing_docs_in_private_items,
clippy::mut_mut // This lint warns `&mut &mut <ty>`.
)]
};
let proj_ref_allowed_lints = if cx.project_ref { Some(&global_allowed_lints) } else { None };
let proj_ref = quote! {
#proj_ref_allowed_lints
#[allow(dead_code)] // This lint warns unused fields/variants.
#[allow(clippy::ref_option_ref)] // This lint warns `&Option<&<ty>>`.
#[allow(clippy::missing_docs_in_private_items)]
#[allow(
dead_code, // This lint warns unused fields/variants.
#proj_ref_allowed_lints
clippy::missing_docs_in_private_items,
clippy::ref_option_ref // This lint warns `&Option<&<ty>>`.
)]
};
let proj_own_allowed_lints =
if cx.project_replace.ident().is_some() { Some(&global_allowed_lints) } else { None };
let variant_size_differences = if cx.kind == Enum {
Some(quote! { variant_size_differences, clippy::large_enum_variant, })
} else {
None
};
let proj_own = quote! {
#proj_own_allowed_lints
#[allow(dead_code)] // This lint warns unused fields/variants.
#[allow(clippy::missing_docs_in_private_items)]
#large_enum_variant
#[allow(
dead_code, // This lint warns unused fields/variants.
#proj_own_allowed_lints
#variant_size_differences
clippy::missing_docs_in_private_items
)]
};
(proj_mut, proj_ref, proj_own)
}
Expand Down Expand Up @@ -808,7 +817,7 @@ fn make_unpin_impl(cx: &Context<'_>) -> TokenStream {
// `__UnpinStruct` type must also be public.
// However, we ensure that the user can never actually reference
// this 'public' type by creating this type in the inside of `const`.
#[allow(missing_debug_implementations)]
#[allow(missing_debug_implementations, unnameable_types)]
#vis struct #struct_ident #proj_generics #ty_where_clause {
__pin_project_use_generics: _pin_project::__private::AlwaysUnpin<
#lifetime, (#(_pin_project::__private::PhantomData<#type_params>),*)
Expand Down Expand Up @@ -975,14 +984,14 @@ fn make_proj_impl(
let mut project_replace = cx.project_replace.span().map(|span| {
// It is enough to only set the span of the signature.
let sig = quote_spanned! { span =>
#allow_dead_code
#[inline]
#vis fn project_replace(
self: _pin_project::__private::Pin<&mut Self>,
__replacement: Self,
) -> #proj_own_ident #orig_ty_generics
};
quote! {
#allow_dead_code
#[inline]
#sig {
unsafe {
let __self_ptr: *mut Self = self.get_unchecked_mut();
Expand Down
9 changes: 8 additions & 1 deletion pin-project-internal/src/pinned_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ pub(crate) fn attribute(args: &TokenStream, mut input: ItemImpl) -> TokenStream
}
tokens
} else {
input.attrs.push(parse_quote!(#[allow(unused_qualifications)])); // https://github.com/rust-lang/rust/issues/122519
input.attrs.push(parse_quote!(#[allow(
unused_qualifications, // https://github.com/rust-lang/rust/issues/122519
// This lint warns of `clippy::*` generated by external macros.
// We allow this lint for compatibility with older compilers.
clippy::unknown_clippy_lints,
clippy::absolute_paths
)]));
input.into_token_stream()
}
}
Expand Down Expand Up @@ -198,6 +204,7 @@ fn expand_impl(item: &mut ItemImpl) {
let ident = format_ident!("__drop_inner");
// Add a dummy `__drop_inner` function to prevent users call outer `__drop_inner`.
drop_inner.block.stmts.insert(0, parse_quote!(fn #ident() {}));
drop_inner.attrs.push(parse_quote!(#[allow(clippy::single_call_fn)]));
drop_inner.sig.ident = ident;
drop_inner.sig.generics = item.generics.clone();
let receiver = drop_inner.sig.receiver().expect("drop() should have a receiver").clone();
Expand Down
96 changes: 57 additions & 39 deletions tests/expand/default/enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ enum Enum<T, U> {
Tuple(#[pin] T, U),
Unit,
}
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(unused_tuple_struct_fields)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::missing_docs_in_private_items)]
#[allow(
dead_code,
deprecated,
explicit_outlives_requirements,
single_use_lifetimes,
unreachable_pub,
unused_tuple_struct_fields,
clippy::unknown_clippy_lints,
clippy::absolute_paths,
clippy::min_ident_chars,
clippy::pattern_type_mismatch,
clippy::pub_with_shorthand,
clippy::redundant_pub_crate,
clippy::single_char_lifetime_names,
clippy::type_repetition_in_bounds,
clippy::missing_docs_in_private_items,
clippy::mut_mut
)]
enum EnumProj<'pin, T, U>
where
Enum<T, U>: 'pin,
Expand All @@ -28,18 +34,24 @@ where
Tuple(::pin_project::__private::Pin<&'pin mut (T)>, &'pin mut (U)),
Unit,
}
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(unused_tuple_struct_fields)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::missing_docs_in_private_items)]
#[allow(
dead_code,
deprecated,
explicit_outlives_requirements,
single_use_lifetimes,
unreachable_pub,
unused_tuple_struct_fields,
clippy::unknown_clippy_lints,
clippy::absolute_paths,
clippy::min_ident_chars,
clippy::pattern_type_mismatch,
clippy::pub_with_shorthand,
clippy::redundant_pub_crate,
clippy::single_char_lifetime_names,
clippy::type_repetition_in_bounds,
clippy::missing_docs_in_private_items,
clippy::ref_option_ref
)]
enum EnumProjRef<'pin, T, U>
where
Enum<T, U>: 'pin,
Expand All @@ -48,20 +60,26 @@ where
Tuple(::pin_project::__private::Pin<&'pin (T)>, &'pin (U)),
Unit,
}
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(unused_tuple_struct_fields)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(unused_qualifications)]
#[allow(clippy::needless_lifetimes)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
#[allow(
unused_qualifications,
deprecated,
explicit_outlives_requirements,
single_use_lifetimes,
unreachable_pub,
unused_tuple_struct_fields,
clippy::unknown_clippy_lints,
clippy::absolute_paths,
clippy::min_ident_chars,
clippy::pattern_type_mismatch,
clippy::pub_with_shorthand,
clippy::redundant_pub_crate,
clippy::single_char_lifetime_names,
clippy::type_repetition_in_bounds,
clippy::needless_lifetimes,
clippy::semicolon_if_nothing_returned,
clippy::use_self,
clippy::used_underscore_binding
)]
const _: () = {
#[allow(unused_extern_crates)]
extern crate pin_project as _pin_project;
Expand Down Expand Up @@ -114,7 +132,7 @@ const _: () = {
}
}
}
#[allow(missing_debug_implementations)]
#[allow(missing_debug_implementations, unnameable_types)]
struct __Enum<'pin, T, U> {
__pin_project_use_generics: _pin_project::__private::AlwaysUnpin<
'pin,
Expand Down
44 changes: 23 additions & 21 deletions tests/expand/default/struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,38 @@ struct Struct<T, U> {
pinned: T,
unpinned: U,
}
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(unused_tuple_struct_fields)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(unused_qualifications)]
#[allow(clippy::needless_lifetimes)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
#[allow(
unused_qualifications,
deprecated,
explicit_outlives_requirements,
single_use_lifetimes,
unreachable_pub,
unused_tuple_struct_fields,
clippy::unknown_clippy_lints,
clippy::absolute_paths,
clippy::min_ident_chars,
clippy::pattern_type_mismatch,
clippy::pub_with_shorthand,
clippy::redundant_pub_crate,
clippy::single_char_lifetime_names,
clippy::type_repetition_in_bounds,
clippy::needless_lifetimes,
clippy::semicolon_if_nothing_returned,
clippy::use_self,
clippy::used_underscore_binding
)]
const _: () = {
#[allow(unused_extern_crates)]
extern crate pin_project as _pin_project;
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::missing_docs_in_private_items)]
#[allow(dead_code, clippy::missing_docs_in_private_items, clippy::mut_mut)]
struct __StructProjection<'pin, T, U>
where
Struct<T, U>: 'pin,
{
pinned: ::pin_project::__private::Pin<&'pin mut (T)>,
unpinned: &'pin mut (U),
}
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::missing_docs_in_private_items)]
#[allow(dead_code, clippy::missing_docs_in_private_items, clippy::ref_option_ref)]
struct __StructProjectionRef<'pin, T, U>
where
Struct<T, U>: 'pin,
Expand Down Expand Up @@ -76,7 +78,7 @@ const _: () = {
let _ = &this.pinned;
let _ = &this.unpinned;
}
#[allow(missing_debug_implementations)]
#[allow(missing_debug_implementations, unnameable_types)]
struct __Struct<'pin, T, U> {
__pin_project_use_generics: _pin_project::__private::AlwaysUnpin<
'pin,
Expand Down
Loading

0 comments on commit 2fb3d56

Please sign in to comment.