diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-08-13 21:11:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-13 21:11:13 +0200 |
| commit | d4f5a89f6e9f62f147994b65cacf77e944e2aec6 (patch) | |
| tree | bc0876cd79e3149709bea841f5454a9b63ae6e72 | |
| parent | f68a28d95c47b9802b9dbb8d8e7a89de45a381ec (diff) | |
| parent | 1e445f48d4d0880d4c158c3ae96be8f0d9ad2e02 (diff) | |
| download | rust-d4f5a89f6e9f62f147994b65cacf77e944e2aec6.tar.gz rust-d4f5a89f6e9f62f147994b65cacf77e944e2aec6.zip | |
Rollup merge of #129034 - henryksloan:coroutine-must-use, r=joboet
Add `#[must_use]` attribute to `Coroutine` trait [Coroutines tracking issue](https://github.com/rust-lang/rust/issues/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`.
| -rw-r--r-- | library/core/src/ops/coroutine.rs | 1 | ||||
| -rw-r--r-- | tests/ui/coroutine/issue-58888.rs | 5 |
2 files changed, 4 insertions, 2 deletions
diff --git a/library/core/src/ops/coroutine.rs b/library/core/src/ops/coroutine.rs index 13df888d24c..c7d596d74c3 100644 --- a/library/core/src/ops/coroutine.rs +++ b/library/core/src/ops/coroutine.rs @@ -69,6 +69,7 @@ pub enum CoroutineState<Y, R> { #[lang = "coroutine"] #[unstable(feature = "coroutine_trait", issue = "43122")] #[fundamental] +#[must_use = "coroutines are lazy and do nothing unless resumed"] pub trait Coroutine<R = ()> { /// The type of value this coroutine yields. /// diff --git a/tests/ui/coroutine/issue-58888.rs b/tests/ui/coroutine/issue-58888.rs index 6266f97ce8c..e4fada0cd43 100644 --- a/tests/ui/coroutine/issue-58888.rs +++ b/tests/ui/coroutine/issue-58888.rs @@ -13,7 +13,8 @@ impl Database { } fn check_connection(&self) -> impl Coroutine<Yield = (), Return = ()> + '_ { - #[coroutine] move || { + #[coroutine] + move || { let iter = self.get_connection(); for i in iter { yield i @@ -23,5 +24,5 @@ impl Database { } fn main() { - Database.check_connection(); + let _ = Database.check_connection(); } |
