Skip to content

Commit d42d437

Browse files
committed
Format source codes and update tests
1 parent 24e3725 commit d42d437

File tree

11 files changed

+83
-58
lines changed

11 files changed

+83
-58
lines changed

src/bin/cargo-fmt.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ fn format_crate(
123123
let files: Vec<_> = targets
124124
.into_iter()
125125
.filter(|t| t.kind.should_format())
126-
.inspect(|t| if verbosity == Verbosity::Verbose {
127-
println!("[{:?}] {:?}", t.kind, t.path)
126+
.inspect(|t| {
127+
if verbosity == Verbosity::Verbose {
128+
println!("[{:?}] {:?}", t.kind, t.path)
129+
}
128130
})
129131
.map(|t| t.path)
130132
.collect();
@@ -240,12 +242,14 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
240242
.as_array()
241243
.unwrap()
242244
.into_iter()
243-
.filter(|member| if workspace_hitlist == WorkspaceHitlist::All {
244-
true
245-
} else {
246-
let member_obj = member.as_object().unwrap();
247-
let member_name = member_obj.get("name").unwrap().as_str().unwrap();
248-
hitlist.take(&member_name.to_string()).is_some()
245+
.filter(|member| {
246+
if workspace_hitlist == WorkspaceHitlist::All {
247+
true
248+
} else {
249+
let member_obj = member.as_object().unwrap();
250+
let member_name = member_obj.get("name").unwrap().as_str().unwrap();
251+
hitlist.take(&member_name.to_string()).is_some()
252+
}
249253
})
250254
.collect();
251255
if hitlist.len() != 0 {

src/chains.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ fn is_extendable_parent(context: &RewriteContext, parent_str: &str) -> bool {
268268

269269
// True if the chain is only `?`s.
270270
fn chain_only_try(exprs: &[ast::Expr]) -> bool {
271-
exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node {
272-
true
273-
} else {
274-
false
271+
exprs.iter().all(|e| {
272+
if let ast::ExprKind::Try(_) = e.node {
273+
true
274+
} else {
275+
false
276+
}
275277
})
276278
}
277279

src/comment.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,12 @@ fn rewrite_comment_inner(
239239
line
240240
})
241241
.map(|s| left_trim_comment_line(s, &style))
242-
.map(|line| if orig.starts_with("/*") && line_breaks == 0 {
243-
line.trim_left()
244-
} else {
245-
line
242+
.map(|line| {
243+
if orig.starts_with("/*") && line_breaks == 0 {
244+
line.trim_left()
245+
} else {
246+
line
247+
}
246248
});
247249

248250
let mut result = opener.to_owned();

src/expr.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,12 +1248,13 @@ impl<'a> ControlFlow<'a> {
12481248
// for event in event
12491249
let between_kwd_cond = mk_sp(
12501250
context.codemap.span_after(self.span, self.keyword.trim()),
1251-
self.pat
1252-
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
1251+
self.pat.map_or(cond_span.lo, |p| {
1252+
if self.matcher.is_empty() {
12531253
p.span.lo
12541254
} else {
12551255
context.codemap.span_before(self.span, self.matcher.trim())
1256-
}),
1256+
}
1257+
}),
12571258
);
12581259

12591260
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
@@ -2820,13 +2821,17 @@ where
28202821
if items.len() == 1 {
28212822
// 3 = "(" + ",)"
28222823
let nested_shape = try_opt!(shape.sub_width(3)).visual_indent(1);
2823-
return items.next().unwrap().rewrite(context, nested_shape).map(
2824-
|s| if context.config.spaces_within_parens() {
2825-
format!("( {}, )", s)
2826-
} else {
2827-
format!("({},)", s)
2828-
},
2829-
);
2824+
return items
2825+
.next()
2826+
.unwrap()
2827+
.rewrite(context, nested_shape)
2828+
.map(|s| {
2829+
if context.config.spaces_within_parens() {
2830+
format!("( {}, )", s)
2831+
} else {
2832+
format!("({},)", s)
2833+
}
2834+
});
28302835
}
28312836

28322837
let list_lo = context.codemap.span_after(span, "(");

src/types.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,12 @@ impl Rewrite for ast::Ty {
708708
ast::TyKind::Paren(ref ty) => {
709709
let budget = try_opt!(shape.width.checked_sub(2));
710710
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
711-
.map(|ty_str| if context.config.spaces_within_parens() {
712-
format!("( {} )", ty_str)
713-
} else {
714-
format!("({})", ty_str)
711+
.map(|ty_str| {
712+
if context.config.spaces_within_parens() {
713+
format!("( {} )", ty_str)
714+
} else {
715+
format!("({})", ty_str)
716+
}
715717
})
716718
}
717719
ast::TyKind::Slice(ref ty) => {
@@ -721,10 +723,12 @@ impl Rewrite for ast::Ty {
721723
try_opt!(shape.width.checked_sub(2))
722724
};
723725
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
724-
.map(|ty_str| if context.config.spaces_within_square_brackets() {
725-
format!("[ {} ]", ty_str)
726-
} else {
727-
format!("[{}]", ty_str)
726+
.map(|ty_str| {
727+
if context.config.spaces_within_square_brackets() {
728+
format!("[ {} ]", ty_str)
729+
} else {
730+
format!("[{}]", ty_str)
731+
}
728732
})
729733
}
730734
ast::TyKind::Tup(ref items) => rewrite_tuple(

src/vertical.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ fn struct_field_preix_max_min_width<T: AlignedItem>(
174174
fields
175175
.iter()
176176
.map(|field| {
177-
field
178-
.rewrite_prefix(context, shape)
179-
.and_then(|field_str| if field_str.contains('\n') {
177+
field.rewrite_prefix(context, shape).and_then(|field_str| {
178+
if field_str.contains('\n') {
180179
None
181180
} else {
182181
Some(field_str.len())
183-
})
182+
}
183+
})
184184
})
185185
.fold(Some((0, ::std::usize::MAX)), |acc, len| match (acc, len) {
186186
(Some((max_len, min_len)), Some(len)) => {

src/visitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,8 @@ impl Rewrite for ast::MetaItem {
888888

889889
impl Rewrite for ast::Attribute {
890890
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
891-
try_opt!(self.meta())
892-
.rewrite(context, shape)
893-
.map(|rw| if self.is_sugared_doc {
891+
try_opt!(self.meta()).rewrite(context, shape).map(|rw| {
892+
if self.is_sugared_doc {
894893
rw
895894
} else {
896895
let original = context.snippet(self.span);
@@ -903,7 +902,8 @@ impl Rewrite for ast::Attribute {
903902
} else {
904903
format!("{}[{}]", prefix, rw)
905904
}
906-
})
905+
}
906+
})
907907
}
908908
}
909909

tests/target/chains-visual.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ fn main() {
2121
false => (),
2222
});
2323

24-
loong_func().quux(move || if true {
25-
1
26-
} else {
27-
2
24+
loong_func().quux(move || {
25+
if true {
26+
1
27+
} else {
28+
2
29+
}
2830
});
2931

3032
some_fuuuuuuuuunction().method_call_a(aaaaa, bbbbb, |c| {

tests/target/chains.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ fn main() {
2323
false => (),
2424
});
2525

26-
loong_func().quux(move || if true {
27-
1
28-
} else {
29-
2
26+
loong_func().quux(move || {
27+
if true {
28+
1
29+
} else {
30+
2
31+
}
3032
});
3133

3234
some_fuuuuuuuuunction().method_call_a(aaaaa, bbbbb, |c| {

tests/target/closure-block-inside-macro.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// rustfmt-fn_call_style: Block
22

33
// #1547
4-
fuzz_target!(|data: &[u8]| if let Some(first) = data.first() {
5-
let index = *first as usize;
6-
if index >= ENCODINGS.len() {
7-
return;
4+
fuzz_target!(|data: &[u8]| {
5+
if let Some(first) = data.first() {
6+
let index = *first as usize;
7+
if index >= ENCODINGS.len() {
8+
return;
9+
}
810
}
911
let encoding = ENCODINGS[index];
1012
dispatch_test(encoding, &data[1..]);

tests/target/hard-tabs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ fn main() {
7373
arg(a, b, c, d, e)
7474
}
7575

76-
loong_func().quux(move || if true {
77-
1
78-
} else {
79-
2
76+
loong_func().quux(move || {
77+
if true {
78+
1
79+
} else {
80+
2
81+
}
8082
});
8183

8284
fffffffffffffffffffffffffffffffffff(a, {

0 commit comments

Comments
 (0)