Skip to content

Commit ea535c9

Browse files
committed
Auto merge of rust-lang#12804 - B14CK313:master, r=y21
fulfill expectations in `check_unsafe_derive_deserialize` The utility function `clippy_utils::fulfill_or_allowed` is not used because using it would require to move the check for allowed after the check iterating over all inherent impls of the type, doing possibly unnecessary work. Instead, `is_lint_allowed` is called as before, but additionally, once certain that the lint should be emitted, `span_lint_hir_and_then` is called instead of `span_lint_and_help` to also fulfill expectations. Note: as this is my first contribution, please feel free to nitpick or request changes. I am happy to adjust the implementation. fixes: rust-lang#12802 changelog: fulfill expectations in [`unsafe_derive_deserialize`]
2 parents 2efebd2 + 1b6c916 commit ea535c9

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

clippy_lints/src/derive.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
22
use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy};
33
use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths};
44
use rustc_errors::Applicability;
@@ -390,13 +390,17 @@ fn check_unsafe_derive_deserialize<'tcx>(
390390
.map(|imp_did| cx.tcx.hir().expect_item(imp_did.expect_local()))
391391
.any(|imp| has_unsafe(cx, imp))
392392
{
393-
span_lint_and_help(
393+
span_lint_hir_and_then(
394394
cx,
395395
UNSAFE_DERIVE_DESERIALIZE,
396+
adt_hir_id,
396397
item.span,
397398
"you are deriving `serde::Deserialize` on a type that has methods using `unsafe`",
398-
None,
399-
"consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html",
399+
|diag| {
400+
diag.help(
401+
"consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html",
402+
);
403+
},
400404
);
401405
}
402406
}

tests/ui/unsafe_derive_deserialize.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(lint_reasons)]
12
#![warn(clippy::unsafe_derive_deserialize)]
23
#![allow(unused, clippy::missing_safety_doc)]
34

@@ -71,4 +72,14 @@ impl G {
7172
}
7273
}
7374

75+
// Check that we honor the `expect` attribute on the ADT
76+
#[expect(clippy::unsafe_derive_deserialize)]
77+
#[derive(Deserialize)]
78+
pub struct H;
79+
impl H {
80+
pub fn unsafe_block(&self) {
81+
unsafe {}
82+
}
83+
}
84+
7485
fn main() {}

tests/ui/unsafe_derive_deserialize.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
2-
--> tests/ui/unsafe_derive_deserialize.rs:8:10
2+
--> tests/ui/unsafe_derive_deserialize.rs:9:10
33
|
44
LL | #[derive(Deserialize)]
55
| ^^^^^^^^^^^
@@ -10,7 +10,7 @@ LL | #[derive(Deserialize)]
1010
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

1212
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
13-
--> tests/ui/unsafe_derive_deserialize.rs:17:10
13+
--> tests/ui/unsafe_derive_deserialize.rs:18:10
1414
|
1515
LL | #[derive(Deserialize)]
1616
| ^^^^^^^^^^^
@@ -19,7 +19,7 @@ LL | #[derive(Deserialize)]
1919
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
2020

2121
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
22-
--> tests/ui/unsafe_derive_deserialize.rs:24:10
22+
--> tests/ui/unsafe_derive_deserialize.rs:25:10
2323
|
2424
LL | #[derive(Deserialize)]
2525
| ^^^^^^^^^^^
@@ -28,7 +28,7 @@ LL | #[derive(Deserialize)]
2828
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
2929

3030
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
31-
--> tests/ui/unsafe_derive_deserialize.rs:33:10
31+
--> tests/ui/unsafe_derive_deserialize.rs:34:10
3232
|
3333
LL | #[derive(Deserialize)]
3434
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)