Skip to content

unsafe_derive_deserialize: do not consider pin!() as unsafe #15137

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) -> Self::Result {
if let ExprKind::Block(block, _) = expr.kind
&& block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
&& block
.span
.source_callee()
.and_then(|expr| expr.macro_def_id)
.is_none_or(|did| {
(self.cx.tcx.crate_name(did.krate), self.cx.tcx.item_name(did)) != (sym::core, sym::pin)
})
Comment on lines +437 to +441
Copy link

@danielhenrymantilla danielhenrymantilla Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems oddly specific to pin!; what about something more general such as:

Suggested change
.source_callee()
.and_then(|expr| expr.macro_def_id)
.is_none_or(|did| {
(self.cx.tcx.crate_name(did.krate), self.cx.tcx.item_name(did)) != (sym::core, sym::pin)
})
.from_expansion()
.not()

{
return ControlFlow::Break(());
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/unsafe_derive_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ impl H {
}

fn main() {}

mod issue15120 {
#[derive(serde::Deserialize)]
struct Foo;

impl Foo {
fn foo(&self) {
// Do not lint if `unsafe` comes from the `core::pin::pin!()` macro
std::pin::pin!(());
}
}
}
Loading