Skip to content

Commit ad97abc

Browse files
unsafe_derive_deserialize: do not consider pin!() as unsafe (rust-lang#15137)
In Rust 1.88, the `pin!()` macro uses `unsafe` and triggers `unsafe_derive_deserialize`. Fixes rust-lang/rust-clippy#15120 changelog: [`unsafe_derive_deserialize`]: do not trigger because of the standard library's `pin!()` macro
2 parents 21943a9 + b49e360 commit ad97abc

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

clippy_lints/src/derive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
432432
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) -> Self::Result {
433433
if let ExprKind::Block(block, _) = expr.kind
434434
&& block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
435+
&& block
436+
.span
437+
.source_callee()
438+
.and_then(|expr| expr.macro_def_id)
439+
.is_none_or(|did| !self.cx.tcx.is_diagnostic_item(sym::pin_macro, did))
435440
{
436441
return ControlFlow::Break(());
437442
}

tests/ui/unsafe_derive_deserialize.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,32 @@ impl H {
8282
}
8383

8484
fn main() {}
85+
86+
mod issue15120 {
87+
macro_rules! uns {
88+
($e:expr) => {
89+
unsafe { $e }
90+
};
91+
}
92+
93+
#[derive(serde::Deserialize)]
94+
struct Foo;
95+
96+
impl Foo {
97+
fn foo(&self) {
98+
// Do not lint if `unsafe` comes from the `core::pin::pin!()` macro
99+
std::pin::pin!(());
100+
}
101+
}
102+
103+
//~v unsafe_derive_deserialize
104+
#[derive(serde::Deserialize)]
105+
struct Bar;
106+
107+
impl Bar {
108+
fn bar(&self) {
109+
// Lint if `unsafe` comes from the another macro
110+
_ = uns!(42);
111+
}
112+
}
113+
}

tests/ui/unsafe_derive_deserialize.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,14 @@ LL | #[derive(Deserialize)]
3636
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html
3737
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
3838

39-
error: aborting due to 4 previous errors
39+
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
40+
--> tests/ui/unsafe_derive_deserialize.rs:104:14
41+
|
42+
LL | #[derive(serde::Deserialize)]
43+
| ^^^^^^^^^^^^^^^^^^
44+
|
45+
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html
46+
= note: this error originates in the derive macro `serde::Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
47+
48+
error: aborting due to 5 previous errors
4049

0 commit comments

Comments
 (0)