Skip to content

Commit

Permalink
Merge #331
Browse files Browse the repository at this point in the history
331: Clean up lint-related code generation r=taiki-e a=taiki-e

- Suppress `clippy::use_self` and `clippy::type_repetition_in_bounds` lints in generated code.
- Reduce the amount of code generated.

Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e committed Jul 21, 2021
2 parents ad6ffe7 + 2b9887b commit e2cbc63
Show file tree
Hide file tree
Showing 44 changed files with 177 additions and 438 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- [Suppress `clippy::use_self` and `clippy::type_repetition_in_bounds` lints in generated code.](https://github.com/taiki-e/pin-project/pull/331)

## [1.0.7] - 2021-04-16

- [Fix compile error when using `self::` as prefix of path inside `#[pinned_drop]` impl.](https://github.com/taiki-e/pin-project/pull/326)
Expand Down
6 changes: 5 additions & 1 deletion examples/enum-default-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// ```

#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)]
#![allow(clippy::needless_lifetimes, clippy::just_underscores_and_digits)]
#![allow(
clippy::needless_lifetimes,
clippy::just_underscores_and_digits,
clippy::used_underscore_binding
)]

use pin_project::pin_project;

Expand Down
2 changes: 1 addition & 1 deletion examples/pinned_drop-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// ```

#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)]
#![allow(clippy::needless_lifetimes)]
#![allow(clippy::needless_lifetimes, clippy::mut_mut)]

use std::pin::Pin;

Expand Down
26 changes: 15 additions & 11 deletions pin-project-internal/src/pin_project/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl GenerateTokens {
// * https://github.com/taiki-e/pin-project/pull/70
#allowed_lints
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
#scoped
Expand All @@ -112,12 +113,13 @@ fn global_allowed_lints() -> TokenStream {
#[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
}
}

/// Returns attributes used on projected types.
fn proj_allowed_lints(kind: TypeKind) -> (TokenStream, TokenStream, TokenStream) {
let large_enum_variant = if kind == Enum {
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)]
Expand All @@ -126,20 +128,22 @@ fn proj_allowed_lints(kind: TypeKind) -> (TokenStream, TokenStream, TokenStream)
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! {
#global_allowed_lints
#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::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326}
};
let proj_ref_allowed_lints = if cx.project_ref { Some(&global_allowed_lints) } else { None };
let proj_ref = quote! {
#global_allowed_lints
#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::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326
};
let proj_own_allowed_lints =
if cx.project_replace.ident().is_some() { Some(&global_allowed_lints) } else { None };
let proj_own = quote! {
#global_allowed_lints
#proj_own_allowed_lints
#[allow(dead_code)] // This lint warns unused fields/variants.
#large_enum_variant
};
Expand Down Expand Up @@ -368,7 +372,7 @@ fn parse_struct<'a>(
Fields::Unit => unreachable!(),
};

let (proj_attrs, proj_ref_attrs, proj_own_attrs) = proj_allowed_lints(cx.kind);
let (proj_attrs, proj_ref_attrs, proj_own_attrs) = proj_allowed_lints(cx);
generate.extend(cx.project, quote! {
#proj_attrs
#vis struct #proj_ident #proj_generics #where_clause_fields
Expand Down Expand Up @@ -441,7 +445,7 @@ fn parse_enum<'a>(
let proj_generics = &cx.proj.generics;
let proj_where_clause = &cx.proj.where_clause;

let (proj_attrs, proj_ref_attrs, proj_own_attrs) = proj_allowed_lints(cx.kind);
let (proj_attrs, proj_ref_attrs, proj_own_attrs) = proj_allowed_lints(cx);
if cx.project {
generate.extend(true, quote! {
#proj_attrs
Expand Down Expand Up @@ -611,7 +615,7 @@ fn visit_fields<'a>(
let proj_own_fields = surround(delim, proj_own_fields);

let proj_move = Group::new(delim, proj_move);
let proj_own_body = proj_own_body(cx, variant_ident, Some(proj_move), &pinned_bindings);
let proj_own_body = proj_own_body(cx, variant_ident, Some(&proj_move), &pinned_bindings);

Ok(ProjectedFields {
proj_pat,
Expand All @@ -629,7 +633,7 @@ fn visit_fields<'a>(
fn proj_own_body(
cx: &Context<'_>,
variant_ident: Option<&Ident>,
proj_move: Option<Group>,
proj_move: Option<&Group>,
pinned_fields: &[Ident],
) -> TokenStream {
let ident = &cx.proj.own_ident;
Expand Down
1 change: 1 addition & 0 deletions tests/auxiliary/macro/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(nightly)]
#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(clippy::pedantic)]

use proc_macro::TokenStream;
use quote::{format_ident, quote, ToTokens};
Expand Down
6 changes: 4 additions & 2 deletions tests/expand/default/enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ enum Enum<T, U> {
#[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::type_repetition_in_bounds)]
enum EnumProj<'pin, T, U>
where
Enum<T, U>: 'pin,
Expand All @@ -39,9 +39,9 @@ where
#[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::type_repetition_in_bounds)]
enum EnumProjRef<'pin, T, U>
where
Enum<T, U>: 'pin,
Expand All @@ -61,7 +61,9 @@ where
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
impl<T, U> Enum<T, U> {
Expand Down
20 changes: 2 additions & 18 deletions tests/expand/default/struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,22 @@ struct Struct<T, U> {
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::type_repetition_in_bounds)]
struct __StructProjection<'pin, T, U>
where
Struct<T, U>: 'pin,
{
pinned: ::pin_project::__private::Pin<&'pin mut (T)>,
unpinned: &'pin mut (U),
}
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::type_repetition_in_bounds)]
struct __StructProjectionRef<'pin, T, U>
where
Struct<T, U>: 'pin,
Expand Down
20 changes: 2 additions & 18 deletions tests/expand/default/tuple_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,21 @@ struct TupleStruct<T, U>(#[pin] T, U);
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::type_repetition_in_bounds)]
struct __TupleStructProjection<'pin, T, U>(
::pin_project::__private::Pin<&'pin mut (T)>,
&'pin mut (U),
)
where
TupleStruct<T, U>: 'pin;
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::type_repetition_in_bounds)]
struct __TupleStructProjectionRef<'pin, T, U>(
::pin_project::__private::Pin<&'pin (T)>,
&'pin (U),
Expand Down
7 changes: 5 additions & 2 deletions tests/expand/multifields/enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ enum Enum<T, U> {
#[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::type_repetition_in_bounds)]
enum EnumProj<'pin, T, U>
where
Enum<T, U>: 'pin,
Expand All @@ -49,9 +49,9 @@ where
#[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::type_repetition_in_bounds)]
enum EnumProjRef<'pin, T, U>
where
Enum<T, U>: 'pin,
Expand All @@ -78,6 +78,7 @@ where
#[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(variant_size_differences)]
#[allow(clippy::large_enum_variant)]
Expand All @@ -104,7 +105,9 @@ enum EnumProjOwn<T, U> {
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
impl<T, U> Enum<T, U> {
Expand Down
28 changes: 2 additions & 26 deletions tests/expand/multifields/struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ struct Struct<T, U> {
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::type_repetition_in_bounds)]
struct __StructProjection<'pin, T, U>
where
Struct<T, U>: 'pin,
Expand All @@ -39,17 +32,8 @@ const _: () = {
unpinned1: &'pin mut (U),
unpinned2: &'pin mut (U),
}
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::type_repetition_in_bounds)]
struct __StructProjectionRef<'pin, T, U>
where
Struct<T, U>: 'pin,
Expand All @@ -59,14 +43,6 @@ const _: () = {
unpinned1: &'pin (U),
unpinned2: &'pin (U),
}
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
struct __StructProjectionOwned<T, U> {
pinned1: ::pin_project::__private::PhantomData<T>,
Expand Down
28 changes: 2 additions & 26 deletions tests/expand/multifields/tuple_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ struct TupleStruct<T, U>(#[pin] T, #[pin] T, U, U);
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::use_self)]
#[allow(clippy::used_underscore_binding)]
const _: () = {
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::type_repetition_in_bounds)]
struct __TupleStructProjection<'pin, T, U>(
::pin_project::__private::Pin<&'pin mut (T)>,
::pin_project::__private::Pin<&'pin mut (T)>,
Expand All @@ -31,17 +24,8 @@ const _: () = {
)
where
TupleStruct<T, U>: 'pin;
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::type_repetition_in_bounds)]
struct __TupleStructProjectionRef<'pin, T, U>(
::pin_project::__private::Pin<&'pin (T)>,
::pin_project::__private::Pin<&'pin (T)>,
Expand All @@ -50,14 +34,6 @@ const _: () = {
)
where
TupleStruct<T, U>: 'pin;
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
#[allow(dead_code)]
struct __TupleStructProjectionOwned<T, U>(
::pin_project::__private::PhantomData<T>,
Expand Down
Loading

0 comments on commit e2cbc63

Please sign in to comment.