-
-
Notifications
You must be signed in to change notification settings - Fork 857
add #[allow(deprecated)]
to derive implementations
#2879
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
Merged
dtolnay
merged 1 commit into
serde-rs:master
from
rcrisanti:fix/silence-deprecation-warnings-derive
Sep 16, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub fn allow_deprecated(input: &syn::DeriveInput) -> syn::Result<TokenStream> { | ||
if should_allow_deprecated(input)? { | ||
Ok(quote! { #[allow(deprecated)]}) | ||
} else { | ||
Ok(TokenStream::default()) | ||
} | ||
} | ||
|
||
/// Determine if an `#[allow(deprecated)]` should be added to the derived impl. | ||
/// | ||
/// This should happen if the derive input or a variant of the enum (if derive input is an enum) | ||
/// has on of: | ||
/// - `#[deprecated]` | ||
/// - `#[allow(deprecated)]` | ||
fn should_allow_deprecated(input: &syn::DeriveInput) -> syn::Result<bool> { | ||
if contains_deprecated_attrs(&input.attrs)? { | ||
return Ok(true); | ||
} | ||
if let syn::Data::Enum(data_enum) = &input.data { | ||
for variant in &data_enum.variants { | ||
if contains_deprecated_attrs(&variant.attrs)? { | ||
return Ok(true); | ||
} | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
/// Check whether a set of attributes contains one of: | ||
/// - `#[deprecated]` | ||
/// - `#[allow(deprecated)]` | ||
fn contains_deprecated_attrs(attrs: &[syn::Attribute]) -> syn::Result<bool> { | ||
for attr in attrs { | ||
if let syn::Meta::Path(path) = &attr.meta { | ||
if path.is_ident("deprecated") { | ||
return Ok(true); | ||
} | ||
} | ||
if let syn::Meta::List(meta_list) = &attr.meta { | ||
if meta_list.path.is_ident("allow") { | ||
let mut deprecated_allowed = false; | ||
meta_list.parse_nested_meta(|meta| { | ||
if meta.path.is_ident("deprecated") { | ||
deprecated_allowed = true; | ||
} | ||
Ok(()) | ||
})?; | ||
if deprecated_allowed { | ||
return Ok(true); | ||
} | ||
} | ||
} | ||
} | ||
Ok(false) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod ast; | ||
pub mod attr; | ||
pub mod deprecated; | ||
pub mod name; | ||
|
||
mod case; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![deny(deprecated)] | ||
#![allow(dead_code)] | ||
|
||
use serde_derive::{Deserialize, Serialize}; | ||
|
||
/// deprecated enum | ||
#[derive(Serialize, Deserialize)] | ||
#[deprecated] | ||
enum E1 { | ||
A, | ||
B, | ||
} | ||
|
||
/// deprecated struct | ||
#[derive(Serialize, Deserialize)] | ||
#[deprecated] | ||
struct S1 { | ||
a: bool, | ||
} | ||
|
||
/// deprecated enum variant | ||
#[derive(Serialize, Deserialize)] | ||
enum E2 { | ||
A, | ||
#[deprecated] | ||
B, | ||
} | ||
|
||
/// deprecated struct field | ||
#[derive(Serialize, Deserialize)] | ||
struct S2 { | ||
#[deprecated] | ||
a: bool, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![deny(deprecated)] | ||
|
||
use serde::Deserializer; | ||
use serde_derive::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Struct { | ||
#[serde(deserialize_with = "deprecated_with")] | ||
pub field: i32, | ||
} | ||
|
||
#[deprecated] | ||
fn deprecated_with<'de, D>(_deserializer: D) -> Result<i32, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
unimplemented!() | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: use of deprecated function `deprecated_with` | ||
--> tests/ui/deprecated/deprecated_de_with.rs:8:32 | ||
| | ||
8 | #[serde(deserialize_with = "deprecated_with")] | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> tests/ui/deprecated/deprecated_de_with.rs:1:9 | ||
| | ||
1 | #![deny(deprecated)] | ||
| ^^^^^^^^^^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![deny(deprecated)] | ||
|
||
use serde::Serializer; | ||
use serde_derive::Serialize; | ||
|
||
#[derive(Serialize)] | ||
pub struct Struct { | ||
#[serde(serialize_with = "deprecated_with")] | ||
pub field: i32, | ||
} | ||
|
||
#[deprecated] | ||
fn deprecated_with<S>(_field: &i32, _serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
unimplemented!() | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: use of deprecated function `deprecated_with` | ||
--> tests/ui/deprecated/deprecated_ser_with.rs:8:30 | ||
| | ||
8 | #[serde(serialize_with = "deprecated_with")] | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> tests/ui/deprecated/deprecated_ser_with.rs:1:9 | ||
| | ||
1 | #![deny(deprecated)] | ||
| ^^^^^^^^^^ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be "has one of"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was already fixed by 97168d3.