Skip to content

Commit 01a7a4d

Browse files
KarelPeetersemilio
authored andcommitted
Also implement div-style must_use_type annotation.
1 parent 9833b67 commit 01a7a4d

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ impl CodeGenerator for CompInfo {
20232023
attributes.push(attributes::derives(&derives))
20242024
}
20252025

2026-
if ctx.must_use_type_by_name(item) {
2026+
if item.annotations().must_use_type() || ctx.must_use_type_by_name(item) {
20272027
attributes.push(attributes::must_use());
20282028
}
20292029

src/ir/annotations.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub struct Annotations {
4242
disallow_debug: bool,
4343
/// Manually disable deriving/implement default on this type.
4444
disallow_default: bool,
45+
/// Whether to add a #[must_use] annotation to this type.
46+
must_use_type: bool,
4547
/// Whether fields should be marked as private or not. You can set this on
4648
/// structs (it will apply to all the fields), or individual fields.
4749
private_fields: Option<bool>,
@@ -84,6 +86,7 @@ impl Default for Annotations {
8486
disallow_copy: false,
8587
disallow_debug: false,
8688
disallow_default: false,
89+
must_use_type: false,
8790
private_fields: None,
8891
accessor_kind: None,
8992
constify_enum_variant: false,
@@ -163,6 +166,11 @@ impl Annotations {
163166
self.disallow_default
164167
}
165168

169+
/// Should this type get a `#[must_use]` annotation?
170+
pub fn must_use_type(&self) -> bool {
171+
self.must_use_type
172+
}
173+
166174
/// Should the fields be private?
167175
pub fn private_fields(&self) -> Option<bool> {
168176
self.private_fields
@@ -190,6 +198,7 @@ impl Annotations {
190198
"nocopy" => self.disallow_copy = true,
191199
"nodebug" => self.disallow_debug = true,
192200
"nodefault" => self.disallow_default = true,
201+
"mustusetype" => self.must_use_type = true,
193202
"replaces" => {
194203
self.use_instead_of = Some(
195204
attr.value.split("::").map(Into::into).collect(),

tests/expectations/tests/issue-710-must-use-type.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
#[repr(C)]
99
#[derive(Debug, Copy, Clone)]
1010
#[must_use]
11-
pub struct MyType {
11+
pub struct A {
1212
_unused: [u8; 0],
1313
}
14+
/// <div rustbindgen mustusetype></div>
1415
#[repr(C)]
1516
#[derive(Debug, Copy, Clone)]
16-
pub struct OtherType {
17+
#[must_use]
18+
pub struct B {
19+
_unused: [u8; 0],
20+
}
21+
#[repr(C)]
22+
#[derive(Debug, Copy, Clone)]
23+
pub struct C {
1724
_unused: [u8; 0],
1825
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// bindgen-flags: --must-use-type MyType
1+
// bindgen-flags: --must-use-type A
22

3-
struct MyType;
3+
struct A;
44

5-
struct OtherType;
5+
/** <div rustbindgen mustusetype></div> */
6+
struct B;
7+
8+
struct C;

0 commit comments

Comments
 (0)