Skip to main content

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