Skip to content

Commit

Permalink
Fix self_ handling during PascalCasification (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Veetaha authored Dec 28, 2024
1 parent 5099fef commit 0bd62b7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion bon-macros/src/util/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ impl IdentExt for syn::Ident {
// https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094
//
// So no need to handle raw identifiers here.
let renamed = RenameRule::PascalCase.apply_to_field(self.raw_name());
let mut renamed = RenameRule::PascalCase.apply_to_field(self.raw_name());

// Make sure `Self` keyword isn't generated.
// This may happen if the input was `self_`, for example.
if renamed == "Self" {
renamed.push('_');
}

Self::new(&renamed, Span::call_site())
}

Expand Down
13 changes: 13 additions & 0 deletions bon/tests/integration/builder/raw_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ fn test_fn() {
}
}
}

// This is based on the issue https://github.com/elastio/bon/issues/237
#[test]
fn test_self_underscore_bug_237() {
#[derive(Builder)]
struct Sut {
self_: u32,
}

let builder: SutBuilder<sut_builder::SetSelf_> = Sut::builder().self_(42);

assert_eq!(builder.build().self_, 42);
}

0 comments on commit 0bd62b7

Please sign in to comment.