Skip to content

Commit d4f5a89

Browse files
authored
Rollup merge of rust-lang#129034 - henryksloan:coroutine-must-use, r=joboet
Add `#[must_use]` attribute to `Coroutine` trait [Coroutines tracking issue](rust-lang#43122) Like closures (`FnOnce`, `AsyncFn`, etc.), coroutines are lazy and do nothing unless called (resumed). Closure traits like `FnOnce` have `#[must_use = "closures are lazy and do nothing unless called"]` to catch likely bugs for users of APIs that produce them. This PR adds such a `#[must_use]` attribute to `trait Coroutine`.
2 parents f68a28d + 1e445f4 commit d4f5a89

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

library/core/src/ops/coroutine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub enum CoroutineState<Y, R> {
6969
#[lang = "coroutine"]
7070
#[unstable(feature = "coroutine_trait", issue = "43122")]
7171
#[fundamental]
72+
#[must_use = "coroutines are lazy and do nothing unless resumed"]
7273
pub trait Coroutine<R = ()> {
7374
/// The type of value this coroutine yields.
7475
///

tests/ui/coroutine/issue-58888.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ impl Database {
1313
}
1414

1515
fn check_connection(&self) -> impl Coroutine<Yield = (), Return = ()> + '_ {
16-
#[coroutine] move || {
16+
#[coroutine]
17+
move || {
1718
let iter = self.get_connection();
1819
for i in iter {
1920
yield i
@@ -23,5 +24,5 @@ impl Database {
2324
}
2425

2526
fn main() {
26-
Database.check_connection();
27+
let _ = Database.check_connection();
2728
}

0 commit comments

Comments
 (0)