Skip to content

Commit d726492

Browse files
committed
Remove spaces_within_parens_and_brackets
cc #1974
1 parent 7b6d2b4 commit d726492

File tree

14 files changed

+56
-527
lines changed

14 files changed

+56
-527
lines changed

Configurations.md

-46
Original file line numberDiff line numberDiff line change
@@ -1589,52 +1589,6 @@ fn main() {
15891589
}
15901590
```
15911591

1592-
## `spaces_within_parens_and_brackets`
1593-
1594-
Put spaces within non-empty generic arguments, parentheses, and square brackets
1595-
1596-
- **Default value**: `false`
1597-
- **Possible values**: `true`, `false`
1598-
- **Stable**: No
1599-
1600-
#### `false` (default):
1601-
1602-
```rust
1603-
// generic arguments
1604-
fn lorem<T: Eq>(t: T) {
1605-
// body
1606-
}
1607-
1608-
// non-empty parentheses
1609-
fn lorem<T: Eq>(t: T) {
1610-
let lorem = (ipsum, dolor);
1611-
}
1612-
1613-
// non-empty square brackets
1614-
fn lorem<T: Eq>(t: T) {
1615-
let lorem: [usize; 2] = [ipsum, dolor];
1616-
}
1617-
```
1618-
1619-
#### `true`:
1620-
1621-
```rust
1622-
// generic arguments
1623-
fn lorem< T: Eq >( t: T ) {
1624-
// body
1625-
}
1626-
1627-
// non-empty parentheses
1628-
fn lorem< T: Eq >( t: T ) {
1629-
let lorem = ( ipsum, dolor );
1630-
}
1631-
1632-
// non-empty square brackets
1633-
fn lorem< T: Eq >( t: T ) {
1634-
let lorem: [ usize; 2 ] = [ ipsum, dolor ];
1635-
}
1636-
```
1637-
16381592
## `struct_lit_single_line`
16391593

16401594
Put small struct literals on a single line

src/chains.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,7 @@ fn rewrite_method_call(
498498
.map(|ty| ty.rewrite(context, shape))
499499
.collect::<Option<Vec<_>>>()?;
500500

501-
let type_str =
502-
if context.config.spaces_within_parens_and_brackets() && !type_list.is_empty() {
503-
format!("::< {} >", type_list.join(", "))
504-
} else {
505-
format!("::<{}>", type_list.join(", "))
506-
};
501+
let type_str = format!("::<{}>", type_list.join(", "));
507502

508503
(types.last().unwrap().span.hi(), type_str)
509504
};

src/config/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ create_config! {
7777
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
7878
space_before_colon: bool, false, false, "Leave a space before the colon";
7979
space_after_colon: bool, true, false, "Leave a space after the colon";
80-
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ... range operators";
81-
spaces_within_parens_and_brackets: bool, false, false,
82-
"Put spaces within non-empty parentheses or brackets";
80+
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
8381
binop_separator: SeparatorPlace, SeparatorPlace::Front, false,
8482
"Where to put a binary operator when a binary expression goes multiline.";
8583

src/expr.rs

+26-65
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use string::{rewrite_string, StringFormat};
3939
use types::{can_be_overflowed_type, rewrite_path, PathContext};
4040
use utils::{
4141
colon_spaces, contains_skip, count_newlines, first_line_width, inner_attributes,
42-
last_line_extendable, last_line_width, mk_sp, outer_attributes, paren_overhead,
43-
ptr_vec_to_ref_vec, semicolon_for_stmt, wrap_str,
42+
last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
43+
semicolon_for_stmt, wrap_str,
4444
};
4545
use vertical::rewrite_with_alignment;
4646
use visitor::FmtVisitor;
@@ -223,21 +223,14 @@ pub fn format_expr(
223223
ast::ExprKind::Index(ref expr, ref index) => {
224224
rewrite_index(&**expr, &**index, context, shape)
225225
}
226-
ast::ExprKind::Repeat(ref expr, ref repeats) => {
227-
let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
228-
("[ ", " ]")
229-
} else {
230-
("[", "]")
231-
};
232-
rewrite_pair(
233-
&**expr,
234-
&**repeats,
235-
PairParts::new(lbr, "; ", rbr),
236-
context,
237-
shape,
238-
SeparatorPlace::Back,
239-
)
240-
}
226+
ast::ExprKind::Repeat(ref expr, ref repeats) => rewrite_pair(
227+
&**expr,
228+
&**repeats,
229+
PairParts::new("[", "; ", "]"),
230+
context,
231+
shape,
232+
SeparatorPlace::Back,
233+
),
241234
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
242235
let delim = match limits {
243236
ast::RangeLimits::HalfOpen => "..",
@@ -1581,27 +1574,15 @@ fn rewrite_paren(
15811574
break;
15821575
}
15831576

1584-
let total_paren_overhead = paren_overhead(context);
1585-
let paren_overhead = total_paren_overhead / 2;
1586-
let sub_shape = shape
1587-
.offset_left(paren_overhead)
1588-
.and_then(|s| s.sub_width(paren_overhead))?;
1589-
1590-
let paren_wrapper = |s: &str| {
1591-
if context.config.spaces_within_parens_and_brackets() && !s.is_empty() {
1592-
format!("( {}{}{} )", pre_comment, s, post_comment)
1593-
} else {
1594-
format!("({}{}{})", pre_comment, s, post_comment)
1595-
}
1596-
};
1577+
// 1 `(`
1578+
let sub_shape = shape.offset_left(1).and_then(|s| s.sub_width(1))?;
15971579

15981580
let subexpr_str = subexpr.rewrite(context, sub_shape)?;
15991581
debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
16001582

1601-
if subexpr_str.contains('\n')
1602-
|| first_line_width(&subexpr_str) + total_paren_overhead <= shape.width
1603-
{
1604-
Some(paren_wrapper(&subexpr_str))
1583+
// 2 = `()`
1584+
if subexpr_str.contains('\n') || first_line_width(&subexpr_str) + 2 <= shape.width {
1585+
Some(format!("({}{}{})", pre_comment, &subexpr_str, post_comment))
16051586
} else {
16061587
None
16071588
}
@@ -1615,54 +1596,44 @@ fn rewrite_index(
16151596
) -> Option<String> {
16161597
let expr_str = expr.rewrite(context, shape)?;
16171598

1618-
let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
1619-
("[ ", " ]")
1620-
} else {
1621-
("[", "]")
1622-
};
1623-
1624-
let offset = last_line_width(&expr_str) + lbr.len();
1599+
let offset = last_line_width(&expr_str) + 1;
16251600
let rhs_overhead = shape.rhs_overhead(context.config);
16261601
let index_shape = if expr_str.contains('\n') {
16271602
Shape::legacy(context.config.max_width(), shape.indent)
16281603
.offset_left(offset)
1629-
.and_then(|shape| shape.sub_width(rbr.len() + rhs_overhead))
1604+
.and_then(|shape| shape.sub_width(1 + rhs_overhead))
16301605
} else {
1631-
shape.visual_indent(offset).sub_width(offset + rbr.len())
1606+
shape.visual_indent(offset).sub_width(offset + 1)
16321607
};
16331608
let orig_index_rw = index_shape.and_then(|s| index.rewrite(context, s));
16341609

16351610
// Return if index fits in a single line.
16361611
match orig_index_rw {
16371612
Some(ref index_str) if !index_str.contains('\n') => {
1638-
return Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr));
1613+
return Some(format!("{}[{}]", expr_str, index_str));
16391614
}
16401615
_ => (),
16411616
}
16421617

16431618
// Try putting index on the next line and see if it fits in a single line.
16441619
let indent = shape.indent.block_indent(context.config);
1645-
let index_shape = Shape::indented(indent, context.config).offset_left(lbr.len())?;
1646-
let index_shape = index_shape.sub_width(rbr.len() + rhs_overhead)?;
1620+
let index_shape = Shape::indented(indent, context.config).offset_left(1)?;
1621+
let index_shape = index_shape.sub_width(1 + rhs_overhead)?;
16471622
let new_index_rw = index.rewrite(context, index_shape);
16481623
match (orig_index_rw, new_index_rw) {
16491624
(_, Some(ref new_index_str)) if !new_index_str.contains('\n') => Some(format!(
1650-
"{}{}{}{}{}",
1625+
"{}{}[{}]",
16511626
expr_str,
16521627
indent.to_string_with_newline(context.config),
1653-
lbr,
16541628
new_index_str,
1655-
rbr
16561629
)),
16571630
(None, Some(ref new_index_str)) => Some(format!(
1658-
"{}{}{}{}{}",
1631+
"{}{}[{}]",
16591632
expr_str,
16601633
indent.to_string_with_newline(context.config),
1661-
lbr,
16621634
new_index_str,
1663-
rbr
16641635
)),
1665-
(Some(ref index_str), _) => Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr)),
1636+
(Some(ref index_str), _) => Some(format!("{}[{}]", expr_str, index_str)),
16661637
_ => None,
16671638
}
16681639
}
@@ -1877,13 +1848,7 @@ where
18771848
.next()
18781849
.unwrap()
18791850
.rewrite(context, nested_shape)
1880-
.map(|s| {
1881-
if context.config.spaces_within_parens_and_brackets() {
1882-
format!("( {}, )", s)
1883-
} else {
1884-
format!("({},)", s)
1885-
}
1886-
});
1851+
.map(|s| format!("({},)", s));
18871852
}
18881853

18891854
let list_lo = context.snippet_provider.span_after(span, "(");
@@ -1919,11 +1884,7 @@ where
19191884
};
19201885
let list_str = write_list(&item_vec, &fmt)?;
19211886

1922-
if context.config.spaces_within_parens_and_brackets() && !list_str.is_empty() {
1923-
Some(format!("( {} )", list_str))
1924-
} else {
1925-
Some(format!("({})", list_str))
1926-
}
1887+
Some(format!("({})", list_str))
19271888
}
19281889

19291890
pub fn rewrite_tuple<'a, T>(

src/items.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1915,12 +1915,6 @@ fn rewrite_fn_base(
19151915
} else {
19161916
result.push('(');
19171917
}
1918-
if context.config.spaces_within_parens_and_brackets()
1919-
&& !fd.inputs.is_empty()
1920-
&& result.ends_with('(')
1921-
{
1922-
result.push(' ')
1923-
}
19241918

19251919
// Skip `pub(crate)`.
19261920
let lo_after_visibility = get_bytepos_after_visibility(&fn_sig.visibility, span);
@@ -1978,9 +1972,6 @@ fn rewrite_fn_base(
19781972
if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
19791973
result.push('\n');
19801974
}
1981-
if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty() {
1982-
result.push(' ')
1983-
}
19841975
// If the last line of args contains comment, we cannot put the closing paren
19851976
// on the same line.
19861977
if arg_str

src/macros.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -259,32 +259,25 @@ pub fn rewrite_macro_inner(
259259
// Handle special case: `vec![expr; expr]`
260260
if vec_with_semi {
261261
let mac_shape = shape.offset_left(macro_name.len())?;
262-
let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
263-
("[ ", " ]")
264-
} else {
265-
("[", "]")
266-
};
267-
// 6 = `vec!` + `; `
268-
let total_overhead = lbr.len() + rbr.len() + 6;
262+
// 8 = `vec![]` + `; `
263+
let total_overhead = 8;
269264
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
270265
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
271266
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
272267
if !lhs.contains('\n')
273268
&& !rhs.contains('\n')
274269
&& lhs.len() + rhs.len() + total_overhead <= shape.width
275270
{
276-
Some(format!("{}{}{}; {}{}", macro_name, lbr, lhs, rhs, rbr))
271+
Some(format!("{}[{}; {}]", macro_name, lhs, rhs))
277272
} else {
278273
Some(format!(
279-
"{}{}{}{};{}{}{}{}",
274+
"{}[{}{};{}{}{}]",
280275
macro_name,
281-
lbr,
282276
nested_shape.indent.to_string_with_newline(context.config),
283277
lhs,
284278
nested_shape.indent.to_string_with_newline(context.config),
285279
rhs,
286280
shape.indent.to_string_with_newline(context.config),
287-
rbr
288281
))
289282
}
290283
} else {

src/overflow.rs

+10-27
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListIte
2323
use rewrite::{Rewrite, RewriteContext};
2424
use shape::Shape;
2525
use spanned::Spanned;
26-
use utils::{
27-
count_newlines, extra_offset, first_line_width, last_line_width, mk_sp, paren_overhead,
28-
};
26+
use utils::{count_newlines, extra_offset, first_line_width, last_line_width, mk_sp};
2927

3028
use std::cmp::min;
3129

@@ -140,26 +138,16 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
140138
force_separator_tactic: Option<SeparatorTactic>,
141139
custom_delims: Option<(&'a str, &'a str)>,
142140
) -> Context<'a, T> {
143-
// 2 = `( `, 1 = `(`
144-
let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
145-
2
146-
} else {
147-
1
148-
};
149141
let used_width = extra_offset(ident, shape);
150-
let one_line_width = shape.width.saturating_sub(used_width + 2 * paren_overhead);
142+
// 1 = `()`
143+
let one_line_width = shape.width.saturating_sub(used_width + 2);
151144

152145
// 1 = "(" or ")"
153146
let one_line_shape = shape
154147
.offset_left(last_line_width(ident) + 1)
155148
.and_then(|shape| shape.sub_width(1))
156149
.unwrap_or(Shape { width: 0, ..shape });
157-
let nested_shape = shape_from_indent_style(
158-
context,
159-
shape,
160-
used_width + 2 * paren_overhead,
161-
used_width + paren_overhead,
162-
);
150+
let nested_shape = shape_from_indent_style(context, shape, used_width + 2, used_width + 1);
163151
Context {
164152
context,
165153
items,
@@ -417,12 +405,13 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
417405
Some((lhs, rhs)) => (lhs, rhs),
418406
_ => (self.prefix, self.suffix),
419407
};
420-
let paren_overhead = paren_overhead(self.context);
421-
let fits_one_line = items_str.len() + paren_overhead <= shape.width;
408+
409+
// 2 = `()`
410+
let fits_one_line = items_str.len() + 2 <= shape.width;
422411
let extend_width = if items_str.is_empty() {
423-
paren_overhead
412+
2
424413
} else {
425-
first_line_width(items_str) + (paren_overhead / 2)
414+
first_line_width(items_str) + 1
426415
};
427416
let nested_indent_str = self
428417
.nested_shape
@@ -441,13 +430,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
441430
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
442431
|| (is_extendable && extend_width <= shape.width)
443432
{
444-
if self.context.config.spaces_within_parens_and_brackets() && !items_str.is_empty() {
445-
result.push(' ');
446-
result.push_str(items_str);
447-
result.push(' ');
448-
} else {
449-
result.push_str(items_str);
450-
}
433+
result.push_str(items_str);
451434
} else {
452435
if !items_str.is_empty() {
453436
result.push_str(&nested_indent_str);

src/patterns.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,7 @@ impl Rewrite for Pat {
156156
let pats = pats?;
157157

158158
// Unwrap all the sub-strings and join them with commas.
159-
let result = if context.config.spaces_within_parens_and_brackets() {
160-
format!("[ {} ]", pats.join(", "))
161-
} else {
162-
format!("[{}]", pats.join(", "))
163-
};
164-
Some(result)
159+
Some(format!("[{}]", pats.join(", ")))
165160
}
166161
PatKind::Struct(ref path, ref fields, ellipsis) => {
167162
rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape)

0 commit comments

Comments
 (0)