Skip to content

Commit 5436977

Browse files
committed
Merge pull request #1017 from marcusklaas/tweak-if-else
Format non-statement if-else expressions on a single line
2 parents 3a87aa5 + 98c0570 commit 5436977

34 files changed

+274
-297
lines changed

src/chains.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -354,27 +354,13 @@ fn rewrite_chain_subexpr(expr: &ast::Expr,
354354
}
355355
ast::ExprKind::Field(_, ref field) => {
356356
let s = format!(".{}", field.node);
357-
if s.len() <= width {
358-
Some(s)
359-
} else {
360-
None
361-
}
357+
if s.len() <= width { Some(s) } else { None }
362358
}
363359
ast::ExprKind::TupField(_, ref field) => {
364360
let s = format!(".{}", field.node);
365-
if s.len() <= width {
366-
Some(s)
367-
} else {
368-
None
369-
}
370-
}
371-
ast::ExprKind::Try(_) => {
372-
if width >= 1 {
373-
Some("?".into())
374-
} else {
375-
None
376-
}
361+
if s.len() <= width { Some(s) } else { None }
377362
}
363+
ast::ExprKind::Try(_) => if width >= 1 { Some("?".into()) } else { None },
378364
_ => unreachable!(),
379365
}
380366
}

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ create_config! {
381381
chain_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of chain";
382382
chains_overflow_last: bool, true, "Allow last call in method chain to break the line";
383383
reorder_imports: bool, false, "Reorder import statements alphabetically";
384-
single_line_if_else: bool, false, "Put else on same line as closing brace for if statements";
384+
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
385+
expressions. A value of zero means always break \
386+
if-else expressions.";
385387
format_strings: bool, true, "Format string literals where necessary";
386388
force_format_strings: bool, false, "Always format string literals";
387389
take_source_hints: bool, true, "Retain some formatting characteristics from the source code";

src/expr.rs

Lines changed: 209 additions & 197 deletions
Large diffs are not rendered by default.

src/imports.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ pub fn rewrite_use_list(width: usize,
111111
}
112112

113113
// 2 = ::
114-
let path_separation_w = if !path_str.is_empty() {
115-
2
116-
} else {
117-
0
118-
};
114+
let path_separation_w = if !path_str.is_empty() { 2 } else { 0 };
119115
// 1 = {
120116
let supp_indent = path_str.len() + path_separation_w + 1;
121117
// 1 = }
@@ -140,11 +136,7 @@ pub fn rewrite_use_list(width: usize,
140136
// potentially move "self" to the front of the vector without touching
141137
// the rest of the items.
142138
let has_self = move_self_to_front(&mut items);
143-
let first_index = if has_self {
144-
0
145-
} else {
146-
1
147-
};
139+
let first_index = if has_self { 0 } else { 1 };
148140

149141
if context.config.reorder_imports {
150142
items[1..].sort_by(|a, b| a.item.cmp(&b.item));

src/items.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ impl<'a> FmtVisitor<'a> {
139139
// FIXME(#21): we're dropping potential comments in between the
140140
// function keywords here.
141141
let vis = format_visibility(&item.vis);
142-
let mut_str = if is_mutable {
143-
"mut "
144-
} else {
145-
""
146-
};
142+
let mut_str = if is_mutable { "mut " } else { "" };
147143
let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident);
148144
let offset = self.block_indent + prefix.len();
149145
// 1 = ;
@@ -261,11 +257,7 @@ impl<'a> FmtVisitor<'a> {
261257
if self.config.fn_single_line && is_simple_block_stmt(block, codemap) {
262258
let rewrite = {
263259
if let Some(ref e) = block.expr {
264-
let suffix = if semicolon_for_expr(e) {
265-
";"
266-
} else {
267-
""
268-
};
260+
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
269261

270262
e.rewrite(&self.get_context(),
271263
self.config.max_width - self.block_indent.width(),
@@ -1274,11 +1266,7 @@ fn rewrite_fn_base(context: &RewriteContext,
12741266
.rewrite(&context, context.config.max_width - indent.width(), indent));
12751267

12761268
let multi_line_ret_str = ret_str.contains('\n');
1277-
let ret_str_len = if multi_line_ret_str {
1278-
0
1279-
} else {
1280-
ret_str.len()
1281-
};
1269+
let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
12821270

12831271
// Args.
12841272
let (mut one_line_budget, mut multi_line_budget, mut arg_indent) =

src/lists.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
208208
let first = i == 0;
209209
let last = iter.peek().is_none();
210210
let separate = !last || trailing_separator;
211-
let item_sep_len = if separate {
212-
sep_len
213-
} else {
214-
0
215-
};
211+
let item_sep_len = if separate { sep_len } else { 0 };
216212

217213
// Item string may be multi-line. Its length (used for block comment alignment)
218214
// Should be only the length of the last line.

src/missed_spans.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,7 @@ impl<'a> FmtVisitor<'a> {
9393

9494
fn replace_chars(string: &str) -> String {
9595
string.chars()
96-
.map(|ch| {
97-
if ch.is_whitespace() {
98-
ch
99-
} else {
100-
'X'
101-
}
102-
})
96+
.map(|ch| { if ch.is_whitespace() { ch } else { 'X' } })
10397
.collect()
10498
}
10599

src/patterns.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,7 @@ impl Rewrite for Pat {
126126
PatKind::Struct(ref path, ref fields, elipses) => {
127127
let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
128128

129-
let (elipses_str, terminator) = if elipses {
130-
(", ..", "..")
131-
} else {
132-
("", "}")
133-
};
129+
let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") };
134130

135131
// 5 = `{` plus space before and after plus `}` plus space before.
136132
let budget = try_opt!(width.checked_sub(path.len() + 5 + elipses_str.len()));

src/types.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ fn rewrite_segment(expr_context: bool,
187187

188188
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
189189
let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
190-
let separator = if expr_context {
191-
"::"
192-
} else {
193-
""
194-
};
190+
let separator = if expr_context { "::" } else { "" };
195191

196192
// 1 for <
197193
let extra_offset = 1 + separator.len();

src/utils.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
7777
Visibility::Crate(_) => Cow::from("pub(crate) "),
7878
Visibility::Restricted { ref path, .. } => {
7979
let Path { global, ref segments, .. } = **path;
80-
let prefix = if global {
81-
"::"
82-
} else {
83-
""
84-
};
80+
let prefix = if global { "::" } else { "" };
8581
let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str());
8682

8783
Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::")))

src/visitor.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ impl<'a> FmtVisitor<'a> {
7777
// Check if this block has braces.
7878
let snippet = self.snippet(b.span);
7979
let has_braces = snippet.starts_with("{") || snippet.starts_with("unsafe");
80-
let brace_compensation = if has_braces {
81-
BytePos(1)
82-
} else {
83-
BytePos(0)
84-
};
80+
let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
8581

8682
self.last_pos = self.last_pos + brace_compensation;
8783
self.block_indent = self.block_indent.block_indent(self.config);

tests/config/small_tabs.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum_trailing_comma = true
2121
report_todo = "Always"
2222
report_fixme = "Never"
2323
reorder_imports = false
24-
single_line_if_else = false
24+
single_line_if_else_max_width = 0
2525
format_strings = true
2626
chains_overflow_last = true
2727
take_source_hints = true

tests/source/chains-block-indented-base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-chain_base_indent: Inherit
23
// Test chain formatting with block indented base
34

tests/source/chains-no-overflow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-chains_overflow_last: false
23
// Test chain formatting without overflowing the last item.
34

tests/source/chains-visual.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-chain_indent: Visual
23
// rustfmt-chain_base_indent: Visual
34
// Test chain formatting.

tests/source/chains.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// Test chain formatting.
23

34
fn main() {

tests/source/closure.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ fn main() {
1111
};
1212

1313
let loooooooooooooong_name = |field| {
14-
// TODO(#27): format comments.
14+
// format comments.
1515
if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo
1616
} else {
1717
field.span.lo
1818
}};
1919

20-
let block_me = |field| if true_story() { 1 } else { 2 };
21-
2220
let unblock_me = |trivial| {
2321
closure()
2422
};
2523

2624
let empty = |arg| {};
2725

28-
let simple = |arg| { /* TODO(#27): comment formatting */ foo(arg) };
26+
let simple = |arg| { /* comment formatting */ foo(arg) };
2927

3028
let test = | | { do_something(); do_something_else(); };
3129

tests/source/else-if-brace-style-always-next-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-else_if_brace_style: AlwaysNextLine
23

34
fn main() {

tests/source/else-if-brace-style-always-same-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-else_if_brace_style: AlwaysSameLine
23

34
fn main() {

tests/source/else-if-brace-style-closing-next-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-else_if_brace_style: ClosingNextLine
23

34
fn main() {

tests/source/expr.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,20 @@ fn ranges() {
255255
// the expr below won't compile for some reason...
256256
// let a = 0 ... ;
257257
}
258+
259+
fn if_else() {
260+
let exact = diff /
261+
(if size == 0 {
262+
1
263+
} else {
264+
size
265+
});
266+
267+
let cx = tp1.x +
268+
any * radius *
269+
if anticlockwise {
270+
1.0
271+
} else {
272+
-1.0
273+
};
274+
}

tests/source/hard-tabs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-wrap_comments: true
23
// rustfmt-hard_tabs: true
34

tests/source/single-line-if-else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-single_line_if_else: true
1+
// rustfmt-single_line_if_else_max_width: 100
22

33
// Format if-else expressions on a single line, when possible.
44

tests/target/chains-block-indented-base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-chain_base_indent: Inherit
23
// Test chain formatting with block indented base
34

tests/target/chains-no-overflow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-chains_overflow_last: false
23
// Test chain formatting without overflowing the last item.
34

tests/target/chains-visual.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-chain_indent: Visual
23
// rustfmt-chain_base_indent: Visual
34
// Test chain formatting.

tests/target/chains.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// Test chain formatting.
23

34
fn main() {

tests/target/closure.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,20 @@ fn main() {
1818
};
1919

2020
let loooooooooooooong_name = |field| {
21-
// TODO(#27): format comments.
21+
// format comments.
2222
if field.node.attrs.len() > 0 {
2323
field.node.attrs[0].span.lo
2424
} else {
2525
field.span.lo
2626
}
2727
};
2828

29-
let block_me = |field| if true_story() {
30-
1
31-
} else {
32-
2
33-
};
34-
3529
let unblock_me = |trivial| closure();
3630

3731
let empty = |arg| {};
3832

3933
let simple = |arg| {
40-
// TODO(#27): comment formatting
34+
// comment formatting
4135
foo(arg)
4236
};
4337

tests/target/else-if-brace-style-always-next-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-else_if_brace_style: AlwaysNextLine
23

34
fn main() {

tests/target/else-if-brace-style-always-same-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-else_if_brace_style: AlwaysSameLine
23

34
fn main() {

tests/target/else-if-brace-style-closing-next-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-else_if_brace_style: ClosingNextLine
23

34
fn main() {

tests/target/expr.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ fn foo() -> bool {
6262
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3 {
6363
}
6464

65-
let test = if true {
66-
5
67-
} else {
68-
3
69-
};
65+
let test = if true { 5 } else { 3 };
7066

7167
if cond() {
7268
something();
@@ -92,11 +88,7 @@ fn bar() {
9288
syntactically_correct(loop {
9389
sup('?');
9490
},
95-
if cond {
96-
0
97-
} else {
98-
1
99-
});
91+
if cond { 0 } else { 1 });
10092

10193
let third = ..10;
10294
let infi_range = ..;
@@ -277,3 +269,9 @@ fn ranges() {
277269
// the expr below won't compile for some reason...
278270
// let a = 0 ... ;
279271
}
272+
273+
fn if_else() {
274+
let exact = diff / (if size == 0 { 1 } else { size });
275+
276+
let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 };
277+
}

tests/target/hard-tabs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-single_line_if_else_max_width: 0
12
// rustfmt-wrap_comments: true
23
// rustfmt-hard_tabs: true
34

tests/target/single-line-if-else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-single_line_if_else: true
1+
// rustfmt-single_line_if_else_max_width: 100
22

33
// Format if-else expressions on a single line, when possible.
44

0 commit comments

Comments
 (0)