|
1 | 1 | use std::borrow::Cow;
|
2 | 2 | use std::cmp::min;
|
3 | 3 | use std::collections::HashMap;
|
| 4 | +use std::ops::Deref; |
4 | 5 |
|
5 | 6 | use itertools::Itertools;
|
6 | 7 | use rustc_ast::token::{Delimiter, LitKind};
|
@@ -282,14 +283,31 @@ fn format_expr_inner(
|
282 | 283 | ast::ExprKind::AddrOf(borrow_kind, mutability, ref expr) => {
|
283 | 284 | rewrite_expr_addrof(context, borrow_kind, mutability, expr, shape)
|
284 | 285 | }
|
285 |
| - ast::ExprKind::Cast(ref expr, ref ty) => rewrite_pair( |
286 |
| - &**expr, |
287 |
| - &**ty, |
288 |
| - PairParts::infix(" as "), |
289 |
| - context, |
290 |
| - shape, |
291 |
| - SeparatorPlace::Front, |
292 |
| - ), |
| 286 | + ast::ExprKind::Cast(ref expr, ref ty) => { |
| 287 | + let mut result = String::new(); |
| 288 | + let empty_angle_brackets = ty_ends_with_empty_angle_brackets(ty); |
| 289 | + let shape = if empty_angle_brackets { |
| 290 | + result.push('('); |
| 291 | + shape.sub_width(2)? |
| 292 | + } else { |
| 293 | + shape |
| 294 | + }; |
| 295 | + |
| 296 | + result.push_str(&rewrite_pair( |
| 297 | + &**expr, |
| 298 | + &**ty, |
| 299 | + PairParts::infix(" as "), |
| 300 | + context, |
| 301 | + shape, |
| 302 | + SeparatorPlace::Front, |
| 303 | + )?); |
| 304 | + |
| 305 | + if empty_angle_brackets { |
| 306 | + result.push(')'); |
| 307 | + } |
| 308 | + |
| 309 | + Some(result) |
| 310 | + } |
293 | 311 | ast::ExprKind::Type(ref expr, ref ty) => rewrite_pair(
|
294 | 312 | &**expr,
|
295 | 313 | &**ty,
|
@@ -458,6 +476,21 @@ fn format_expr_inner(
|
458 | 476 | })
|
459 | 477 | }
|
460 | 478 |
|
| 479 | +fn ty_ends_with_empty_angle_brackets(ty: &ast::Ty) -> bool { |
| 480 | + if let ast::TyKind::Path(_, path) = &ty.kind { |
| 481 | + matches!( |
| 482 | + path.segments.last(), |
| 483 | + Some(ast::PathSegment {args: Some(generic_args), ..}) |
| 484 | + if matches!( |
| 485 | + generic_args.deref(), |
| 486 | + ast::GenericArgs::AngleBracketed(bracket_args) if bracket_args.args.is_empty() |
| 487 | + ) |
| 488 | + ) |
| 489 | + } else { |
| 490 | + false |
| 491 | + } |
| 492 | +} |
| 493 | + |
461 | 494 | pub(crate) fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
|
462 | 495 | name: &'a str,
|
463 | 496 | exprs: impl Iterator<Item = &'a T>,
|
|
0 commit comments