Skip to content

Commit 0734282

Browse files
authored
Rollup merge of #89610 - guswynn:must_use_future, r=wesleywiser
warn on must_use use on async fn's As referenced in #78149 This only works on `async` fn's for now, I can also look into if I can get `Box<dyn Future>` and `impl Future` working at this level (hir)
2 parents 41301c3 + 83ce771 commit 0734282

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

compiler/rustc_passes/src/check_attr.rs

+32
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl CheckAttrVisitor<'tcx> {
112112
self.check_default_method_body_is_const(attr, span, target)
113113
}
114114
sym::must_not_suspend => self.check_must_not_suspend(&attr, span, target),
115+
sym::must_use => self.check_must_use(hir_id, &attr, span, target),
115116
sym::rustc_const_unstable
116117
| sym::rustc_const_stable
117118
| sym::unstable
@@ -1046,6 +1047,37 @@ impl CheckAttrVisitor<'tcx> {
10461047
is_valid
10471048
}
10481049

1050+
/// Warns against some misuses of `#[must_use]`
1051+
fn check_must_use(
1052+
&self,
1053+
hir_id: HirId,
1054+
attr: &Attribute,
1055+
span: &Span,
1056+
_target: Target,
1057+
) -> bool {
1058+
let node = self.tcx.hir().get(hir_id);
1059+
if let Some(fn_node) = node.fn_kind() {
1060+
if let rustc_hir::IsAsync::Async = fn_node.asyncness() {
1061+
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
1062+
lint.build(
1063+
"`must_use` attribute on `async` functions \
1064+
applies to the anonymous `Future` returned by the \
1065+
function, not the value within",
1066+
)
1067+
.span_label(
1068+
*span,
1069+
"this attribute does nothing, the `Future`s \
1070+
returned by async functions are already `must_use`",
1071+
)
1072+
.emit();
1073+
});
1074+
}
1075+
}
1076+
1077+
// For now, its always valid
1078+
true
1079+
}
1080+
10491081
/// Checks if `#[must_not_suspend]` is applied to a function. Returns `true` if valid.
10501082
fn check_must_not_suspend(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
10511083
match target {
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// edition:2018
2+
// run-pass
3+
#![allow(dead_code)]
4+
5+
#[must_use]
6+
//~^ WARNING `must_use`
7+
async fn test() -> i32 {
8+
1
9+
}
10+
11+
12+
struct Wowee {}
13+
14+
impl Wowee {
15+
#[must_use]
16+
//~^ WARNING `must_use`
17+
async fn test_method() -> i32 {
18+
1
19+
}
20+
}
21+
22+
/* FIXME(guswynn) update this test when async-fn-in-traits works
23+
trait Doer {
24+
#[must_use]
25+
async fn test_trait_method() -> i32;
26+
WARNING must_use
27+
async fn test_other_trait() -> i32;
28+
}
29+
30+
impl Doer for Wowee {
31+
async fn test_trait_method() -> i32 {
32+
1
33+
}
34+
#[must_use]
35+
async fn test_other_trait() -> i32 {
36+
WARNING must_use
37+
1
38+
}
39+
}
40+
*/
41+
42+
fn main() {
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within
2+
--> $DIR/unused-async.rs:5:1
3+
|
4+
LL | #[must_use]
5+
| ^^^^^^^^^^^
6+
LL |
7+
LL | / async fn test() -> i32 {
8+
LL | | 1
9+
LL | | }
10+
| |_- this attribute does nothing, the `Future`s returned by async functions are already `must_use`
11+
|
12+
= note: `#[warn(unused_attributes)]` on by default
13+
14+
warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within
15+
--> $DIR/unused-async.rs:15:5
16+
|
17+
LL | #[must_use]
18+
| ^^^^^^^^^^^
19+
LL |
20+
LL | / async fn test_method() -> i32 {
21+
LL | | 1
22+
LL | | }
23+
| |_____- this attribute does nothing, the `Future`s returned by async functions are already `must_use`
24+
25+
warning: 2 warnings emitted
26+

0 commit comments

Comments
 (0)