rust-lang / rust-lang/rust-clippy

`unsafe_derive_deserialize` fires for `unsafe` blocks in associated constants

Open
#10,349 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

unsafe_derive_deserialize is triggered when you have an associated constant that uses unsafe. However, associated constants will not affect a derived serde::Deserialize impl.

To ensure this particular bit of information doesn't get lost:
My original code had something along the lines of const FOO: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(123) }, because Option::unwrap is not const-stable.

Lint Name

unsafe_derive_deserialize

Reproducer

I tried this code:

#![warn(clippy::unsafe_derive_deserialize)]

#[derive(serde::Deserialize)]
pub struct Foo;

impl Foo {
    #[allow(unused_unsafe)]
    pub const BAR: () = unsafe {};
}

I saw this happen:

warning: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
 --> src/lib.rs:4:10
  |
4 | #[derive(serde::Deserialize)]
  |          ^^^^^^^^^^^^^^^^^^
  |
  = help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_derive_deserialize
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::unsafe_derive_deserialize)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: this warning originates in the derive macro `serde::Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `playground` (lib) generated 1 warning

I expected to see this happen:
No output.

Version
Note: this particular instance- this was the MRE- was triggered on the rust playground with the following Clippy: version 0.1.69 (2023-02-14 0416b1a)

However, the version info for my local system, where I originally found this issue is as follows:
rustc 1.66.0-nightly (5e9772042 2022-10-29)
binary: rustc
commit-hash: 5e9772042948002f9c6f60c4c81603170035fffa
commit-date: 2022-10-29
host: aarch64-apple-darwin
release: 1.66.0-nightly
LLVM version: 15.0.2

This also occurs on the latest nightly available on the playground as of posting (2023-02-14)
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the unsafe_derive_deserialize warning with the associated-constant example and inspect that lint's implementation and existing tests. Done means the reproducer no longer emits a warning while the lint still detects unsafe methods relevant to derived serde::Deserialize implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.