rustfmt_nightly/
expr.rs

1use std::borrow::Cow;
2use std::cmp::min;
3
4use itertools::Itertools;
5use rustc_ast::token::{Delimiter, Lit, LitKind};
6use rustc_ast::{ForLoopKind, MatchKind, ast, token};
7use rustc_span::{BytePos, Span};
8use tracing::debug;
9
10use crate::chains::rewrite_chain;
11use crate::closures;
12use crate::comment::{
13    CharClasses, FindUncommented, combine_strs_with_missing_comments, contains_comment,
14    recover_comment_removed, rewrite_comment, rewrite_missing_comment,
15};
16use crate::config::lists::*;
17use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, StyleEdition};
18use crate::lists::{
19    ListFormatting, Separator, definitive_tactic, itemize_list, shape_for_tactic,
20    struct_lit_formatting, struct_lit_shape, struct_lit_tactic, write_list,
21};
22use crate::macros::{MacroPosition, rewrite_macro};
23use crate::matches::rewrite_match;
24use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
25use crate::pairs::{PairParts, rewrite_all_pairs, rewrite_pair};
26use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
27use crate::shape::{Indent, Shape};
28use crate::source_map::{LineRangeUtils, SpanUtils};
29use crate::spanned::Spanned;
30use crate::stmt;
31use crate::string::{StringFormat, rewrite_string};
32use crate::types::{PathContext, rewrite_path};
33use crate::utils::{
34    colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
35    inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
36    semicolon_for_expr, unicode_str_width, wrap_str,
37};
38use crate::vertical::rewrite_with_alignment;
39use crate::visitor::FmtVisitor;
40
41impl Rewrite for ast::Expr {
42    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
43        self.rewrite_result(context, shape).ok()
44    }
45
46    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
47        format_expr(self, ExprType::SubExpression, context, shape)
48    }
49}
50
51#[derive(Copy, Clone, PartialEq)]
52pub(crate) enum ExprType {
53    Statement,
54    SubExpression,
55}
56
57pub(crate) fn lit_ends_in_dot(lit: &Lit) -> bool {
58    matches!(lit, Lit { kind: LitKind::Float, suffix: None, symbol } if symbol.as_str().ends_with('.'))
59}
60
61pub(crate) fn format_expr(
62    expr: &ast::Expr,
63    expr_type: ExprType,
64    context: &RewriteContext<'_>,
65    shape: Shape,
66) -> RewriteResult {
67    skip_out_of_file_lines_range_err!(context, expr.span);
68
69    if contains_skip(&*expr.attrs) {
70        return Ok(context.snippet(expr.span()).to_owned());
71    }
72    let shape = if expr_type == ExprType::Statement && semicolon_for_expr(context, expr) {
73        shape.sub_width(1).max_width_error(shape.width, expr.span)?
74    } else {
75        shape
76    };
77
78    let expr_rw = match expr.kind {
79        ast::ExprKind::Array(ref expr_vec) => rewrite_array(
80            "",
81            expr_vec.iter(),
82            expr.span,
83            context,
84            shape,
85            choose_separator_tactic(context, expr.span),
86            None,
87        ),
88        ast::ExprKind::Lit(token_lit) => {
89            if let Ok(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
90                Ok(expr_rw)
91            } else {
92                if let LitKind::StrRaw(_) = token_lit.kind {
93                    Ok(context.snippet(expr.span).trim().into())
94                } else {
95                    Err(RewriteError::Unknown)
96                }
97            }
98        }
99        ast::ExprKind::Call(ref callee, ref args) => {
100            let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
101            let callee_str = callee.rewrite_result(context, shape)?;
102            rewrite_call(context, &callee_str, args, inner_span, shape)
103        }
104        ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span),
105        ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
106            // FIXME: format comments between operands and operator
107            rewrite_all_pairs(expr, shape, context).or_else(|_| {
108                rewrite_pair(
109                    &**lhs,
110                    &**rhs,
111                    PairParts::infix(&format!(" {} ", context.snippet(op.span))),
112                    context,
113                    shape,
114                    context.config.binop_separator(),
115                )
116            })
117        }
118        ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
119        ast::ExprKind::Struct(ref struct_expr) => {
120            let ast::StructExpr {
121                qself,
122                fields,
123                path,
124                rest,
125            } = &**struct_expr;
126            rewrite_struct_lit(
127                context,
128                path,
129                qself,
130                fields,
131                rest,
132                &expr.attrs,
133                expr.span,
134                shape,
135            )
136        }
137        ast::ExprKind::Tup(ref items) => {
138            rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
139        }
140        ast::ExprKind::Use(_, _) => {
141            // FIXME: properly implement this
142            Ok(context.snippet(expr.span()).to_owned())
143        }
144        ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr),
145        ast::ExprKind::If(..)
146        | ast::ExprKind::ForLoop { .. }
147        | ast::ExprKind::Loop(..)
148        | ast::ExprKind::While(..) => to_control_flow(expr, expr_type)
149            .unknown_error()
150            .and_then(|control_flow| control_flow.rewrite_result(context, shape)),
151        ast::ExprKind::ConstBlock(ref anon_const) => {
152            let rewrite = match anon_const.value.kind {
153                ast::ExprKind::Block(ref block, opt_label) => {
154                    // Inner attributes are associated with the `ast::ExprKind::ConstBlock` node,
155                    // not the `ast::Block` node we're about to rewrite. To prevent dropping inner
156                    // attributes call `rewrite_block` directly.
157                    // See https://github.com/rust-lang/rustfmt/issues/6158
158                    rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)?
159                }
160                _ => anon_const.rewrite_result(context, shape)?,
161            };
162            Ok(format!("const {}", rewrite))
163        }
164        ast::ExprKind::Block(ref block, opt_label) => {
165            match expr_type {
166                ExprType::Statement => {
167                    if is_unsafe_block(block) {
168                        rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
169                    } else if let Some(rw) =
170                        rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
171                    {
172                        // Rewrite block without trying to put it in a single line.
173                        Ok(rw)
174                    } else {
175                        let prefix = block_prefix(context, block, shape)?;
176
177                        rewrite_block_with_visitor(
178                            context,
179                            &prefix,
180                            block,
181                            Some(&expr.attrs),
182                            opt_label,
183                            shape,
184                            true,
185                        )
186                    }
187                }
188                ExprType::SubExpression => {
189                    rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
190                }
191            }
192        }
193        ast::ExprKind::Match(ref cond, ref arms, kind) => {
194            rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind)
195        }
196        ast::ExprKind::Path(ref qself, ref path) => {
197            rewrite_path(context, PathContext::Expr, qself, path, shape)
198        }
199        ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
200            rewrite_assignment(context, lhs, rhs, None, shape)
201        }
202        ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
203            rewrite_assignment(context, lhs, rhs, Some(op), shape)
204        }
205        ast::ExprKind::Continue(ref opt_label) => {
206            let id_str = match *opt_label {
207                Some(label) => format!(" {}", label.ident),
208                None => String::new(),
209            };
210            Ok(format!("continue{id_str}"))
211        }
212        ast::ExprKind::Break(ref opt_label, ref opt_expr) => {
213            let id_str = match *opt_label {
214                Some(label) => format!(" {}", label.ident),
215                None => String::new(),
216            };
217
218            if let Some(ref expr) = *opt_expr {
219                rewrite_unary_prefix(context, &format!("break{id_str} "), &**expr, shape)
220            } else {
221                Ok(format!("break{id_str}"))
222            }
223        }
224        ast::ExprKind::Yield(ast::YieldKind::Prefix(ref opt_expr)) => {
225            if let Some(ref expr) = *opt_expr {
226                rewrite_unary_prefix(context, "yield ", &**expr, shape)
227            } else {
228                Ok("yield".to_string())
229            }
230        }
231        ast::ExprKind::Closure(ref cl) => closures::rewrite_closure(
232            &cl.binder,
233            cl.constness,
234            cl.capture_clause,
235            &cl.coroutine_kind,
236            cl.movability,
237            &cl.fn_decl,
238            &cl.body,
239            expr.span,
240            context,
241            shape,
242        ),
243        ast::ExprKind::Try(..)
244        | ast::ExprKind::Field(..)
245        | ast::ExprKind::MethodCall(..)
246        | ast::ExprKind::Await(_, _)
247        | ast::ExprKind::Yield(ast::YieldKind::Postfix(_)) => rewrite_chain(expr, context, shape),
248        ast::ExprKind::MacCall(ref mac) => {
249            rewrite_macro(mac, context, shape, MacroPosition::Expression).or_else(|_| {
250                wrap_str(
251                    context.snippet(expr.span).to_owned(),
252                    context.config.max_width(),
253                    shape,
254                )
255                .max_width_error(shape.width, expr.span)
256            })
257        }
258        ast::ExprKind::Ret(None) => Ok("return".to_owned()),
259        ast::ExprKind::Ret(Some(ref expr)) => {
260            rewrite_unary_prefix(context, "return ", &**expr, shape)
261        }
262        ast::ExprKind::Become(ref expr) => rewrite_unary_prefix(context, "become ", &**expr, shape),
263        ast::ExprKind::Yeet(None) => Ok("do yeet".to_owned()),
264        ast::ExprKind::Yeet(Some(ref expr)) => {
265            rewrite_unary_prefix(context, "do yeet ", &**expr, shape)
266        }
267        ast::ExprKind::AddrOf(borrow_kind, mutability, ref expr) => {
268            rewrite_expr_addrof(context, borrow_kind, mutability, expr, shape)
269        }
270        ast::ExprKind::Cast(ref expr, ref ty) => rewrite_pair(
271            &**expr,
272            &**ty,
273            PairParts::infix(" as "),
274            context,
275            shape,
276            SeparatorPlace::Front,
277        ),
278        ast::ExprKind::Index(ref expr, ref index, _) => {
279            rewrite_index(&**expr, &**index, context, shape)
280        }
281        ast::ExprKind::Repeat(ref expr, ref repeats) => rewrite_pair(
282            &**expr,
283            &*repeats.value,
284            PairParts::new("[", "; ", "]"),
285            context,
286            shape,
287            SeparatorPlace::Back,
288        ),
289        ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
290            let delim = match limits {
291                ast::RangeLimits::HalfOpen => "..",
292                ast::RangeLimits::Closed => "..=",
293            };
294
295            fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
296                match lhs.kind {
297                    ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit),
298                    ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr),
299                    ast::ExprKind::Binary(_, _, ref rhs_expr) => {
300                        needs_space_before_range(context, rhs_expr)
301                    }
302                    _ => false,
303                }
304            }
305
306            fn needs_space_after_range(rhs: &ast::Expr) -> bool {
307                // Don't format `.. ..` into `....`, which is invalid.
308                //
309                // This check is unnecessary for `lhs`, because a range
310                // starting from another range needs parentheses as `(x ..) ..`
311                // (`x .. ..` is a range from `x` to `..`).
312                matches!(rhs.kind, ast::ExprKind::Range(None, _, _))
313            }
314
315            let default_sp_delim = |lhs: Option<&ast::Expr>, rhs: Option<&ast::Expr>| {
316                let space_if = |b: bool| if b { " " } else { "" };
317
318                format!(
319                    "{}{}{}",
320                    lhs.map_or("", |lhs| space_if(needs_space_before_range(context, lhs))),
321                    delim,
322                    rhs.map_or("", |rhs| space_if(needs_space_after_range(rhs))),
323                )
324            };
325
326            match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
327                (Some(lhs), Some(rhs)) => {
328                    let sp_delim = if context.config.spaces_around_ranges() {
329                        format!(" {delim} ")
330                    } else {
331                        default_sp_delim(Some(lhs), Some(rhs))
332                    };
333                    rewrite_pair(
334                        &*lhs,
335                        &*rhs,
336                        PairParts::infix(&sp_delim),
337                        context,
338                        shape,
339                        context.config.binop_separator(),
340                    )
341                }
342                (None, Some(rhs)) => {
343                    let sp_delim = if context.config.spaces_around_ranges() {
344                        format!("{delim} ")
345                    } else {
346                        default_sp_delim(None, Some(rhs))
347                    };
348                    rewrite_unary_prefix(context, &sp_delim, &*rhs, shape)
349                }
350                (Some(lhs), None) => {
351                    let sp_delim = if context.config.spaces_around_ranges() {
352                        format!(" {delim}")
353                    } else {
354                        default_sp_delim(Some(lhs), None)
355                    };
356                    rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
357                }
358                (None, None) => Ok(delim.to_owned()),
359            }
360        }
361        // We do not format these expressions yet, but they should still
362        // satisfy our width restrictions.
363        // Style Guide RFC for InlineAsm variant pending
364        // https://github.com/rust-dev-tools/fmt-rfcs/issues/152
365        ast::ExprKind::InlineAsm(..) => Ok(context.snippet(expr.span).to_owned()),
366        ast::ExprKind::TryBlock(ref block, None) => {
367            if let rw @ Ok(_) =
368                rewrite_single_line_block(context, "try ", block, Some(&expr.attrs), None, shape)
369            {
370                rw
371            } else {
372                // FIXME: 9 sounds like `"do catch ".len()`, so may predate the rename
373                // 9 = `try `
374                let budget = shape.width.saturating_sub(9);
375                Ok(format!(
376                    "{}{}",
377                    "try ",
378                    rewrite_block(
379                        block,
380                        Some(&expr.attrs),
381                        None,
382                        context,
383                        Shape::legacy(budget, shape.indent)
384                    )?
385                ))
386            }
387        }
388        // FIXME: heterogeneous try blocks, which include a type so are harder to format
389        ast::ExprKind::TryBlock(_, Some(_)) => Err(RewriteError::Unknown),
390        ast::ExprKind::Gen(capture_by, ref block, ref kind, _) => {
391            let mover = if matches!(capture_by, ast::CaptureBy::Value { .. }) {
392                "move "
393            } else {
394                ""
395            };
396            if let rw @ Ok(_) = rewrite_single_line_block(
397                context,
398                format!("{kind} {mover}").as_str(),
399                block,
400                Some(&expr.attrs),
401                None,
402                shape,
403            ) {
404                rw
405            } else {
406                // 6 = `async `
407                let budget = shape.width.saturating_sub(6);
408                Ok(format!(
409                    "{kind} {mover}{}",
410                    rewrite_block(
411                        block,
412                        Some(&expr.attrs),
413                        None,
414                        context,
415                        Shape::legacy(budget, shape.indent)
416                    )?
417                ))
418            }
419        }
420        ast::ExprKind::Underscore => Ok("_".to_owned()),
421        ast::ExprKind::FormatArgs(..)
422        | ast::ExprKind::Type(..)
423        | ast::ExprKind::IncludedBytes(..)
424        | ast::ExprKind::OffsetOf(..)
425        | ast::ExprKind::UnsafeBinderCast(..) => {
426            // These don't normally occur in the AST because macros aren't expanded. However,
427            // rustfmt tries to parse macro arguments when formatting macros, so it's not totally
428            // impossible for rustfmt to come across one of these nodes when formatting a file.
429            // Also, rustfmt might get passed the output from `-Zunpretty=expanded`.
430            Err(RewriteError::Unknown)
431        }
432        ast::ExprKind::Err(_) | ast::ExprKind::Dummy => Err(RewriteError::Unknown),
433    };
434
435    expr_rw
436        .map(|expr_str| recover_comment_removed(expr_str, expr.span, context))
437        .and_then(|expr_str| {
438            let attrs = outer_attributes(&expr.attrs);
439            let attrs_str = attrs.rewrite_result(context, shape)?;
440            let span = mk_sp(
441                attrs.last().map_or(expr.span.lo(), |attr| attr.span.hi()),
442                expr.span.lo(),
443            );
444            combine_strs_with_missing_comments(context, &attrs_str, &expr_str, span, shape, false)
445        })
446}
447
448pub(crate) fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
449    name: &'a str,
450    exprs: impl Iterator<Item = &'a T>,
451    span: Span,
452    context: &'a RewriteContext<'_>,
453    shape: Shape,
454    force_separator_tactic: Option<SeparatorTactic>,
455    delim_token: Option<Delimiter>,
456) -> RewriteResult {
457    overflow::rewrite_with_square_brackets(
458        context,
459        name,
460        exprs,
461        shape,
462        span,
463        force_separator_tactic,
464        delim_token,
465    )
466}
467
468fn rewrite_empty_block(
469    context: &RewriteContext<'_>,
470    block: &ast::Block,
471    attrs: Option<&[ast::Attribute]>,
472    label: Option<ast::Label>,
473    prefix: &str,
474    shape: Shape,
475) -> Option<String> {
476    if block_has_statements(block) {
477        return None;
478    }
479
480    let label_str = rewrite_label(context, label);
481    if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
482        return None;
483    }
484
485    if !block_contains_comment(context, block) && shape.width >= 2 {
486        return Some(format!("{prefix}{label_str}{{}}"));
487    }
488
489    // If a block contains only a single-line comment, then leave it on one line.
490    let user_str = context.snippet(block.span);
491    let user_str = user_str.trim();
492    if user_str.starts_with('{') && user_str.ends_with('}') {
493        let comment_str = user_str[1..user_str.len() - 1].trim();
494        if block.stmts.is_empty()
495            && !comment_str.contains('\n')
496            && !comment_str.starts_with("//")
497            && comment_str.len() + 4 <= shape.width
498        {
499            return Some(format!("{prefix}{label_str}{{ {comment_str} }}"));
500        }
501    }
502
503    None
504}
505
506fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape) -> RewriteResult {
507    Ok(match block.rules {
508        ast::BlockCheckMode::Unsafe(..) => {
509            let snippet = context.snippet(block.span);
510            let open_pos = snippet.find_uncommented("{").unknown_error()?;
511            // Extract comment between unsafe and block start.
512            let trimmed = &snippet[6..open_pos].trim();
513
514            if !trimmed.is_empty() {
515                // 9 = "unsafe  {".len(), 7 = "unsafe ".len()
516                let budget = shape
517                    .width
518                    .checked_sub(9)
519                    .max_width_error(shape.width, block.span)?;
520                format!(
521                    "unsafe {} ",
522                    rewrite_comment(
523                        trimmed,
524                        true,
525                        Shape::legacy(budget, shape.indent + 7),
526                        context.config,
527                    )?
528                )
529            } else {
530                "unsafe ".to_owned()
531            }
532        }
533        ast::BlockCheckMode::Default => String::new(),
534    })
535}
536
537fn rewrite_single_line_block(
538    context: &RewriteContext<'_>,
539    prefix: &str,
540    block: &ast::Block,
541    attrs: Option<&[ast::Attribute]>,
542    label: Option<ast::Label>,
543    shape: Shape,
544) -> RewriteResult {
545    if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
546        let expr_shape = shape
547            .offset_left(last_line_width(prefix))
548            .max_width_error(shape.width, block_expr.span())?;
549        let expr_str = block_expr.rewrite_result(context, expr_shape)?;
550        let label_str = rewrite_label(context, label);
551        let result = format!("{prefix}{label_str}{{ {expr_str} }}");
552        if result.len() <= shape.width && !result.contains('\n') {
553            return Ok(result);
554        }
555    }
556    Err(RewriteError::Unknown)
557}
558
559pub(crate) fn rewrite_block_with_visitor(
560    context: &RewriteContext<'_>,
561    prefix: &str,
562    block: &ast::Block,
563    attrs: Option<&[ast::Attribute]>,
564    label: Option<ast::Label>,
565    shape: Shape,
566    has_braces: bool,
567) -> RewriteResult {
568    if let Some(rw_str) = rewrite_empty_block(context, block, attrs, label, prefix, shape) {
569        return Ok(rw_str);
570    }
571
572    let mut visitor = FmtVisitor::from_context(context);
573    visitor.block_indent = shape.indent;
574    visitor.is_if_else_block = context.is_if_else_block();
575    match (block.rules, label) {
576        (ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
577            let snippet = context.snippet(block.span);
578            let open_pos = snippet.find_uncommented("{").unknown_error()?;
579            visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
580        }
581        (ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
582    }
583
584    let inner_attrs = attrs.map(inner_attributes);
585    let label_str = rewrite_label(context, label);
586    visitor.visit_block(block, inner_attrs.as_deref(), has_braces);
587    let visitor_context = visitor.get_context();
588    context
589        .skipped_range
590        .borrow_mut()
591        .append(&mut visitor_context.skipped_range.borrow_mut());
592    Ok(format!("{}{}{}", prefix, label_str, visitor.buffer))
593}
594
595impl Rewrite for ast::Block {
596    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
597        self.rewrite_result(context, shape).ok()
598    }
599
600    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
601        rewrite_block(self, None, None, context, shape)
602    }
603}
604
605fn rewrite_block(
606    block: &ast::Block,
607    attrs: Option<&[ast::Attribute]>,
608    label: Option<ast::Label>,
609    context: &RewriteContext<'_>,
610    shape: Shape,
611) -> RewriteResult {
612    rewrite_block_inner(block, attrs, label, true, context, shape)
613}
614
615fn rewrite_block_inner(
616    block: &ast::Block,
617    attrs: Option<&[ast::Attribute]>,
618    label: Option<ast::Label>,
619    allow_single_line: bool,
620    context: &RewriteContext<'_>,
621    shape: Shape,
622) -> RewriteResult {
623    let prefix = block_prefix(context, block, shape)?;
624
625    // shape.width is used only for the single line case: either the empty block `{}`,
626    // or an unsafe expression `unsafe { e }`.
627    if let Some(rw_str) = rewrite_empty_block(context, block, attrs, label, &prefix, shape) {
628        return Ok(rw_str);
629    }
630
631    let result_str =
632        rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true)?;
633    if allow_single_line && result_str.lines().count() <= 3 {
634        if let rw @ Ok(_) = rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
635        {
636            return rw;
637        }
638    }
639    Ok(result_str)
640}
641
642/// Rewrite the divergent block of a `let-else` statement.
643pub(crate) fn rewrite_let_else_block(
644    block: &ast::Block,
645    allow_single_line: bool,
646    context: &RewriteContext<'_>,
647    shape: Shape,
648) -> RewriteResult {
649    rewrite_block_inner(block, None, None, allow_single_line, context, shape)
650}
651
652// Rewrite condition if the given expression has one.
653pub(crate) fn rewrite_cond(
654    context: &RewriteContext<'_>,
655    expr: &ast::Expr,
656    shape: Shape,
657) -> Option<String> {
658    match expr.kind {
659        ast::ExprKind::Match(ref cond, _, MatchKind::Prefix) => {
660            // `match `cond` {`
661            let cond_shape = match context.config.indent_style() {
662                IndentStyle::Visual => shape.shrink_left(6).and_then(|s| s.sub_width(2))?,
663                IndentStyle::Block => shape.offset_left(8)?,
664            };
665            cond.rewrite(context, cond_shape)
666        }
667        _ => to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| {
668            let alt_block_sep =
669                String::from("\n") + &shape.indent.block_only().to_string(context.config);
670            control_flow
671                .rewrite_cond(context, shape, &alt_block_sep)
672                .ok()
673                .map(|rw| rw.0)
674        }),
675    }
676}
677
678// Abstraction over control flow expressions
679#[derive(Debug)]
680struct ControlFlow<'a> {
681    cond: Option<&'a ast::Expr>,
682    block: &'a ast::Block,
683    else_block: Option<&'a ast::Expr>,
684    label: Option<ast::Label>,
685    pat: Option<&'a ast::Pat>,
686    keyword: &'a str,
687    matcher: &'a str,
688    connector: &'a str,
689    allow_single_line: bool,
690    // HACK: `true` if this is an `if` expression in an `else if`.
691    nested_if: bool,
692    span: Span,
693}
694
695fn extract_pats_and_cond(expr: &ast::Expr) -> (Option<&ast::Pat>, &ast::Expr) {
696    match expr.kind {
697        ast::ExprKind::Let(ref pat, ref cond, _, _) => (Some(pat), cond),
698        _ => (None, expr),
699    }
700}
701
702// FIXME: Refactor this.
703fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<'_>> {
704    match expr.kind {
705        ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
706            let (pat, cond) = extract_pats_and_cond(cond);
707            Some(ControlFlow::new_if(
708                cond,
709                pat,
710                if_block,
711                else_block.as_ref().map(|e| &**e),
712                expr_type == ExprType::SubExpression,
713                false,
714                expr.span,
715            ))
716        }
717        ast::ExprKind::ForLoop {
718            ref pat,
719            ref iter,
720            ref body,
721            label,
722            kind,
723        } => Some(ControlFlow::new_for(
724            pat, iter, body, label, expr.span, kind,
725        )),
726        ast::ExprKind::Loop(ref block, label, _) => {
727            Some(ControlFlow::new_loop(block, label, expr.span))
728        }
729        ast::ExprKind::While(ref cond, ref block, label) => {
730            let (pat, cond) = extract_pats_and_cond(cond);
731            Some(ControlFlow::new_while(pat, cond, block, label, expr.span))
732        }
733        _ => None,
734    }
735}
736
737fn choose_matcher(pat: Option<&ast::Pat>) -> &'static str {
738    pat.map_or("", |_| "let")
739}
740
741impl<'a> ControlFlow<'a> {
742    fn new_if(
743        cond: &'a ast::Expr,
744        pat: Option<&'a ast::Pat>,
745        block: &'a ast::Block,
746        else_block: Option<&'a ast::Expr>,
747        allow_single_line: bool,
748        nested_if: bool,
749        span: Span,
750    ) -> ControlFlow<'a> {
751        let matcher = choose_matcher(pat);
752        ControlFlow {
753            cond: Some(cond),
754            block,
755            else_block,
756            label: None,
757            pat,
758            keyword: "if",
759            matcher,
760            connector: " =",
761            allow_single_line,
762            nested_if,
763            span,
764        }
765    }
766
767    fn new_loop(block: &'a ast::Block, label: Option<ast::Label>, span: Span) -> ControlFlow<'a> {
768        ControlFlow {
769            cond: None,
770            block,
771            else_block: None,
772            label,
773            pat: None,
774            keyword: "loop",
775            matcher: "",
776            connector: "",
777            allow_single_line: false,
778            nested_if: false,
779            span,
780        }
781    }
782
783    fn new_while(
784        pat: Option<&'a ast::Pat>,
785        cond: &'a ast::Expr,
786        block: &'a ast::Block,
787        label: Option<ast::Label>,
788        span: Span,
789    ) -> ControlFlow<'a> {
790        let matcher = choose_matcher(pat);
791        ControlFlow {
792            cond: Some(cond),
793            block,
794            else_block: None,
795            label,
796            pat,
797            keyword: "while",
798            matcher,
799            connector: " =",
800            allow_single_line: false,
801            nested_if: false,
802            span,
803        }
804    }
805
806    fn new_for(
807        pat: &'a ast::Pat,
808        cond: &'a ast::Expr,
809        block: &'a ast::Block,
810        label: Option<ast::Label>,
811        span: Span,
812        kind: ForLoopKind,
813    ) -> ControlFlow<'a> {
814        ControlFlow {
815            cond: Some(cond),
816            block,
817            else_block: None,
818            label,
819            pat: Some(pat),
820            keyword: match kind {
821                ForLoopKind::For => "for",
822                ForLoopKind::ForAwait => "for await",
823            },
824            matcher: "",
825            connector: " in",
826            allow_single_line: false,
827            nested_if: false,
828            span,
829        }
830    }
831
832    fn rewrite_single_line(
833        &self,
834        pat_expr_str: &str,
835        context: &RewriteContext<'_>,
836        width: usize,
837    ) -> Option<String> {
838        assert!(self.allow_single_line);
839        let else_block = self.else_block?;
840        let fixed_cost = self.keyword.len() + "  {  } else {  }".len();
841
842        if let ast::ExprKind::Block(ref else_node, _) = else_block.kind {
843            let (if_expr, else_expr) = match (
844                stmt::Stmt::from_simple_block(context, self.block, None),
845                stmt::Stmt::from_simple_block(context, else_node, None),
846                pat_expr_str.contains('\n'),
847            ) {
848                (Some(if_expr), Some(else_expr), false) => (if_expr, else_expr),
849                _ => return None,
850            };
851
852            let new_width = width.checked_sub(pat_expr_str.len() + fixed_cost)?;
853            let if_str = if_expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;
854
855            let new_width = new_width.checked_sub(if_str.len())?;
856            let else_str = else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;
857
858            if if_str.contains('\n') || else_str.contains('\n') {
859                return None;
860            }
861
862            let result = format!(
863                "{} {} {{ {} }} else {{ {} }}",
864                self.keyword, pat_expr_str, if_str, else_str
865            );
866
867            if result.len() <= width {
868                return Some(result);
869            }
870        }
871
872        None
873    }
874}
875
876/// Returns `true` if the last line of pat_str has leading whitespace and it is wider than the
877/// shape's indent.
878fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool {
879    let mut leading_whitespaces = 0;
880    for c in pat_str.chars().rev() {
881        match c {
882            '\n' => break,
883            _ if c.is_whitespace() => leading_whitespaces += 1,
884            _ => leading_whitespaces = 0,
885        }
886    }
887    leading_whitespaces > start_column
888}
889
890impl<'a> ControlFlow<'a> {
891    fn rewrite_pat_expr(
892        &self,
893        context: &RewriteContext<'_>,
894        expr: &ast::Expr,
895        shape: Shape,
896        offset: usize,
897    ) -> RewriteResult {
898        debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, self.pat, expr);
899
900        let cond_shape = shape
901            .offset_left(offset)
902            .max_width_error(shape.width, expr.span)?;
903        if let Some(pat) = self.pat {
904            let matcher = if self.matcher.is_empty() {
905                self.matcher.to_owned()
906            } else {
907                format!("{} ", self.matcher)
908            };
909            let pat_shape = cond_shape
910                .offset_left(matcher.len())
911                .and_then(|s| s.sub_width(self.connector.len()))
912                .max_width_error(cond_shape.width, pat.span)?;
913            let pat_string = pat.rewrite_result(context, pat_shape)?;
914            let comments_lo = context
915                .snippet_provider
916                .span_after(self.span.with_lo(pat.span.hi()), self.connector.trim());
917            let comments_span = mk_sp(comments_lo, expr.span.lo());
918            return rewrite_assign_rhs_with_comments(
919                context,
920                &format!("{}{}{}", matcher, pat_string, self.connector),
921                expr,
922                cond_shape,
923                &RhsAssignKind::Expr(&expr.kind, expr.span),
924                RhsTactics::Default,
925                comments_span,
926                true,
927            );
928        }
929
930        let expr_rw = expr.rewrite_result(context, cond_shape);
931        // The expression may (partially) fit on the current line.
932        // We do not allow splitting between `if` and condition.
933        if self.keyword == "if" || expr_rw.is_ok() {
934            return expr_rw;
935        }
936
937        // The expression won't fit on the current line, jump to next.
938        let nested_shape = shape
939            .block_indent(context.config.tab_spaces())
940            .with_max_width(context.config);
941        let nested_indent_str = nested_shape.indent.to_string_with_newline(context.config);
942        expr.rewrite_result(context, nested_shape)
943            .map(|expr_rw| format!("{}{}", nested_indent_str, expr_rw))
944    }
945
946    fn rewrite_cond(
947        &self,
948        context: &RewriteContext<'_>,
949        shape: Shape,
950        alt_block_sep: &str,
951    ) -> Result<(String, usize), RewriteError> {
952        // Do not take the rhs overhead from the upper expressions into account
953        // when rewriting pattern.
954        let new_width = context.budget(shape.used_width());
955        let fresh_shape = Shape {
956            width: new_width,
957            ..shape
958        };
959        let constr_shape = if self.nested_if {
960            // We are part of an if-elseif-else chain. Our constraints are tightened.
961            // 7 = "} else " .len()
962            fresh_shape
963                .offset_left(7)
964                .max_width_error(fresh_shape.width, self.span)?
965        } else {
966            fresh_shape
967        };
968
969        let label_string = rewrite_label(context, self.label);
970        // 1 = space after keyword.
971        let offset = self.keyword.len() + label_string.len() + 1;
972
973        let pat_expr_string = match self.cond {
974            Some(cond) => self.rewrite_pat_expr(context, cond, constr_shape, offset)?,
975            None => String::new(),
976        };
977
978        let brace_overhead =
979            if context.config.control_brace_style() != ControlBraceStyle::AlwaysNextLine {
980                // 2 = ` {`
981                2
982            } else {
983                0
984            };
985        let one_line_budget = context
986            .config
987            .max_width()
988            .saturating_sub(constr_shape.used_width() + offset + brace_overhead);
989        let force_newline_brace = (pat_expr_string.contains('\n')
990            || pat_expr_string.len() > one_line_budget)
991            && (!last_line_extendable(&pat_expr_string)
992                || last_line_offsetted(shape.used_width(), &pat_expr_string));
993
994        // Try to format if-else on single line.
995        if self.allow_single_line && context.config.single_line_if_else_max_width() > 0 {
996            let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
997
998            if let Some(cond_str) = trial {
999                if cond_str.len() <= context.config.single_line_if_else_max_width() {
1000                    return Ok((cond_str, 0));
1001                }
1002            }
1003        }
1004
1005        let cond_span = if let Some(cond) = self.cond {
1006            cond.span
1007        } else {
1008            mk_sp(self.block.span.lo(), self.block.span.lo())
1009        };
1010
1011        // `for event in event`
1012        // Do not include label in the span.
1013        let lo = self
1014            .label
1015            .map_or(self.span.lo(), |label| label.ident.span.hi());
1016        let between_kwd_cond = mk_sp(
1017            context
1018                .snippet_provider
1019                .span_after(mk_sp(lo, self.span.hi()), self.keyword.trim()),
1020            if self.pat.is_none() {
1021                cond_span.lo()
1022            } else if self.matcher.is_empty() {
1023                self.pat.unwrap().span.lo()
1024            } else {
1025                context
1026                    .snippet_provider
1027                    .span_before(self.span, self.matcher.trim())
1028            },
1029        );
1030
1031        let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
1032
1033        let after_cond_comment =
1034            extract_comment(mk_sp(cond_span.hi(), self.block.span.lo()), context, shape);
1035
1036        let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
1037            ""
1038        } else if context.config.control_brace_style() == ControlBraceStyle::AlwaysNextLine
1039            || force_newline_brace
1040        {
1041            alt_block_sep
1042        } else {
1043            " "
1044        };
1045
1046        let used_width = if pat_expr_string.contains('\n') {
1047            last_line_width(&pat_expr_string)
1048        } else {
1049            // 2 = spaces after keyword and condition.
1050            label_string.len() + self.keyword.len() + pat_expr_string.len() + 2
1051        };
1052
1053        Ok((
1054            format!(
1055                "{}{}{}{}{}",
1056                label_string,
1057                self.keyword,
1058                between_kwd_cond_comment.as_ref().map_or(
1059                    if pat_expr_string.is_empty() || pat_expr_string.starts_with('\n') {
1060                        ""
1061                    } else {
1062                        " "
1063                    },
1064                    |s| &**s,
1065                ),
1066                pat_expr_string,
1067                after_cond_comment.as_ref().map_or(block_sep, |s| &**s)
1068            ),
1069            used_width,
1070        ))
1071    }
1072}
1073
1074/// Rewrite the `else` keyword with surrounding comments.
1075///
1076/// force_newline_else: whether or not to rewrite the `else` keyword on a newline.
1077/// is_last: true if this is an `else` and `false` if this is an `else if` block.
1078/// context: rewrite context
1079/// span: Span between the end of the last expression and the start of the else block,
1080///       which contains the `else` keyword
1081/// shape: Shape
1082pub(crate) fn rewrite_else_kw_with_comments(
1083    force_newline_else: bool,
1084    is_last: bool,
1085    context: &RewriteContext<'_>,
1086    span: Span,
1087    shape: Shape,
1088) -> String {
1089    let else_kw_lo = context.snippet_provider.span_before(span, "else");
1090    let before_else_kw = mk_sp(span.lo(), else_kw_lo);
1091    let before_else_kw_comment = extract_comment(before_else_kw, context, shape);
1092
1093    let else_kw_hi = context.snippet_provider.span_after(span, "else");
1094    let after_else_kw = mk_sp(else_kw_hi, span.hi());
1095    let after_else_kw_comment = extract_comment(after_else_kw, context, shape);
1096
1097    let newline_sep = &shape.indent.to_string_with_newline(context.config);
1098    let before_sep = match context.config.control_brace_style() {
1099        _ if force_newline_else => newline_sep.as_ref(),
1100        ControlBraceStyle::AlwaysNextLine | ControlBraceStyle::ClosingNextLine => {
1101            newline_sep.as_ref()
1102        }
1103        ControlBraceStyle::AlwaysSameLine => " ",
1104    };
1105    let after_sep = match context.config.control_brace_style() {
1106        ControlBraceStyle::AlwaysNextLine if is_last => newline_sep.as_ref(),
1107        _ => " ",
1108    };
1109
1110    format!(
1111        "{}else{}",
1112        before_else_kw_comment.as_ref().map_or(before_sep, |s| &**s),
1113        after_else_kw_comment.as_ref().map_or(after_sep, |s| &**s),
1114    )
1115}
1116
1117impl<'a> Rewrite for ControlFlow<'a> {
1118    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1119        self.rewrite_result(context, shape).ok()
1120    }
1121
1122    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1123        debug!("ControlFlow::rewrite {:?} {:?}", self, shape);
1124
1125        let alt_block_sep = &shape.indent.to_string_with_newline(context.config);
1126        let (cond_str, used_width) = self.rewrite_cond(context, shape, alt_block_sep)?;
1127        // If `used_width` is 0, it indicates that whole control flow is written in a single line.
1128        if used_width == 0 {
1129            return Ok(cond_str);
1130        }
1131
1132        let block_width = shape.width.saturating_sub(used_width);
1133        // This is used only for the empty block case: `{}`. So, we use 1 if we know
1134        // we should avoid the single line case.
1135        let block_width = if self.else_block.is_some() || self.nested_if {
1136            min(1, block_width)
1137        } else {
1138            block_width
1139        };
1140        let block_shape = Shape {
1141            width: block_width,
1142            ..shape
1143        };
1144        let block_str = {
1145            let old_val = context.is_if_else_block.replace(self.else_block.is_some());
1146            let result =
1147                rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true);
1148            context.is_if_else_block.replace(old_val);
1149            result?
1150        };
1151
1152        let mut result = format!("{cond_str}{block_str}");
1153
1154        if let Some(else_block) = self.else_block {
1155            let shape = Shape::indented(shape.indent, context.config);
1156            let mut last_in_chain = false;
1157            let rewrite = match else_block.kind {
1158                // If the else expression is another if-else expression, prevent it
1159                // from being formatted on a single line.
1160                // Note how we're passing the original shape, as the
1161                // cost of "else" should not cascade.
1162                ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
1163                    let (pats, cond) = extract_pats_and_cond(cond);
1164                    ControlFlow::new_if(
1165                        cond,
1166                        pats,
1167                        if_block,
1168                        next_else_block.as_ref().map(|e| &**e),
1169                        false,
1170                        true,
1171                        mk_sp(else_block.span.lo(), self.span.hi()),
1172                    )
1173                    .rewrite_result(context, shape)
1174                }
1175                _ => {
1176                    last_in_chain = true;
1177                    // When rewriting a block, the width is only used for single line
1178                    // blocks, passing 1 lets us avoid that.
1179                    let else_shape = Shape {
1180                        width: min(1, shape.width),
1181                        ..shape
1182                    };
1183                    format_expr(else_block, ExprType::Statement, context, else_shape)
1184                }
1185            };
1186
1187            let else_kw = rewrite_else_kw_with_comments(
1188                false,
1189                last_in_chain,
1190                context,
1191                self.block.span.between(else_block.span),
1192                shape,
1193            );
1194            result.push_str(&else_kw);
1195            result.push_str(&rewrite?);
1196        }
1197
1198        Ok(result)
1199    }
1200}
1201
1202fn rewrite_label(context: &RewriteContext<'_>, opt_label: Option<ast::Label>) -> Cow<'static, str> {
1203    match opt_label {
1204        Some(label) => Cow::from(format!("{}: ", context.snippet(label.ident.span))),
1205        None => Cow::from(""),
1206    }
1207}
1208
1209fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1210    match rewrite_missing_comment(span, shape, context) {
1211        Ok(ref comment) if !comment.is_empty() => Some(format!(
1212            "{indent}{comment}{indent}",
1213            indent = shape.indent.to_string_with_newline(context.config)
1214        )),
1215        _ => None,
1216    }
1217}
1218
1219pub(crate) fn block_contains_comment(context: &RewriteContext<'_>, block: &ast::Block) -> bool {
1220    contains_comment(context.snippet(block.span))
1221}
1222
1223// Checks that a block contains no statements, an expression and no comments or
1224// attributes.
1225// FIXME: incorrectly returns false when comment is contained completely within
1226// the expression.
1227pub(crate) fn is_simple_block(
1228    context: &RewriteContext<'_>,
1229    block: &ast::Block,
1230    attrs: Option<&[ast::Attribute]>,
1231) -> bool {
1232    block.stmts.len() == 1
1233        && stmt_is_expr(&block.stmts[0])
1234        && !block_contains_comment(context, block)
1235        && attrs.map_or(true, |a| a.is_empty())
1236}
1237
1238/// Checks whether a block contains at most one statement or expression, and no
1239/// comments or attributes.
1240pub(crate) fn is_simple_block_stmt(
1241    context: &RewriteContext<'_>,
1242    block: &ast::Block,
1243    attrs: Option<&[ast::Attribute]>,
1244) -> bool {
1245    block.stmts.len() <= 1
1246        && !block_contains_comment(context, block)
1247        && attrs.map_or(true, |a| a.is_empty())
1248}
1249
1250fn block_has_statements(block: &ast::Block) -> bool {
1251    block
1252        .stmts
1253        .iter()
1254        .any(|stmt| !matches!(stmt.kind, ast::StmtKind::Empty))
1255}
1256
1257/// Checks whether a block contains no statements, expressions, comments, or
1258/// inner attributes.
1259pub(crate) fn is_empty_block(
1260    context: &RewriteContext<'_>,
1261    block: &ast::Block,
1262    attrs: Option<&[ast::Attribute]>,
1263) -> bool {
1264    !block_has_statements(block)
1265        && !block_contains_comment(context, block)
1266        && attrs.map_or(true, |a| inner_attributes(a).is_empty())
1267}
1268
1269pub(crate) fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
1270    matches!(stmt.kind, ast::StmtKind::Expr(..))
1271}
1272
1273pub(crate) fn is_unsafe_block(block: &ast::Block) -> bool {
1274    matches!(block.rules, ast::BlockCheckMode::Unsafe(..))
1275}
1276
1277pub(crate) fn rewrite_literal(
1278    context: &RewriteContext<'_>,
1279    token_lit: token::Lit,
1280    span: Span,
1281    shape: Shape,
1282) -> RewriteResult {
1283    match token_lit.kind {
1284        token::LitKind::Str => rewrite_string_lit(context, span, shape),
1285        token::LitKind::Integer => rewrite_int_lit(context, token_lit, span, shape),
1286        _ => wrap_str(
1287            context.snippet(span).to_owned(),
1288            context.config.max_width(),
1289            shape,
1290        )
1291        .max_width_error(shape.width, span),
1292    }
1293}
1294
1295fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> RewriteResult {
1296    let string_lit = context.snippet(span);
1297
1298    if !context.config.format_strings() {
1299        if string_lit
1300            .lines()
1301            .dropping_back(1)
1302            .all(|line| line.ends_with('\\'))
1303            && context.config.style_edition() >= StyleEdition::Edition2024
1304        {
1305            return Ok(string_lit.to_owned());
1306        } else {
1307            return wrap_str(string_lit.to_owned(), context.config.max_width(), shape)
1308                .max_width_error(shape.width, span);
1309        }
1310    }
1311
1312    // Remove the quote characters.
1313    let str_lit = &string_lit[1..string_lit.len() - 1];
1314
1315    rewrite_string(
1316        str_lit,
1317        &StringFormat::new(shape.visual_indent(0), context.config),
1318        shape.width.saturating_sub(2),
1319    )
1320    .max_width_error(shape.width, span)
1321}
1322
1323fn rewrite_int_lit(
1324    context: &RewriteContext<'_>,
1325    token_lit: token::Lit,
1326    span: Span,
1327    shape: Shape,
1328) -> RewriteResult {
1329    let symbol = token_lit.symbol.as_str();
1330
1331    if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
1332        let hex_lit = match context.config.hex_literal_case() {
1333            HexLiteralCase::Preserve => None,
1334            HexLiteralCase::Upper => Some(symbol_stripped.to_ascii_uppercase()),
1335            HexLiteralCase::Lower => Some(symbol_stripped.to_ascii_lowercase()),
1336        };
1337        if let Some(hex_lit) = hex_lit {
1338            return wrap_str(
1339                format!(
1340                    "0x{}{}",
1341                    hex_lit,
1342                    token_lit.suffix.as_ref().map_or("", |s| s.as_str())
1343                ),
1344                context.config.max_width(),
1345                shape,
1346            )
1347            .max_width_error(shape.width, span);
1348        }
1349    }
1350
1351    wrap_str(
1352        context.snippet(span).to_owned(),
1353        context.config.max_width(),
1354        shape,
1355    )
1356    .max_width_error(shape.width, span)
1357}
1358
1359fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
1360    if context.inside_macro() {
1361        if span_ends_with_comma(context, span) {
1362            Some(SeparatorTactic::Always)
1363        } else {
1364            Some(SeparatorTactic::Never)
1365        }
1366    } else {
1367        None
1368    }
1369}
1370
1371pub(crate) fn rewrite_call(
1372    context: &RewriteContext<'_>,
1373    callee: &str,
1374    args: &[Box<ast::Expr>],
1375    span: Span,
1376    shape: Shape,
1377) -> RewriteResult {
1378    overflow::rewrite_with_parens(
1379        context,
1380        callee,
1381        args.iter(),
1382        shape,
1383        span,
1384        context.config.fn_call_width(),
1385        choose_separator_tactic(context, span),
1386    )
1387}
1388
1389pub(crate) fn is_simple_expr(expr: &ast::Expr) -> bool {
1390    match expr.kind {
1391        ast::ExprKind::Lit(..) => true,
1392        ast::ExprKind::Path(ref qself, ref path) => qself.is_none() && path.segments.len() <= 1,
1393        ast::ExprKind::AddrOf(_, _, ref expr)
1394        | ast::ExprKind::Cast(ref expr, _)
1395        | ast::ExprKind::Field(ref expr, _)
1396        | ast::ExprKind::Try(ref expr)
1397        | ast::ExprKind::Unary(_, ref expr) => is_simple_expr(expr),
1398        ast::ExprKind::Index(ref lhs, ref rhs, _) => is_simple_expr(lhs) && is_simple_expr(rhs),
1399        ast::ExprKind::Repeat(ref lhs, ref rhs) => {
1400            is_simple_expr(lhs) && is_simple_expr(&*rhs.value)
1401        }
1402        _ => false,
1403    }
1404}
1405
1406pub(crate) fn is_every_expr_simple(lists: &[OverflowableItem<'_>]) -> bool {
1407    lists.iter().all(OverflowableItem::is_simple)
1408}
1409
1410pub(crate) fn can_be_overflowed_expr(
1411    context: &RewriteContext<'_>,
1412    expr: &ast::Expr,
1413    args_len: usize,
1414) -> bool {
1415    match expr.kind {
1416        _ if !expr.attrs.is_empty() => false,
1417        ast::ExprKind::Match(..) => {
1418            (context.use_block_indent() && args_len == 1)
1419                || (context.config.indent_style() == IndentStyle::Visual && args_len > 1)
1420                || context.config.overflow_delimited_expr()
1421        }
1422        ast::ExprKind::If(..)
1423        | ast::ExprKind::ForLoop { .. }
1424        | ast::ExprKind::Loop(..)
1425        | ast::ExprKind::While(..) => {
1426            context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
1427        }
1428
1429        // Handle always block-like expressions
1430        ast::ExprKind::Gen(..) | ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => true,
1431
1432        // Handle `[]` and `{}`-like expressions
1433        ast::ExprKind::Array(..) | ast::ExprKind::Struct(..) => {
1434            context.config.overflow_delimited_expr()
1435                || (context.use_block_indent() && args_len == 1)
1436        }
1437        ast::ExprKind::MacCall(ref mac) => {
1438            match (mac.args.delim, context.config.overflow_delimited_expr()) {
1439                (Delimiter::Bracket, true) | (Delimiter::Brace, true) => true,
1440                _ => context.use_block_indent() && args_len == 1,
1441            }
1442        }
1443
1444        // Handle parenthetical expressions
1445        ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Tup(..) => {
1446            context.use_block_indent() && args_len == 1
1447        }
1448
1449        // Handle unary-like expressions
1450        ast::ExprKind::AddrOf(_, _, ref expr)
1451        | ast::ExprKind::Try(ref expr)
1452        | ast::ExprKind::Unary(_, ref expr)
1453        | ast::ExprKind::Cast(ref expr, _) => can_be_overflowed_expr(context, expr, args_len),
1454        _ => false,
1455    }
1456}
1457
1458pub(crate) fn is_nested_call(expr: &ast::Expr) -> bool {
1459    match expr.kind {
1460        ast::ExprKind::Call(..) | ast::ExprKind::MacCall(..) => true,
1461        ast::ExprKind::AddrOf(_, _, ref expr)
1462        | ast::ExprKind::Try(ref expr)
1463        | ast::ExprKind::Unary(_, ref expr)
1464        | ast::ExprKind::Cast(ref expr, _) => is_nested_call(expr),
1465        _ => false,
1466    }
1467}
1468
1469/// Returns `true` if a function call or a method call represented by the given span ends with a
1470/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
1471/// comma from macro can potentially break the code.
1472pub(crate) fn span_ends_with_comma(context: &RewriteContext<'_>, span: Span) -> bool {
1473    let mut result: bool = Default::default();
1474    let mut prev_char: char = Default::default();
1475    let closing_delimiters = &[')', '}', ']'];
1476
1477    for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
1478        match c {
1479            _ if kind.is_comment() || c.is_whitespace() => continue,
1480            c if closing_delimiters.contains(&c) => {
1481                result &= !closing_delimiters.contains(&prev_char);
1482            }
1483            ',' => result = true,
1484            _ => result = false,
1485        }
1486        prev_char = c;
1487    }
1488
1489    result
1490}
1491
1492pub(crate) fn rewrite_paren(
1493    context: &RewriteContext<'_>,
1494    mut subexpr: &ast::Expr,
1495    shape: Shape,
1496    mut span: Span,
1497) -> RewriteResult {
1498    debug!("rewrite_paren, shape: {:?}", shape);
1499
1500    // Extract comments within parens.
1501    let mut pre_span;
1502    let mut post_span;
1503    let mut pre_comment;
1504    let mut post_comment;
1505    let remove_nested_parens = context.config.remove_nested_parens();
1506    loop {
1507        // 1 = "(" or ")"
1508        pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span().lo());
1509        post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
1510        pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
1511        post_comment = rewrite_missing_comment(post_span, shape, context)?;
1512
1513        // Remove nested parens if there are no comments.
1514        if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.kind {
1515            if remove_nested_parens && pre_comment.is_empty() && post_comment.is_empty() {
1516                span = subexpr.span;
1517                subexpr = subsubexpr;
1518                continue;
1519            }
1520        }
1521
1522        break;
1523    }
1524
1525    // 1 = `(` and `)`
1526    let sub_shape = shape
1527        .offset_left(1)
1528        .and_then(|s| s.sub_width(1))
1529        .max_width_error(shape.width, span)?;
1530    let subexpr_str = subexpr.rewrite_result(context, sub_shape)?;
1531    let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
1532    if fits_single_line {
1533        Ok(format!("({pre_comment}{subexpr_str}{post_comment})"))
1534    } else {
1535        rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
1536    }
1537}
1538
1539fn rewrite_paren_in_multi_line(
1540    context: &RewriteContext<'_>,
1541    subexpr: &ast::Expr,
1542    shape: Shape,
1543    pre_span: Span,
1544    post_span: Span,
1545) -> RewriteResult {
1546    let nested_indent = shape.indent.block_indent(context.config);
1547    let nested_shape = Shape::indented(nested_indent, context.config);
1548    let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
1549    let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
1550    let subexpr_str = subexpr.rewrite_result(context, nested_shape)?;
1551
1552    let mut result = String::with_capacity(subexpr_str.len() * 2);
1553    result.push('(');
1554    if !pre_comment.is_empty() {
1555        result.push_str(&nested_indent.to_string_with_newline(context.config));
1556        result.push_str(&pre_comment);
1557    }
1558    result.push_str(&nested_indent.to_string_with_newline(context.config));
1559    result.push_str(&subexpr_str);
1560    if !post_comment.is_empty() {
1561        result.push_str(&nested_indent.to_string_with_newline(context.config));
1562        result.push_str(&post_comment);
1563    }
1564    result.push_str(&shape.indent.to_string_with_newline(context.config));
1565    result.push(')');
1566
1567    Ok(result)
1568}
1569
1570fn rewrite_index(
1571    expr: &ast::Expr,
1572    index: &ast::Expr,
1573    context: &RewriteContext<'_>,
1574    shape: Shape,
1575) -> RewriteResult {
1576    let expr_str = expr.rewrite_result(context, shape)?;
1577
1578    let offset = last_line_width(&expr_str) + 1;
1579    let rhs_overhead = shape.rhs_overhead(context.config);
1580    let index_shape = if expr_str.contains('\n') {
1581        Shape::legacy(context.config.max_width(), shape.indent)
1582            .offset_left(offset)
1583            .and_then(|shape| shape.sub_width(1 + rhs_overhead))
1584    } else {
1585        match context.config.indent_style() {
1586            IndentStyle::Block => shape
1587                .offset_left(offset)
1588                .and_then(|shape| shape.sub_width(1)),
1589            IndentStyle::Visual => shape.visual_indent(offset).sub_width(offset + 1),
1590        }
1591    }
1592    .max_width_error(shape.width, index.span());
1593    let orig_index_rw = index_shape.and_then(|s| index.rewrite_result(context, s));
1594
1595    // Return if index fits in a single line.
1596    match orig_index_rw {
1597        Ok(ref index_str) if !index_str.contains('\n') => {
1598            return Ok(format!("{expr_str}[{index_str}]"));
1599        }
1600        _ => (),
1601    }
1602
1603    // Try putting index on the next line and see if it fits in a single line.
1604    let indent = shape.indent.block_indent(context.config);
1605    let index_shape = Shape::indented(indent, context.config)
1606        .offset_left(1)
1607        .max_width_error(shape.width, index.span())?;
1608    let index_shape = index_shape
1609        .sub_width(1 + rhs_overhead)
1610        .max_width_error(index_shape.width, index.span())?;
1611    let new_index_rw = index.rewrite_result(context, index_shape);
1612    match (orig_index_rw, new_index_rw) {
1613        (_, Ok(ref new_index_str)) if !new_index_str.contains('\n') => Ok(format!(
1614            "{}{}[{}]",
1615            expr_str,
1616            indent.to_string_with_newline(context.config),
1617            new_index_str,
1618        )),
1619        (Err(_), Ok(ref new_index_str)) => Ok(format!(
1620            "{}{}[{}]",
1621            expr_str,
1622            indent.to_string_with_newline(context.config),
1623            new_index_str,
1624        )),
1625        (Ok(ref index_str), _) => Ok(format!("{expr_str}[{index_str}]")),
1626        // When both orig_index_rw and new_index_rw result in errors, we currently propagate the
1627        // error from the second attempt since it is more generous with width constraints.
1628        // This decision is somewhat arbitrary and is open to change.
1629        (Err(_), Err(new_index_rw_err)) => Err(new_index_rw_err),
1630    }
1631}
1632
1633fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool {
1634    !has_base && fields.iter().all(|field| !field.is_shorthand)
1635}
1636
1637fn rewrite_struct_lit<'a>(
1638    context: &RewriteContext<'_>,
1639    path: &ast::Path,
1640    qself: &Option<Box<ast::QSelf>>,
1641    fields: &'a [ast::ExprField],
1642    struct_rest: &ast::StructRest,
1643    attrs: &[ast::Attribute],
1644    span: Span,
1645    shape: Shape,
1646) -> RewriteResult {
1647    debug!("rewrite_struct_lit: shape {:?}", shape);
1648
1649    enum StructLitField<'a> {
1650        Regular(&'a ast::ExprField),
1651        Base(&'a ast::Expr),
1652        Rest(Span),
1653    }
1654
1655    // 2 = " {".len()
1656    let path_shape = shape.sub_width(2).max_width_error(shape.width, span)?;
1657    let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
1658
1659    let has_base_or_rest = match struct_rest {
1660        ast::StructRest::None if fields.is_empty() => return Ok(format!("{path_str} {{}}")),
1661        ast::StructRest::Rest(_) if fields.is_empty() => {
1662            return Ok(format!("{path_str} {{ .. }}"));
1663        }
1664        ast::StructRest::Rest(_) | ast::StructRest::Base(_) => true,
1665        _ => false,
1666    };
1667
1668    // Foo { a: Foo } - indent is +3, width is -5.
1669    let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)
1670        .max_width_error(shape.width, span)?;
1671
1672    let one_line_width = h_shape.map_or(0, |shape| shape.width);
1673    let body_lo = context.snippet_provider.span_after(span, "{");
1674    let fields_str = if struct_lit_can_be_aligned(fields, has_base_or_rest)
1675        && context.config.struct_field_align_threshold() > 0
1676    {
1677        rewrite_with_alignment(
1678            fields,
1679            context,
1680            v_shape,
1681            mk_sp(body_lo, span.hi()),
1682            one_line_width,
1683        )
1684        .unknown_error()?
1685    } else {
1686        let field_iter = fields.iter().map(StructLitField::Regular).chain(
1687            match struct_rest {
1688                ast::StructRest::Base(expr) => Some(StructLitField::Base(&**expr)),
1689                ast::StructRest::Rest(span) => Some(StructLitField::Rest(*span)),
1690                ast::StructRest::None => None,
1691            }
1692            .into_iter(),
1693        );
1694
1695        let span_lo = |item: &StructLitField<'_>| match *item {
1696            StructLitField::Regular(field) => field.span().lo(),
1697            StructLitField::Base(expr) => {
1698                let last_field_hi = fields.last().map_or(span.lo(), |field| field.span.hi());
1699                let snippet = context.snippet(mk_sp(last_field_hi, expr.span.lo()));
1700                let pos = snippet.find_uncommented("..").unwrap();
1701                last_field_hi + BytePos(pos as u32)
1702            }
1703            StructLitField::Rest(span) => span.lo(),
1704        };
1705        let span_hi = |item: &StructLitField<'_>| match *item {
1706            StructLitField::Regular(field) => field.span().hi(),
1707            StructLitField::Base(expr) => expr.span.hi(),
1708            StructLitField::Rest(span) => span.hi(),
1709        };
1710        let rewrite = |item: &StructLitField<'_>| match *item {
1711            StructLitField::Regular(field) => {
1712                // The 1 taken from the v_budget is for the comma.
1713                rewrite_field(
1714                    context,
1715                    field,
1716                    v_shape.sub_width(1).max_width_error(v_shape.width, span)?,
1717                    0,
1718                )
1719            }
1720            StructLitField::Base(expr) => {
1721                // 2 = ..
1722                expr.rewrite_result(
1723                    context,
1724                    v_shape
1725                        .offset_left(2)
1726                        .max_width_error(v_shape.width, span)?,
1727                )
1728                .map(|s| format!("..{}", s))
1729            }
1730            StructLitField::Rest(_) => Ok("..".to_owned()),
1731        };
1732
1733        let items = itemize_list(
1734            context.snippet_provider,
1735            field_iter,
1736            "}",
1737            ",",
1738            span_lo,
1739            span_hi,
1740            rewrite,
1741            body_lo,
1742            span.hi(),
1743            false,
1744        );
1745        let item_vec = items.collect::<Vec<_>>();
1746
1747        let tactic = struct_lit_tactic(h_shape, context, &item_vec);
1748        let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
1749
1750        let ends_with_comma = span_ends_with_comma(context, span);
1751        let force_no_trailing_comma = context.inside_macro() && !ends_with_comma;
1752
1753        let fmt = struct_lit_formatting(
1754            nested_shape,
1755            tactic,
1756            context,
1757            force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(),
1758        );
1759
1760        write_list(&item_vec, &fmt)?
1761    };
1762
1763    let fields_str =
1764        wrap_struct_field(context, attrs, &fields_str, shape, v_shape, one_line_width)?;
1765    Ok(format!("{path_str} {{{fields_str}}}"))
1766
1767    // FIXME if context.config.indent_style() == Visual, but we run out
1768    // of space, we should fall back to BlockIndent.
1769}
1770
1771pub(crate) fn wrap_struct_field(
1772    context: &RewriteContext<'_>,
1773    attrs: &[ast::Attribute],
1774    fields_str: &str,
1775    shape: Shape,
1776    nested_shape: Shape,
1777    one_line_width: usize,
1778) -> RewriteResult {
1779    let should_vertical = context.config.indent_style() == IndentStyle::Block
1780        && (fields_str.contains('\n')
1781            || !context.config.struct_lit_single_line()
1782            || fields_str.len() > one_line_width);
1783
1784    let inner_attrs = &inner_attributes(attrs);
1785    if inner_attrs.is_empty() {
1786        if should_vertical {
1787            Ok(format!(
1788                "{}{}{}",
1789                nested_shape.indent.to_string_with_newline(context.config),
1790                fields_str,
1791                shape.indent.to_string_with_newline(context.config)
1792            ))
1793        } else {
1794            // One liner or visual indent.
1795            Ok(format!(" {fields_str} "))
1796        }
1797    } else {
1798        Ok(format!(
1799            "{}{}{}{}{}",
1800            nested_shape.indent.to_string_with_newline(context.config),
1801            inner_attrs.rewrite_result(context, shape)?,
1802            nested_shape.indent.to_string_with_newline(context.config),
1803            fields_str,
1804            shape.indent.to_string_with_newline(context.config)
1805        ))
1806    }
1807}
1808
1809pub(crate) fn struct_lit_field_separator(config: &Config) -> &str {
1810    colon_spaces(config)
1811}
1812
1813pub(crate) fn rewrite_field(
1814    context: &RewriteContext<'_>,
1815    field: &ast::ExprField,
1816    shape: Shape,
1817    prefix_max_width: usize,
1818) -> RewriteResult {
1819    if contains_skip(&field.attrs) {
1820        return Ok(context.snippet(field.span()).to_owned());
1821    }
1822    let mut attrs_str = field.attrs.rewrite_result(context, shape)?;
1823    if !attrs_str.is_empty() {
1824        attrs_str.push_str(&shape.indent.to_string_with_newline(context.config));
1825    };
1826    let name = context.snippet(field.ident.span);
1827    if field.is_shorthand {
1828        Ok(attrs_str + name)
1829    } else {
1830        let mut separator = String::from(struct_lit_field_separator(context.config));
1831        for _ in 0..prefix_max_width.saturating_sub(name.len()) {
1832            separator.push(' ');
1833        }
1834        let overhead = name.len() + separator.len();
1835        let expr_shape = shape
1836            .offset_left(overhead)
1837            .max_width_error(shape.width, field.span)?;
1838        let expr = field.expr.rewrite_result(context, expr_shape);
1839        let is_lit = matches!(field.expr.kind, ast::ExprKind::Lit(_));
1840        match expr {
1841            Ok(ref e)
1842                if !is_lit && e.as_str() == name && context.config.use_field_init_shorthand() =>
1843            {
1844                Ok(attrs_str + name)
1845            }
1846            Ok(e) => Ok(format!("{attrs_str}{name}{separator}{e}")),
1847            Err(_) => {
1848                let expr_offset = shape.indent.block_indent(context.config);
1849                let expr = field
1850                    .expr
1851                    .rewrite_result(context, Shape::indented(expr_offset, context.config));
1852                expr.map(|s| {
1853                    format!(
1854                        "{}{}:\n{}{}",
1855                        attrs_str,
1856                        name,
1857                        expr_offset.to_string(context.config),
1858                        s
1859                    )
1860                })
1861            }
1862        }
1863    }
1864}
1865
1866fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
1867    context: &RewriteContext<'_>,
1868    mut items: impl Iterator<Item = &'a T>,
1869    span: Span,
1870    shape: Shape,
1871    is_singleton_tuple: bool,
1872) -> RewriteResult {
1873    // In case of length 1, need a trailing comma
1874    debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
1875    if is_singleton_tuple {
1876        // 3 = "(" + ",)"
1877        let nested_shape = shape
1878            .sub_width(3)
1879            .max_width_error(shape.width, span)?
1880            .visual_indent(1);
1881        return items
1882            .next()
1883            .unwrap()
1884            .rewrite_result(context, nested_shape)
1885            .map(|s| format!("({},)", s));
1886    }
1887
1888    let list_lo = context.snippet_provider.span_after(span, "(");
1889    let nested_shape = shape
1890        .sub_width(2)
1891        .max_width_error(shape.width, span)?
1892        .visual_indent(1);
1893    let items = itemize_list(
1894        context.snippet_provider,
1895        items,
1896        ")",
1897        ",",
1898        |item| item.span().lo(),
1899        |item| item.span().hi(),
1900        |item| item.rewrite_result(context, nested_shape),
1901        list_lo,
1902        span.hi() - BytePos(1),
1903        false,
1904    );
1905    let item_vec: Vec<_> = items.collect();
1906    let tactic = definitive_tactic(
1907        &item_vec,
1908        ListTactic::HorizontalVertical,
1909        Separator::Comma,
1910        nested_shape.width,
1911    );
1912    let fmt = ListFormatting::new(nested_shape, context.config)
1913        .tactic(tactic)
1914        .ends_with_newline(false);
1915    let list_str = write_list(&item_vec, &fmt)?;
1916
1917    Ok(format!("({list_str})"))
1918}
1919
1920fn rewrite_let(
1921    context: &RewriteContext<'_>,
1922    shape: Shape,
1923    pat: &ast::Pat,
1924    expr: &ast::Expr,
1925) -> RewriteResult {
1926    let mut result = "let ".to_owned();
1927
1928    // TODO(ytmimi) comments could appear between `let` and the `pat`
1929
1930    // 4 = "let ".len()
1931    let pat_shape = shape
1932        .offset_left(4)
1933        .max_width_error(shape.width, pat.span)?;
1934    let pat_str = pat.rewrite_result(context, pat_shape)?;
1935    result.push_str(&pat_str);
1936
1937    // TODO(ytmimi) comments could appear between `pat` and `=`
1938    result.push_str(" =");
1939
1940    let comments_lo = context
1941        .snippet_provider
1942        .span_after(expr.span.with_lo(pat.span.hi()), "=");
1943    let comments_span = mk_sp(comments_lo, expr.span.lo());
1944    rewrite_assign_rhs_with_comments(
1945        context,
1946        result,
1947        expr,
1948        shape,
1949        &RhsAssignKind::Expr(&expr.kind, expr.span),
1950        RhsTactics::Default,
1951        comments_span,
1952        true,
1953    )
1954}
1955
1956pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
1957    context: &'a RewriteContext<'_>,
1958    items: impl Iterator<Item = &'a T>,
1959    span: Span,
1960    shape: Shape,
1961    is_singleton_tuple: bool,
1962) -> RewriteResult {
1963    debug!("rewrite_tuple {:?}", shape);
1964    if context.use_block_indent() {
1965        // We use the same rule as function calls for rewriting tuples.
1966        let force_tactic = if context.inside_macro() {
1967            if span_ends_with_comma(context, span) {
1968                Some(SeparatorTactic::Always)
1969            } else {
1970                Some(SeparatorTactic::Never)
1971            }
1972        } else if is_singleton_tuple {
1973            Some(SeparatorTactic::Always)
1974        } else {
1975            None
1976        };
1977        overflow::rewrite_with_parens(
1978            context,
1979            "",
1980            items,
1981            shape,
1982            span,
1983            context.config.fn_call_width(),
1984            force_tactic,
1985        )
1986    } else {
1987        rewrite_tuple_in_visual_indent_style(context, items, span, shape, is_singleton_tuple)
1988    }
1989}
1990
1991pub(crate) fn rewrite_unary_prefix<R: Rewrite + Spanned>(
1992    context: &RewriteContext<'_>,
1993    prefix: &str,
1994    rewrite: &R,
1995    shape: Shape,
1996) -> RewriteResult {
1997    let shape = shape
1998        .offset_left(prefix.len())
1999        .max_width_error(shape.width, rewrite.span())?;
2000    rewrite
2001        .rewrite_result(context, shape)
2002        .map(|r| format!("{}{}", prefix, r))
2003}
2004
2005// FIXME: this is probably not correct for multi-line Rewrites. we should
2006// subtract suffix.len() from the last line budget, not the first!
2007pub(crate) fn rewrite_unary_suffix<R: Rewrite + Spanned>(
2008    context: &RewriteContext<'_>,
2009    suffix: &str,
2010    rewrite: &R,
2011    shape: Shape,
2012) -> RewriteResult {
2013    let shape = shape
2014        .sub_width(suffix.len())
2015        .max_width_error(shape.width, rewrite.span())?;
2016    rewrite.rewrite_result(context, shape).map(|mut r| {
2017        r.push_str(suffix);
2018        r
2019    })
2020}
2021
2022fn rewrite_unary_op(
2023    context: &RewriteContext<'_>,
2024    op: ast::UnOp,
2025    expr: &ast::Expr,
2026    shape: Shape,
2027) -> RewriteResult {
2028    // For some reason, an UnOp is not spanned like BinOp!
2029    rewrite_unary_prefix(context, op.as_str(), expr, shape)
2030}
2031
2032pub(crate) enum RhsAssignKind<'ast> {
2033    Expr(&'ast ast::ExprKind, #[allow(dead_code)] Span),
2034    Bounds,
2035    Ty,
2036}
2037
2038impl<'ast> RhsAssignKind<'ast> {
2039    // TODO(calebcartwright)
2040    // Preemptive addition for handling RHS with chains, not yet utilized.
2041    // It may make more sense to construct the chain first and then check
2042    // whether there are actually chain elements.
2043    #[allow(dead_code)]
2044    fn is_chain(&self) -> bool {
2045        match self {
2046            RhsAssignKind::Expr(kind, _) => {
2047                matches!(
2048                    kind,
2049                    ast::ExprKind::Try(..)
2050                        | ast::ExprKind::Field(..)
2051                        | ast::ExprKind::MethodCall(..)
2052                        | ast::ExprKind::Await(_, _)
2053                )
2054            }
2055            _ => false,
2056        }
2057    }
2058}
2059
2060fn rewrite_assignment(
2061    context: &RewriteContext<'_>,
2062    lhs: &ast::Expr,
2063    rhs: &ast::Expr,
2064    op: Option<&ast::AssignOp>,
2065    shape: Shape,
2066) -> RewriteResult {
2067    let operator_str = match op {
2068        Some(op) => context.snippet(op.span),
2069        None => "=",
2070    };
2071
2072    // 1 = space between lhs and operator.
2073    let lhs_shape = shape
2074        .sub_width(operator_str.len() + 1)
2075        .max_width_error(shape.width, lhs.span())?;
2076    let lhs_str = format!(
2077        "{} {}",
2078        lhs.rewrite_result(context, lhs_shape)?,
2079        operator_str
2080    );
2081
2082    rewrite_assign_rhs(
2083        context,
2084        lhs_str,
2085        rhs,
2086        &RhsAssignKind::Expr(&rhs.kind, rhs.span),
2087        shape,
2088    )
2089}
2090
2091/// Controls where to put the rhs.
2092#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2093pub(crate) enum RhsTactics {
2094    /// Use heuristics.
2095    Default,
2096    /// Put the rhs on the next line if it uses multiple line, without extra indentation.
2097    ForceNextLineWithoutIndent,
2098    /// Allow overflowing max width if neither `Default` nor `ForceNextLineWithoutIndent`
2099    /// did not work.
2100    AllowOverflow,
2101}
2102
2103// The left hand side must contain everything up to, and including, the
2104// assignment operator.
2105pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
2106    context: &RewriteContext<'_>,
2107    lhs: S,
2108    ex: &R,
2109    rhs_kind: &RhsAssignKind<'_>,
2110    shape: Shape,
2111) -> RewriteResult {
2112    rewrite_assign_rhs_with(context, lhs, ex, shape, rhs_kind, RhsTactics::Default)
2113}
2114
2115pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
2116    context: &RewriteContext<'_>,
2117    lhs: &str,
2118    ex: &R,
2119    shape: Shape,
2120    rhs_kind: &RhsAssignKind<'_>,
2121    rhs_tactics: RhsTactics,
2122) -> RewriteResult {
2123    let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
2124        shape.indent.width()
2125    } else {
2126        0
2127    });
2128    // 1 = space between operator and rhs.
2129    let orig_shape = shape.offset_left(last_line_width + 1).unwrap_or(Shape {
2130        width: 0,
2131        offset: shape.offset + last_line_width + 1,
2132        ..shape
2133    });
2134    let has_rhs_comment = if let Some(offset) = lhs.find_last_uncommented("=") {
2135        lhs.trim_end().len() > offset + 1
2136    } else {
2137        false
2138    };
2139
2140    choose_rhs(
2141        context,
2142        ex,
2143        orig_shape,
2144        ex.rewrite_result(context, orig_shape),
2145        rhs_kind,
2146        rhs_tactics,
2147        has_rhs_comment,
2148    )
2149}
2150
2151pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
2152    context: &RewriteContext<'_>,
2153    lhs: S,
2154    ex: &R,
2155    shape: Shape,
2156    rhs_kind: &RhsAssignKind<'_>,
2157    rhs_tactics: RhsTactics,
2158) -> RewriteResult {
2159    let lhs = lhs.into();
2160    let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
2161    Ok(lhs + &rhs)
2162}
2163
2164pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite + Spanned>(
2165    context: &RewriteContext<'_>,
2166    lhs: S,
2167    ex: &R,
2168    shape: Shape,
2169    rhs_kind: &RhsAssignKind<'_>,
2170    rhs_tactics: RhsTactics,
2171    between_span: Span,
2172    allow_extend: bool,
2173) -> RewriteResult {
2174    let lhs = lhs.into();
2175    let contains_comment = contains_comment(context.snippet(between_span));
2176    let shape = if contains_comment {
2177        shape
2178            .block_left(context.config.tab_spaces())
2179            .max_width_error(shape.width, between_span.with_hi(ex.span().hi()))?
2180    } else {
2181        shape
2182    };
2183    let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
2184    if contains_comment {
2185        let rhs = rhs.trim_start();
2186        combine_strs_with_missing_comments(context, &lhs, rhs, between_span, shape, allow_extend)
2187    } else {
2188        Ok(lhs + &rhs)
2189    }
2190}
2191
2192fn choose_rhs<R: Rewrite>(
2193    context: &RewriteContext<'_>,
2194    expr: &R,
2195    shape: Shape,
2196    orig_rhs: RewriteResult,
2197    _rhs_kind: &RhsAssignKind<'_>,
2198    rhs_tactics: RhsTactics,
2199    has_rhs_comment: bool,
2200) -> RewriteResult {
2201    match orig_rhs {
2202        Ok(ref new_str) if new_str.is_empty() => Ok(String::new()),
2203        Ok(ref new_str) if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width => {
2204            Ok(format!(" {new_str}"))
2205        }
2206        _ => {
2207            // Expression did not fit on the same line as the identifier.
2208            // Try splitting the line and see if that works better.
2209            let new_shape = shape_from_rhs_tactic(context, shape, rhs_tactics)
2210                // TODO(ding-young) Ideally, we can replace unknown_error() with max_width_error(),
2211                // but this requires either implementing the Spanned trait for ast::GenericBounds
2212                // or grabbing the span from the call site.
2213                .unknown_error()?;
2214            let new_rhs = expr.rewrite_result(context, new_shape);
2215            let new_indent_str = &shape
2216                .indent
2217                .block_indent(context.config)
2218                .to_string_with_newline(context.config);
2219            let before_space_str = if has_rhs_comment { "" } else { " " };
2220
2221            match (orig_rhs, new_rhs) {
2222                (Ok(ref orig_rhs), Ok(ref new_rhs))
2223                    if !filtered_str_fits(&new_rhs, context.config.max_width(), new_shape) =>
2224                {
2225                    Ok(format!("{before_space_str}{orig_rhs}"))
2226                }
2227                (Ok(ref orig_rhs), Ok(ref new_rhs))
2228                    if prefer_next_line(orig_rhs, new_rhs, rhs_tactics) =>
2229                {
2230                    Ok(format!("{new_indent_str}{new_rhs}"))
2231                }
2232                (Err(_), Ok(ref new_rhs)) => Ok(format!("{new_indent_str}{new_rhs}")),
2233                (Err(_), Err(_)) if rhs_tactics == RhsTactics::AllowOverflow => {
2234                    let shape = shape.infinite_width();
2235                    expr.rewrite_result(context, shape)
2236                        .map(|s| format!("{}{}", before_space_str, s))
2237                }
2238                // When both orig_rhs and new_rhs result in errors, we currently propagate
2239                // the error from the second attempt since it is more generous with
2240                // width constraints. This decision is somewhat arbitrary and is open to change.
2241                (Err(_), Err(new_rhs_err)) => Err(new_rhs_err),
2242                (Ok(orig_rhs), _) => Ok(format!("{before_space_str}{orig_rhs}")),
2243            }
2244        }
2245    }
2246}
2247
2248fn shape_from_rhs_tactic(
2249    context: &RewriteContext<'_>,
2250    shape: Shape,
2251    rhs_tactic: RhsTactics,
2252) -> Option<Shape> {
2253    match rhs_tactic {
2254        RhsTactics::ForceNextLineWithoutIndent => shape
2255            .with_max_width(context.config)
2256            .sub_width(shape.indent.width()),
2257        RhsTactics::Default | RhsTactics::AllowOverflow => {
2258            Shape::indented(shape.indent.block_indent(context.config), context.config)
2259                .sub_width(shape.rhs_overhead(context.config))
2260        }
2261    }
2262}
2263
2264/// Returns true if formatting next_line_rhs is better on a new line when compared to the
2265/// original's line formatting.
2266///
2267/// It is considered better if:
2268/// 1. the tactic is ForceNextLineWithoutIndent
2269/// 2. next_line_rhs doesn't have newlines
2270/// 3. the original line has more newlines than next_line_rhs
2271/// 4. the original formatting of the first line ends with `(`, `{`, or `[` and next_line_rhs
2272///    doesn't
2273pub(crate) fn prefer_next_line(
2274    orig_rhs: &str,
2275    next_line_rhs: &str,
2276    rhs_tactics: RhsTactics,
2277) -> bool {
2278    rhs_tactics == RhsTactics::ForceNextLineWithoutIndent
2279        || !next_line_rhs.contains('\n')
2280        || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
2281        || first_line_ends_with(orig_rhs, '(') && !first_line_ends_with(next_line_rhs, '(')
2282        || first_line_ends_with(orig_rhs, '{') && !first_line_ends_with(next_line_rhs, '{')
2283        || first_line_ends_with(orig_rhs, '[') && !first_line_ends_with(next_line_rhs, '[')
2284}
2285
2286fn rewrite_expr_addrof(
2287    context: &RewriteContext<'_>,
2288    borrow_kind: ast::BorrowKind,
2289    mutability: ast::Mutability,
2290    expr: &ast::Expr,
2291    shape: Shape,
2292) -> RewriteResult {
2293    let operator_str = match (mutability, borrow_kind) {
2294        (ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
2295        (ast::Mutability::Not, ast::BorrowKind::Pin) => "&pin const ",
2296        (ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
2297        (ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
2298        (ast::Mutability::Mut, ast::BorrowKind::Pin) => "&pin mut ",
2299        (ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
2300    };
2301    rewrite_unary_prefix(context, operator_str, expr, shape)
2302}
2303
2304pub(crate) fn is_method_call(expr: &ast::Expr) -> bool {
2305    match expr.kind {
2306        ast::ExprKind::MethodCall(..) => true,
2307        ast::ExprKind::AddrOf(_, _, ref expr)
2308        | ast::ExprKind::Cast(ref expr, _)
2309        | ast::ExprKind::Try(ref expr)
2310        | ast::ExprKind::Unary(_, ref expr) => is_method_call(expr),
2311        _ => false,
2312    }
2313}
2314
2315#[cfg(test)]
2316mod test {
2317    use super::last_line_offsetted;
2318
2319    #[test]
2320    fn test_last_line_offsetted() {
2321        let lines = "one\n    two";
2322        assert_eq!(last_line_offsetted(2, lines), true);
2323        assert_eq!(last_line_offsetted(4, lines), false);
2324        assert_eq!(last_line_offsetted(6, lines), false);
2325
2326        let lines = "one    two";
2327        assert_eq!(last_line_offsetted(2, lines), false);
2328        assert_eq!(last_line_offsetted(0, lines), false);
2329
2330        let lines = "\ntwo";
2331        assert_eq!(last_line_offsetted(2, lines), false);
2332        assert_eq!(last_line_offsetted(0, lines), false);
2333
2334        let lines = "one\n    two      three";
2335        assert_eq!(last_line_offsetted(2, lines), true);
2336        let lines = "one\n two      three";
2337        assert_eq!(last_line_offsetted(2, lines), false);
2338    }
2339}