diff options
author | Karel Peeters <karel.peeters.leuven@gmail.com> | 2021-07-23 18:40:23 +0200 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2021-07-31 13:47:39 +0200 |
commit | 01a7a4d7158c2d227dc55199fe1a4306dcbe68cf (patch) | |
tree | a6e8ee560363abec287fe680faa4fcea20e238d6 | |
parent | 9833b6745d74896620ea760f3bf81f344a105f30 (diff) |
Also implement div-style must_use_type annotation.
-rw-r--r-- | src/codegen/mod.rs | 2 | ||||
-rw-r--r-- | src/ir/annotations.rs | 9 | ||||
-rw-r--r-- | tests/expectations/tests/issue-710-must-use-type.rs | 11 | ||||
-rw-r--r-- | tests/headers/issue-710-must-use-type.h | 9 |
4 files changed, 25 insertions, 6 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 76c64e4a..2522922a 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2023,7 +2023,7 @@ impl CodeGenerator for CompInfo { attributes.push(attributes::derives(&derives)) } - if ctx.must_use_type_by_name(item) { + if item.annotations().must_use_type() || ctx.must_use_type_by_name(item) { attributes.push(attributes::must_use()); } diff --git a/src/ir/annotations.rs b/src/ir/annotations.rs index 12664f60..4b571eaa 100644 --- a/src/ir/annotations.rs +++ b/src/ir/annotations.rs @@ -42,6 +42,8 @@ pub struct Annotations { disallow_debug: bool, /// Manually disable deriving/implement default on this type. disallow_default: bool, + /// Whether to add a #[must_use] annotation to this type. + must_use_type: bool, /// Whether fields should be marked as private or not. You can set this on /// structs (it will apply to all the fields), or individual fields. private_fields: Option<bool>, @@ -84,6 +86,7 @@ impl Default for Annotations { disallow_copy: false, disallow_debug: false, disallow_default: false, + must_use_type: false, private_fields: None, accessor_kind: None, constify_enum_variant: false, @@ -163,6 +166,11 @@ impl Annotations { self.disallow_default } + /// Should this type get a `#[must_use]` annotation? + pub fn must_use_type(&self) -> bool { + self.must_use_type + } + /// Should the fields be private? pub fn private_fields(&self) -> Option<bool> { self.private_fields @@ -190,6 +198,7 @@ impl Annotations { "nocopy" => self.disallow_copy = true, "nodebug" => self.disallow_debug = true, "nodefault" => self.disallow_default = true, + "mustusetype" => self.must_use_type = true, "replaces" => { self.use_instead_of = Some( attr.value.split("::").map(Into::into).collect(), diff --git a/tests/expectations/tests/issue-710-must-use-type.rs b/tests/expectations/tests/issue-710-must-use-type.rs index 20a713e4..1d598241 100644 --- a/tests/expectations/tests/issue-710-must-use-type.rs +++ b/tests/expectations/tests/issue-710-must-use-type.rs @@ -8,11 +8,18 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] #[must_use] -pub struct MyType { +pub struct A { _unused: [u8; 0], } +/// <div rustbindgen mustusetype></div> #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct OtherType { +#[must_use] +pub struct B { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct C { _unused: [u8; 0], } diff --git a/tests/headers/issue-710-must-use-type.h b/tests/headers/issue-710-must-use-type.h index 0c047e61..276f636d 100644 --- a/tests/headers/issue-710-must-use-type.h +++ b/tests/headers/issue-710-must-use-type.h @@ -1,5 +1,8 @@ -// bindgen-flags: --must-use-type MyType +// bindgen-flags: --must-use-type A -struct MyType; +struct A; -struct OtherType; +/** <div rustbindgen mustusetype></div> */ +struct B; + +struct C; |