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(clippy::needless_pass_by_value)] to drop_inner #200

Merged
merged 1 commit into from
May 6, 2020
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
45 changes: 23 additions & 22 deletions examples/pinned_drop-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,6 @@ const __SCOPE_Struct: () = {
}
}

// It is safe to implement PinnedDrop::drop, but it is not safe to call it.
// This is because destructors can be called multiple times (double dropping
// is unsound: rust-lang/rust#62360).
//
// Ideally, it would be desirable to be able to prohibit manual calls in the
// same way as Drop::drop, but the library cannot. So, by using macros and
// replacing them with private traits, we prevent users from calling
// PinnedDrop::drop.
//
// Users can implement `Drop` safely using `#[pinned_drop]`.
// **Do not call or implement this trait directly.**
impl<T> ::pin_project::__private::PinnedDrop for Struct<'_, T> {
// Since calling it twice on the same object would be UB,
// this method is unsafe.
unsafe fn drop(self: Pin<&mut Self>) {
fn __drop_inner<T>(__self: Pin<&mut Struct<'_, T>>) {
**__self.project().was_dropped = true;
}
__drop_inner(self);
}
}

// Automatically create the appropriate conditional `Unpin` implementation.
//
// See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53.
Expand All @@ -140,4 +118,27 @@ const __SCOPE_Struct: () = {
}
};

// It is safe to implement PinnedDrop::drop, but it is not safe to call it.
// This is because destructors can be called multiple times (double dropping
// is unsound: https://github.com/rust-lang/rust/pull/62360).
//
// Ideally, it would be desirable to be able to prohibit manual calls in the
// same way as Drop::drop, but the library cannot. So, by using macros and
// replacing them with private traits, we prevent users from calling
// PinnedDrop::drop.
//
// Users can implement `Drop` safely using `#[pinned_drop]`.
// **Do not call or implement this trait directly.**
impl<T> ::pin_project::__private::PinnedDrop for Struct<'_, T> {
// Since calling it twice on the same object would be UB,
// this method is unsafe.
unsafe fn drop(self: Pin<&mut Self>) {
#[allow(clippy::needless_pass_by_value)]
fn __drop_inner<T>(__self: Pin<&mut Struct<'_, T>>) {
**__self.project().was_dropped = true;
}
__drop_inner(self);
}
}

fn main() {}
38 changes: 17 additions & 21 deletions pin-project-internal/src/pinned_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,23 @@ fn parse(item: &mut ItemImpl) -> Result<()> {
return Err(error!(item, "not all trait items implemented, missing: `drop`"));
}

for (i, item) in item.items.iter().enumerate() {
match item {
ImplItem::Const(item) => {
return Err(error!(
item,
"const `{}` is not a member of trait `PinnedDrop`", item.ident
));
}
ImplItem::Type(item) => {
return Err(error!(
item,
"type `{}` is not a member of trait `PinnedDrop`", item.ident
));
}
ImplItem::Method(method) => {
parse_method(method)?;
if i != 0 {
return Err(error!(method, "duplicate definitions with name `drop`"));
}
item.items.iter().enumerate().try_for_each(|(i, item)| match item {
ImplItem::Const(item) => {
Err(error!(item, "const `{}` is not a member of trait `PinnedDrop`", item.ident))
}
ImplItem::Type(item) => {
Err(error!(item, "type `{}` is not a member of trait `PinnedDrop`", item.ident))
}
ImplItem::Method(method) => {
parse_method(method)?;
if i == 0 {
Ok(())
} else {
Err(error!(method, "duplicate definitions with name `drop`"))
}
_ => parse_as_empty(&item.to_token_stream())?,
}
}
_ => unreachable!("unexpected ImplItem"),
})?;

expand_item(item);

Expand Down Expand Up @@ -180,6 +174,8 @@ fn expand_item(item: &mut ItemImpl) {
prepend_underscore_to_self(&mut ident.ident);
}
}
// This lint does not warn the receiver.
drop_inner.attrs.push(syn::parse_quote!(#[allow(clippy::needless_pass_by_value)]));
let mut visitor = ReplaceReceiver::new(&item.self_ty);
visitor.visit_signature_mut(&mut drop_inner.sig);
visitor.visit_block_mut(&mut drop_inner.block);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub mod __private {

// It is safe to implement PinnedDrop::drop, but it is not safe to call it.
// This is because destructors can be called multiple times (double dropping
// is unsound: rust-lang/rust#62360).
// is unsound: https://github.com/rust-lang/rust/pull/62360).
//
// Ideally, it would be desirable to be able to prohibit manual calls in the
// same way as Drop::drop, but the library cannot. So, by using macros and
Expand Down
2 changes: 0 additions & 2 deletions tests/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub struct StructPinnedDrop<T, U> {

#[pinned_drop]
impl<T, U> PinnedDrop for StructPinnedDrop<T, U> {
#[allow(clippy::needless_pass_by_value)] // FIXME: https://github.com/rust-lang/rust-clippy/issues/3031?
fn drop(self: Pin<&mut Self>) {}
}

Expand Down Expand Up @@ -79,7 +78,6 @@ pub enum EnumPinnedDrop<T, U> {

#[pinned_drop]
impl<T, U> PinnedDrop for EnumPinnedDrop<T, U> {
#[allow(clippy::needless_pass_by_value)] // FIXME: https://github.com/rust-lang/rust-clippy/issues/3031?
fn drop(self: Pin<&mut Self>) {}
}

Expand Down
1 change: 1 addition & 0 deletions tests/expand/tests/struct/pinned_drop.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const __SCOPE_Struct: () = {
};
impl<T> ::pin_project::__private::PinnedDrop for Struct<'_, T> {
unsafe fn drop(self: Pin<&mut Self>) {
#[allow(clippy::needless_pass_by_value)]
fn __drop_inner<T>(__self: Pin<&mut Struct<'_, T>>) {
**__self.project().was_dropped = true;
}
Expand Down