Skip to content

Commit

Permalink
Generate #[must_use]
Browse files Browse the repository at this point in the history
This is the Rust equivalent of the `warn_unused_result` attribute.

Co-authored-by: Jan Walther <[email protected]>
  • Loading branch information
madsmtm and Jan Walther committed Dec 12, 2024
1 parent 5c61433 commit 8487618
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
11 changes: 10 additions & 1 deletion crates/header-translator/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub(crate) struct MethodModifiers {
non_isolated: bool,
sendable: Option<bool>,
pub(crate) mainthreadonly: bool,
must_use: bool,
}

impl MethodModifiers {
Expand Down Expand Up @@ -108,7 +109,7 @@ impl MethodModifiers {
// <https://clang.llvm.org/docs/AttributeReference.html#objc-requires-super>
}
EntityKind::WarnUnusedResultAttr => {
// TODO: Emit `#[must_use]` on this
this.must_use = true;
}
EntityKind::ObjCClassRef
| EntityKind::ObjCProtocolRef
Expand Down Expand Up @@ -261,6 +262,7 @@ pub struct Method {
non_isolated: bool,
mainthreadonly: bool,
weak_property: bool,
must_use: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -509,6 +511,7 @@ impl Method {
non_isolated: modifiers.non_isolated,
mainthreadonly,
weak_property: false,
must_use: modifiers.must_use,
},
))
}
Expand Down Expand Up @@ -583,6 +586,7 @@ impl Method {
mainthreadonly,
// Don't show `weak`-ness on getters
weak_property: false,
must_use: modifiers.must_use,
})
} else {
None
Expand Down Expand Up @@ -626,6 +630,7 @@ impl Method {
non_isolated: modifiers.non_isolated,
mainthreadonly,
weak_property: attributes.map(|a| a.weak).unwrap_or(false),
must_use: modifiers.must_use,
})
} else {
None
Expand Down Expand Up @@ -686,6 +691,10 @@ impl fmt::Display for Method {

write!(f, "{}", self.availability)?;

if self.must_use {
writeln!(f, " #[must_use]")?;
}

if self.is_optional {
writeln!(f, " #[optional]")?;
}
Expand Down
14 changes: 13 additions & 1 deletion crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ pub enum Stmt {
// Some -> inline function.
body: Option<()>,
safe: bool,
must_use: bool,
},
/// typedef Type TypedefName;
AliasDecl {
Expand Down Expand Up @@ -1357,6 +1358,7 @@ impl Stmt {
let result_type = entity.get_result_type().expect("function result type");
let result_type = Ty::parse_function_return(result_type, context);
let mut arguments = Vec::new();
let mut must_use = false;

if entity.is_static_method() {
warn!("unexpected static method");
Expand Down Expand Up @@ -1390,7 +1392,7 @@ impl Stmt {
arguments.push((name, ty))
}
EntityKind::WarnUnusedResultAttr => {
// TODO: Emit `#[must_use]` on this
must_use = true;
}
EntityKind::PureAttr => {
// Ignore, we currently have no way of marking
Expand All @@ -1415,6 +1417,7 @@ impl Stmt {
result_type,
body,
safe: !data.unsafe_,
must_use,
}]
}
EntityKind::UnionDecl => {
Expand Down Expand Up @@ -2338,6 +2341,7 @@ impl Stmt {
result_type,
body: Some(_),
safe: _,
must_use: _,
} => {
write!(f, "// TODO: ")?;
write!(f, "pub fn {}(", id.name)?;
Expand All @@ -2354,13 +2358,17 @@ impl Stmt {
result_type,
body: None,
safe: false,
must_use,
} => {
// Functions are always C-unwind, since we don't know
// anything about them.
writeln!(f, "extern \"C-unwind\" {{")?;

write!(f, " {}", self.cfg_gate_ln(config))?;
write!(f, " {availability}")?;
if *must_use {
writeln!(f, " #[must_use]")?;
}
write!(f, " pub fn {}(", id.name)?;
for (param, arg_ty) in arguments {
let param = handle_reserved(&crate::to_snake_case(param));
Expand All @@ -2378,9 +2386,13 @@ impl Stmt {
result_type,
body: None,
safe: true,
must_use,
} => {
write!(f, "{}", self.cfg_gate_ln(config))?;
write!(f, "{availability}")?;
if *must_use {
writeln!(f, "#[must_use]")?;
}
writeln!(f, "#[inline]")?;
write!(f, "pub extern \"C-unwind\" fn {}(", id.name)?;
for (param, arg_ty) in arguments {
Expand Down
1 change: 1 addition & 0 deletions crates/objc2/src/topics/about_generated/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `EventKitUI` / `objc2-event-kit-ui`.
- `IOSurface` / `objc2-io-surface`.
- `ScreenSaver` / `objc2-screen-saver`.
* Added `#[must_use]` attributes where the C headers have them.

### Changed
* Allow using `MainThreadBound` without the `NSThread` feature flag.
Expand Down
2 changes: 1 addition & 1 deletion generated

0 comments on commit 8487618

Please sign in to comment.