diff options
| author | Yacin Tmimi <ytmimi@horizonmedia.com> | 2021-10-11 18:30:35 -0400 |
|---|---|---|
| committer | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2021-10-12 19:37:48 -0500 |
| commit | d41805704d4a07ac77e0bc59ea5c91cdb69dea26 (patch) | |
| tree | 00945d8a92acedc367eb33ee52187fa8bd3087bb /src | |
| parent | 8b58cce673d6ae4fbc1f3a8d28859bc100a72ed8 (diff) | |
| download | rust-d41805704d4a07ac77e0bc59ea5c91cdb69dea26.tar.gz rust-d41805704d4a07ac77e0bc59ea5c91cdb69dea26.zip | |
Prevent structs with ".." from being rewritten with alignment
rustfmt should only support rewriting a struct in an expression position with alignment (non-default behavior) when there is no rest (with or without a base) and all of the fields are non-shorthand.
Diffstat (limited to 'src')
| -rw-r--r-- | src/expr.rs | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/expr.rs b/src/expr.rs index 01cc388c186..3a54426b0dd 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1528,12 +1528,12 @@ fn rewrite_struct_lit<'a>( let path_shape = shape.sub_width(2)?; let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?; - let has_base = match struct_rest { + let has_base_or_rest = match struct_rest { ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)), ast::StructRest::Rest(_) if fields.is_empty() => { return Some(format!("{} {{ .. }}", path_str)); } - ast::StructRest::Base(_) => true, + ast::StructRest::Rest(_) | ast::StructRest::Base(_) => true, _ => false, }; @@ -1542,7 +1542,7 @@ fn rewrite_struct_lit<'a>( let one_line_width = h_shape.map_or(0, |shape| shape.width); let body_lo = context.snippet_provider.span_after(span, "{"); - let fields_str = if struct_lit_can_be_aligned(fields, has_base) + let fields_str = if struct_lit_can_be_aligned(fields, has_base_or_rest) && context.config.struct_field_align_threshold() > 0 { rewrite_with_alignment( @@ -1614,10 +1614,7 @@ fn rewrite_struct_lit<'a>( nested_shape, tactic, context, - force_no_trailing_comma - || has_base - || !context.use_block_indent() - || matches!(struct_rest, ast::StructRest::Rest(_)), + force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(), ); write_list(&item_vec, &fmt)? |
