summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorIan Chamberlain <ian.h.chamberlain@gmail.com>2022-05-19 21:54:46 -0400
committerEmilio Cobos Álvarez <emilio@crisal.io>2022-06-05 19:38:05 +0200
commit3a8a60cd4c88fd6e753a2f215796a5117b7bfaa8 (patch)
treec1aa97873f8041b6d2088b03812fa06121d730e9 /src/codegen
parent2cf7ba9ee1a0b545ec52ffea697af42ee681ee22 (diff)
Look for `must_use` on typdefs in function return
Closes #2206
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/mod.rs32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index e4ce9526..fdc9b5ed 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -3965,11 +3965,35 @@ impl CodeGenerator for Function {
let mut attributes = vec![];
- if signature.must_use() &&
- ctx.options().rust_features().must_use_function
- {
- attributes.push(attributes::must_use());
+ if ctx.options().rust_features().must_use_function {
+ let must_use = signature.must_use() || {
+ let ret_ty = signature.return_type();
+
+ let resolved_ret = ret_ty
+ .into_resolver()
+ .through_type_refs()
+ .through_type_aliases()
+ .resolve(ctx);
+
+ let must_use_resolved_ty =
+ resolved_ret.annotations().must_use_type() ||
+ ctx.must_use_type_by_name(resolved_ret);
+
+ let ret = ctx.resolve_item(ret_ty);
+ let must_use_ty = ret.annotations().must_use_type() ||
+ ctx.must_use_type_by_name(ret);
+
+ // If the return type already has #[must_use], the function does not
+ // need the annotation. This preserves the codegen behavior before
+ // type aliases with #[must_use] were supported.
+ !must_use_resolved_ty && must_use_ty
+ };
+
+ if must_use {
+ attributes.push(attributes::must_use());
+ }
}
+
if let Some(comment) = item.comment(ctx) {
attributes.push(attributes::doc(comment));
}