Skip to content

Commit a7a7feb

Browse files
committed
Format source codes and update tests
1 parent 6ab782d commit a7a7feb

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
@@ -269,10 +269,12 @@ fn is_extendable_parent(context: &RewriteContext, parent_str: &str) -> bool {
269269

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

src/comment.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,12 @@ fn rewrite_comment_inner(
326326
line
327327
})
328328
.map(|s| left_trim_comment_line(s, &style))
329-
.map(|line| if orig.starts_with("/*") && line_breaks == 0 {
330-
line.trim_left()
331-
} else {
332-
line
329+
.map(|line| {
330+
if orig.starts_with("/*") && line_breaks == 0 {
331+
line.trim_left()
332+
} else {
333+
line
334+
}
333335
});
334336

335337
let mut result = opener.to_owned();

src/expr.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,12 +1200,13 @@ impl<'a> ControlFlow<'a> {
12001200
// for event in event
12011201
let between_kwd_cond = mk_sp(
12021202
context.codemap.span_after(self.span, self.keyword.trim()),
1203-
self.pat
1204-
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
1203+
self.pat.map_or(cond_span.lo, |p| {
1204+
if self.matcher.is_empty() {
12051205
p.span.lo
12061206
} else {
12071207
context.codemap.span_before(self.span, self.matcher.trim())
1208-
}),
1208+
}
1209+
}),
12091210
);
12101211

12111212
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
@@ -2772,13 +2773,17 @@ where
27722773
if items.len() == 1 {
27732774
// 3 = "(" + ",)"
27742775
let nested_shape = try_opt!(shape.sub_width(3)).visual_indent(1);
2775-
return items.next().unwrap().rewrite(context, nested_shape).map(
2776-
|s| if context.config.spaces_within_parens() {
2777-
format!("( {}, )", s)
2778-
} else {
2779-
format!("({},)", s)
2780-
},
2781-
);
2776+
return items
2777+
.next()
2778+
.unwrap()
2779+
.rewrite(context, nested_shape)
2780+
.map(|s| {
2781+
if context.config.spaces_within_parens() {
2782+
format!("( {}, )", s)
2783+
} else {
2784+
format!("({},)", s)
2785+
}
2786+
});
27822787
}
27832788

27842789
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
@@ -706,10 +706,12 @@ impl Rewrite for ast::Ty {
706706
ast::TyKind::Paren(ref ty) => {
707707
let budget = try_opt!(shape.width.checked_sub(2));
708708
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
709-
.map(|ty_str| if context.config.spaces_within_parens() {
710-
format!("( {} )", ty_str)
711-
} else {
712-
format!("({})", ty_str)
709+
.map(|ty_str| {
710+
if context.config.spaces_within_parens() {
711+
format!("( {} )", ty_str)
712+
} else {
713+
format!("({})", ty_str)
714+
}
713715
})
714716
}
715717
ast::TyKind::Slice(ref ty) => {
@@ -719,10 +721,12 @@ impl Rewrite for ast::Ty {
719721
try_opt!(shape.width.checked_sub(2))
720722
};
721723
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
722-
.map(|ty_str| if context.config.spaces_within_square_brackets() {
723-
format!("[ {} ]", ty_str)
724-
} else {
725-
format!("[{}]", ty_str)
724+
.map(|ty_str| {
725+
if context.config.spaces_within_square_brackets() {
726+
format!("[ {} ]", ty_str)
727+
} else {
728+
format!("[{}]", ty_str)
729+
}
726730
})
727731
}
728732
ast::TyKind::Tup(ref items) => rewrite_tuple(

src/vertical.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ fn struct_field_preix_max_min_width<T: AlignedItem>(
198198
fields
199199
.iter()
200200
.map(|field| {
201-
field
202-
.rewrite_prefix(context, shape)
203-
.and_then(|field_str| if field_str.contains('\n') {
201+
field.rewrite_prefix(context, shape).and_then(|field_str| {
202+
if field_str.contains('\n') {
204203
None
205204
} else {
206205
Some(field_str.len())
207-
})
206+
}
207+
})
208208
})
209209
.fold(Some((0, ::std::usize::MAX)), |acc, len| match (acc, len) {
210210
(Some((max_len, min_len)), Some(len)) => {

src/visitor.rs

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

954954
impl Rewrite for ast::Attribute {
955955
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
956-
try_opt!(self.meta())
957-
.rewrite(context, shape)
958-
.map(|rw| if self.is_sugared_doc {
956+
try_opt!(self.meta()).rewrite(context, shape).map(|rw| {
957+
if self.is_sugared_doc {
959958
rw
960959
} else {
961960
let original = context.snippet(self.span);
@@ -968,7 +967,8 @@ impl Rewrite for ast::Attribute {
968967
} else {
969968
format!("{}[{}]", prefix, rw)
970969
}
971-
})
970+
}
971+
})
972972
}
973973
}
974974

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)