serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
8.98k stars 758 forks source link

`#[derive(Serialize)` and `#[derive(Deserialize)]` on deprecated types emit deprecation warnings #2195

Open nsunderland1 opened 2 years ago

nsunderland1 commented 2 years ago

Given this code (playground link):

#[deprecated]
#[derive(Default, serde::Serialize)]
struct WithSerialize;

#[deprecated]
#[derive(Default, serde::Deserialize)]
struct WithDeserialize;

#[deprecated]
#[derive(Default)]
struct WithoutSerde;

fn main() {
}

I expect not to get deprecation warnings. Instead, I get the following:

warning: use of deprecated struct `WithSerialize`
 [--> src/main.rs:3:8
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=22d2042e09ce11dddfcbac0464dd4fe3#)  |
3 | struct WithSerialize;
  |        ^^^^^^^^^^^^^
  |
  = note: `#[warn(deprecated)]` on by default

warning: use of deprecated struct `WithDeserialize`
 [--> src/main.rs:7:8
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=22d2042e09ce11dddfcbac0464dd4fe3#)  |
7 | struct WithDeserialize;
  |        ^^^^^^^^^^^^^^^

warning: use of deprecated unit struct `WithDeserialize`
 [--> src/main.rs:7:8
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=22d2042e09ce11dddfcbac0464dd4fe3#)  |
7 | struct WithDeserialize;
  |        ^^^^^^^^^^^^^^^

Notice that the derives for Default don't trigger any warnings (this seems to have been fixed some time ago).

Not aware of any workaround (other than undeprecating), and this leads to a bunch of warnings for things that are deprecated but that we still want to be able to support serialization for.

LovingMelody commented 2 years ago

Not exactly a perfect workaround however you can disable the lint if the warnings are an issue by moving the deprecated items to another module (playground link). This is not perfect as it will disable all lints for deprecated usages in that module.

eric-seppanen commented 1 month ago

I am having trouble trying to use a deprecated struct in a (non-deprecated) enum.

For example, this (playground) emits warnings no matter how hard I try to suppress them. If I don't derive serde traits on the enum, the problem goes away.

mod current_structs {
    use serde::{Deserialize, Serialize};

    #[derive(Serialize, Deserialize)]
    pub struct IsCurrent;
}

mod deprecated_structs {
    #![allow(deprecated)]

    use serde::{Deserialize, Serialize};

    #[deprecated]
    #[derive(Serialize, Deserialize)]
    pub struct IsDeprecated;
}

use serde::{Deserialize, Serialize};

use current_structs::*;

#[allow(deprecated)]
use deprecated_structs::*;

#[allow(deprecated)]
#[derive(Serialize, Deserialize)]
enum Container {
    #[allow(deprecated)]
    A(#[allow(deprecated)] IsDeprecated),
    B(IsCurrent),
}