Use qualified path for Result::Ok in bincode_derive (#757)

* Use qualified path for Result::Ok in bincode_derive

* add test
This commit is contained in:
nhaef 2025-03-10 13:15:20 +01:00 committed by GitHub
parent 2b5dbfc05c
commit 3d1c4c339a
3 changed files with 23 additions and 6 deletions

View File

@ -124,7 +124,7 @@ impl DeriveEnum {
} }
} }
} }
body.push_parsed("Ok(())")?; body.push_parsed("core::result::Result::Ok(())")?;
Ok(()) Ok(())
})?; })?;
match_body.punct(','); match_body.punct(',');
@ -274,7 +274,7 @@ impl DeriveEnum {
variant_case.push(variant_index.remove(0)); variant_case.push(variant_index.remove(0));
} }
variant_case.puncts("=>"); variant_case.puncts("=>");
variant_case.ident_str("Ok"); variant_case.push_parsed("core::result::Result::Ok")?;
variant_case.group(Delimiter::Parenthesis, |variant_case_body| { variant_case.group(Delimiter::Parenthesis, |variant_case_body| {
// Self::Variant { } // Self::Variant { }
// Self::Variant { 0: ..., 1: ... 2: ... }, // Self::Variant { 0: ..., 1: ... 2: ... },
@ -384,7 +384,7 @@ impl DeriveEnum {
variant_case.push(variant_index.remove(0)); variant_case.push(variant_index.remove(0));
} }
variant_case.puncts("=>"); variant_case.puncts("=>");
variant_case.ident_str("Ok"); variant_case.push_parsed("core::result::Result::Ok")?;
variant_case.group(Delimiter::Parenthesis, |variant_case_body| { variant_case.group(Delimiter::Parenthesis, |variant_case_body| {
// Self::Variant { } // Self::Variant { }
// Self::Variant { 0: ..., 1: ... 2: ... }, // Self::Variant { 0: ..., 1: ... 2: ... },

View File

@ -56,7 +56,7 @@ impl DeriveStruct {
} }
} }
} }
fn_body.push_parsed("Ok(())")?; fn_body.push_parsed("core::result::Result::Ok(())")?;
Ok(()) Ok(())
})?; })?;
Ok(()) Ok(())
@ -95,7 +95,7 @@ impl DeriveStruct {
.with_return_type(format!("core::result::Result<Self, {}::error::DecodeError>", crate_name)) .with_return_type(format!("core::result::Result<Self, {}::error::DecodeError>", crate_name))
.body(|fn_body| { .body(|fn_body| {
// Ok(Self { // Ok(Self {
fn_body.ident_str("Ok"); fn_body.push_parsed("core::result::Result::Ok")?;
fn_body.group(Delimiter::Parenthesis, |ok_group| { fn_body.group(Delimiter::Parenthesis, |ok_group| {
ok_group.ident_str("Self"); ok_group.ident_str("Self");
ok_group.group(Delimiter::Brace, |struct_body| { ok_group.group(Delimiter::Brace, |struct_body| {
@ -174,7 +174,7 @@ impl DeriveStruct {
.with_return_type(format!("core::result::Result<Self, {}::error::DecodeError>", crate_name)) .with_return_type(format!("core::result::Result<Self, {}::error::DecodeError>", crate_name))
.body(|fn_body| { .body(|fn_body| {
// Ok(Self { // Ok(Self {
fn_body.ident_str("Ok"); fn_body.push_parsed("core::result::Result::Ok")?;
fn_body.group(Delimiter::Parenthesis, |ok_group| { fn_body.group(Delimiter::Parenthesis, |ok_group| {
ok_group.ident_str("Self"); ok_group.ident_str("Self");
ok_group.group(Delimiter::Brace, |struct_body| { ok_group.group(Delimiter::Brace, |struct_body| {

View File

@ -403,6 +403,23 @@ fn test_enum_with_generics_roundtrip() {
assert_eq!(start, decoded); assert_eq!(start, decoded);
} }
mod derive_with_polluted_scope {
#[allow(dead_code)]
#[allow(non_snake_case)]
fn Ok() {}
#[derive(bincode::Encode, bincode::Decode)]
struct A {
a: u32,
}
#[derive(bincode::Encode, bincode::Decode)]
enum B {
A,
B,
}
}
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
mod zoxide { mod zoxide {
extern crate alloc; extern crate alloc;