Skip to content

Commit 548d438

Browse files
committed
Add must_use attributes to accessor methods
1 parent 12fe16a commit 548d438

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

rust/ruby-rbs/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ fn write_node_field_accessor(
117117
rust_type: &str,
118118
) -> std::io::Result<()> {
119119
if field.optional {
120+
writeln!(file, " #[must_use]")?;
120121
writeln!(
121122
file,
122123
" pub fn {}(&self) -> Option<{rust_type}<'a>> {{",
@@ -136,6 +137,7 @@ fn write_node_field_accessor(
136137
)?;
137138
writeln!(file, " }}")?;
138139
} else {
140+
writeln!(file, " #[must_use]")?;
139141
writeln!(
140142
file,
141143
" pub fn {}(&self) -> {rust_type}<'a> {{",
@@ -371,6 +373,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
371373
for field in fields {
372374
match field.c_type.as_str() {
373375
"rbs_string" => {
376+
writeln!(file, " #[must_use]")?;
374377
writeln!(file, " pub fn {}(&self) -> RBSString {{", field.name)?;
375378
writeln!(
376379
file,
@@ -381,6 +384,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
381384
writeln!(file)?;
382385
}
383386
"bool" => {
387+
writeln!(file, " #[must_use]")?;
384388
writeln!(file, " pub fn {}(&self) -> bool {{", field.name)?;
385389
writeln!(file, " unsafe {{ (*self.pointer).{} }}", field.name)?;
386390
writeln!(file, " }}")?;
@@ -398,6 +402,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
398402
}
399403
"rbs_location" => {
400404
if field.optional {
405+
writeln!(file, " #[must_use]")?;
401406
writeln!(
402407
file,
403408
" pub fn {}(&self) -> Option<RBSLocation> {{",
@@ -415,6 +420,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
415420
writeln!(file, " }}")?;
416421
writeln!(file, " }}")?;
417422
} else {
423+
writeln!(file, " #[must_use]")?;
418424
writeln!(file, " pub fn {}(&self) -> RBSLocation {{", field.name)?;
419425
writeln!(
420426
file,
@@ -427,6 +433,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
427433
}
428434
"rbs_location_list" => {
429435
if field.optional {
436+
writeln!(file, " #[must_use]")?;
430437
writeln!(
431438
file,
432439
" pub fn {}(&self) -> Option<RBSLocationList> {{",
@@ -444,6 +451,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
444451
writeln!(file, " }}")?;
445452
writeln!(file, " }}")?;
446453
} else {
454+
writeln!(file, " #[must_use]")?;
447455
writeln!(
448456
file,
449457
" pub fn {}(&self) -> RBSLocationList {{",
@@ -468,6 +476,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
468476
field.name.as_str()
469477
};
470478
if field.optional {
479+
writeln!(file, " #[must_use]")?;
471480
writeln!(file, " pub fn {name}(&self) -> Option<Node<'a>> {{")?;
472481
writeln!(
473482
file,
@@ -479,6 +488,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
479488
" if ptr.is_null() {{ None }} else {{ Some(Node::new(self.parser, ptr)) }}"
480489
)?;
481490
} else {
491+
writeln!(file, " #[must_use]")?;
482492
writeln!(file, " pub fn {name}(&self) -> Node<'a> {{")?;
483493
writeln!(
484494
file,

rust/ruby-rbs/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl Drop for SignatureNode<'_> {
5555
}
5656

5757
impl KeywordNode<'_> {
58+
#[must_use]
5859
pub fn name(&self) -> &[u8] {
5960
unsafe {
6061
let constant_ptr = rbs_constant_pool_id_to_constant(
@@ -78,6 +79,7 @@ pub struct NodeList<'a> {
7879
}
7980

8081
impl<'a> NodeList<'a> {
82+
#[must_use]
8183
pub fn new(parser: NonNull<rbs_parser_t>, pointer: *mut rbs_node_list_t) -> Self {
8284
Self {
8385
parser,
@@ -125,6 +127,7 @@ pub struct RBSHash<'a> {
125127
}
126128

127129
impl<'a> RBSHash<'a> {
130+
#[must_use]
128131
pub fn new(parser: NonNull<rbs_parser_t>, pointer: *mut rbs_hash) -> Self {
129132
Self {
130133
parser,
@@ -171,14 +174,17 @@ pub struct RBSLocation {
171174
}
172175

173176
impl RBSLocation {
177+
#[must_use]
174178
pub fn new(pointer: *const rbs_location_t) -> Self {
175179
Self { pointer }
176180
}
177181

182+
#[must_use]
178183
pub fn start(&self) -> i32 {
179184
unsafe { (*self.pointer).rg.start.byte_pos }
180185
}
181186

187+
#[must_use]
182188
pub fn end(&self) -> i32 {
183189
unsafe { (*self.pointer).rg.end.byte_pos }
184190
}
@@ -189,6 +195,7 @@ pub struct RBSLocationList {
189195
}
190196

191197
impl RBSLocationList {
198+
#[must_use]
192199
pub fn new(pointer: *mut rbs_location_list) -> Self {
193200
Self { pointer }
194201
}
@@ -227,10 +234,12 @@ pub struct RBSString {
227234
}
228235

229236
impl RBSString {
237+
#[must_use]
230238
pub fn new(pointer: *const rbs_string_t) -> Self {
231239
Self { pointer }
232240
}
233241

242+
#[must_use]
234243
pub fn as_bytes(&self) -> &[u8] {
235244
unsafe {
236245
let s = *self.pointer;
@@ -240,6 +249,7 @@ impl RBSString {
240249
}
241250

242251
impl SymbolNode<'_> {
252+
#[must_use]
243253
pub fn name(&self) -> &[u8] {
244254
unsafe {
245255
let constant_ptr = rbs_constant_pool_id_to_constant(

0 commit comments

Comments
 (0)