Skip to content

Commit 635eae2

Browse files
committed
Teach rustfmt to handle postfix yield
1 parent 1c0916a commit 635eae2

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/tools/clippy/clippy_utils/src/ast_utils/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
201201
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lt, rt),
202202
(Block(lb, ll), Block(rb, rl)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lb, rb),
203203
(TryBlock(l), TryBlock(r)) => eq_block(l, r),
204-
(Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
204+
(Yield(l, lk), Yield(r, rk)) => eq_expr_opt(l.as_ref(), r.as_ref()) && lk == rk,
205+
(Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
205206
(Break(ll, le), Break(rl, re)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_expr_opt(le.as_ref(), re.as_ref()),
206207
(Continue(ll), Continue(rl)) => eq_label(ll.as_ref(), rl.as_ref()),
207208
(Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2, _), Index(r1, r2, _)) => {
@@ -688,7 +689,7 @@ pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
688689

689690
pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
690691
use WherePredicateKind::*;
691-
over(&l.attrs, &r.attrs, eq_attr)
692+
over(&l.attrs, &r.attrs, eq_attr)
692693
&& match (&l.kind, &r.kind) {
693694
(BoundPredicate(l), BoundPredicate(r)) => {
694695
over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {

src/tools/rustfmt/src/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub(crate) fn format_expr(
221221
Ok(format!("break{id_str}"))
222222
}
223223
}
224-
ast::ExprKind::Yield(ref opt_expr) => {
224+
ast::ExprKind::Yield(ref opt_expr, ast::YieldKind::Prefix) => {
225225
if let Some(ref expr) = *opt_expr {
226226
rewrite_unary_prefix(context, "yield ", &**expr, shape)
227227
} else {
@@ -243,7 +243,8 @@ pub(crate) fn format_expr(
243243
ast::ExprKind::Try(..)
244244
| ast::ExprKind::Field(..)
245245
| ast::ExprKind::MethodCall(..)
246-
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
246+
| ast::ExprKind::Await(_, _)
247+
| ast::ExprKind::Yield(_, ast::YieldKind::Postfix) => rewrite_chain(expr, context, shape),
247248
ast::ExprKind::MacCall(ref mac) => {
248249
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
249250
wrap_str(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This demonstrates a proposed alternate or additional option of having yield in postfix position.
2+
//@ edition: 2024
3+
4+
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
5+
6+
use std::ops::{Coroutine, CoroutineState};
7+
use std::pin::pin;
8+
9+
fn main() {
10+
let mut coro =
11+
pin!(#[coroutine] |_: i32| { let x = 1.yield;
12+
13+
14+
(x + 2).yield; });
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This demonstrates a proposed alternate or additional option of having yield in postfix position.
2+
//@ edition: 2024
3+
4+
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
5+
6+
use std::ops::{Coroutine, CoroutineState};
7+
use std::pin::pin;
8+
9+
fn main() {
10+
let mut coro =
11+
pin!(#[coroutine] |_: i32| { let x = 1.yield; (x + 2).yield; });
12+
}

0 commit comments

Comments
 (0)