From f9239dd630bb10fcff07cc834f18d4330dcf75c8 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 7 Aug 2017 15:17:57 +0900 Subject: [PATCH] Format source codes and update tests --- src/expr.rs | 22 ++++----- src/missed_spans.rs | 9 ++-- src/types.rs | 11 ++--- src/vertical.rs | 8 +-- src/visitor.rs | 8 +-- tests/target/chains-indent-visual.rs | 11 ++--- tests/target/chains-visual.rs | 73 ++++++++++++---------------- 7 files changed, 62 insertions(+), 80 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index f56f3f8779ed..e800ed0565fb 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1130,10 +1130,8 @@ impl<'a> ControlFlow<'a> { let new_width = try_opt!(width.checked_sub(pat_expr_str.len() + fixed_cost)); let expr = &self.block.stmts[0]; - let if_str = try_opt!(expr.rewrite( - context, - Shape::legacy(new_width, Indent::empty()), - )); + let if_str = + try_opt!(expr.rewrite(context, Shape::legacy(new_width, Indent::empty()),)); let new_width = try_opt!(new_width.checked_sub(if_str.len())); let else_expr = &else_node.stmts[0]; @@ -1246,14 +1244,12 @@ impl<'a> ControlFlow<'a> { // for event in event let between_kwd_cond = mk_sp( context.codemap.span_after(self.span, self.keyword.trim()), - self.pat.map_or( - cond_span.lo, - |p| if self.matcher.is_empty() { + self.pat + .map_or(cond_span.lo, |p| if self.matcher.is_empty() { p.span.lo } else { context.codemap.span_before(self.span, self.matcher.trim()) - }, - ), + }), ); let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape); @@ -2253,15 +2249,17 @@ where _ => (), } } - last_arg_shape(&context, &item_vec, shape, args_max_width) - .map_or((None, None), |arg_shape| { + last_arg_shape(&context, &item_vec, shape, args_max_width).map_or( + (None, None), + |arg_shape| { rewrite_last_arg_with_overflow( &context, args, &mut item_vec[args.len() - 1], arg_shape, ) - }) + }, + ) } else { (None, None) }; diff --git a/src/missed_spans.rs b/src/missed_spans.rs index ac3ad906d17e..b85bc44b1622 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -146,10 +146,11 @@ impl<'a> FmtVisitor<'a> { let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count(); if rewrite_next_comment && - !self.config - .file_lines() - .intersects_range(file_name, cur_line, cur_line + subslice_num_lines) - { + !self.config.file_lines().intersects_range( + file_name, + cur_line, + cur_line + subslice_num_lines, + ) { rewrite_next_comment = false; } diff --git a/src/types.rs b/src/types.rs index 8e0cf2436dee..2927aa7425b8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -553,10 +553,7 @@ impl Rewrite for ast::TyParamBound { let budget = try_opt!(shape.width.checked_sub(1)); Some(format!( "?{}", - try_opt!(tref.rewrite( - context, - Shape::legacy(budget, shape.indent + 1), - )) + try_opt!(tref.rewrite(context, Shape::legacy(budget, shape.indent + 1))) )) } ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, shape), @@ -609,10 +606,8 @@ impl Rewrite for ast::TyParam { }; result.push_str(eq_str); let budget = try_opt!(shape.width.checked_sub(result.len())); - let rewrite = try_opt!(def.rewrite( - context, - Shape::legacy(budget, shape.indent + result.len()), - )); + let rewrite = + try_opt!(def.rewrite(context, Shape::legacy(budget, shape.indent + result.len()))); result.push_str(&rewrite); } diff --git a/src/vertical.rs b/src/vertical.rs index 7f43fa8681a4..521341702c5c 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -174,13 +174,13 @@ fn struct_field_preix_max_min_width( fields .iter() .map(|field| { - field.rewrite_prefix(context, shape).and_then( - |field_str| if field_str.contains('\n') { + field + .rewrite_prefix(context, shape) + .and_then(|field_str| if field_str.contains('\n') { None } else { Some(field_str.len()) - }, - ) + }) }) .fold(Some((0, ::std::usize::MAX)), |acc, len| match (acc, len) { (Some((max_len, min_len)), Some(len)) => { diff --git a/src/visitor.rs b/src/visitor.rs index c9fa70655114..3e23f1a399d7 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -855,8 +855,9 @@ impl Rewrite for ast::MetaItem { impl Rewrite for ast::Attribute { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - try_opt!(self.meta()).rewrite(context, shape).map( - |rw| if self.is_sugared_doc { + try_opt!(self.meta()) + .rewrite(context, shape) + .map(|rw| if self.is_sugared_doc { rw } else { let original = context.snippet(self.span); @@ -869,8 +870,7 @@ impl Rewrite for ast::Attribute { } else { format!("{}[{}]", prefix, rw) } - }, - ) + }) } } diff --git a/tests/target/chains-indent-visual.rs b/tests/target/chains-indent-visual.rs index e3ab320c6e19..66ef44fcd4ae 100644 --- a/tests/target/chains-indent-visual.rs +++ b/tests/target/chains-indent-visual.rs @@ -1,10 +1,9 @@ // rustfmt-chain_indent: Visual fn test() { - let x = my_long_function() - .my_even_longer_function() - .my_nested_function() - .some_random_name() - .another_function() - .do_it(); + let x = my_long_function().my_even_longer_function() + .my_nested_function() + .some_random_name() + .another_function() + .do_it(); } diff --git a/tests/target/chains-visual.rs b/tests/target/chains-visual.rs index cfd7192b8b4a..829a27867dd5 100644 --- a/tests/target/chains-visual.rs +++ b/tests/target/chains-visual.rs @@ -32,15 +32,14 @@ fn main() { x }); - some_fuuuuuuuuunction() - .method_call_a(aaaaa, bbbbb, |c| { - let x = c; - x - }) - .method_call_b(aaaaa, bbbbb, |c| { - let x = c; - x - }); + some_fuuuuuuuuunction().method_call_a(aaaaa, bbbbb, |c| { + let x = c; + x + }) + .method_call_b(aaaaa, bbbbb, |c| { + let x = c; + x + }); fffffffffffffffffffffffffffffffffff(a, { SCRIPT_TASK_ROOT.with(|root| { *root.borrow_mut() = Some(&script_task); }); @@ -67,16 +66,14 @@ fn floaters() { let x = Foo { field1: val1, field2: val2, - } - .method_call() + }.method_call() .method_call(); let y = if cond { val1 } else { val2 - } - .method_call(); + }.method_call(); { match x { @@ -86,8 +83,7 @@ fn floaters() { mparams[match cur.to_digit(10) { Some(d) => d as usize - 1, None => return Err("bad param number".to_owned()), - }] - .clone(), + }].clone(), ); } } @@ -97,22 +93,19 @@ fn floaters() { some(); } else { none(); - } - .bar() + }.bar() .baz(); - Foo { x: val } - .baz(|| { - force(); - multiline(); - }) - .quux(); + Foo { x: val }.baz(|| { + force(); + multiline(); + }) + .quux(); Foo { y: i_am_multi_line, z: ok, - } - .baz(|| { + }.baz(|| { force(); multiline(); }) @@ -121,8 +114,7 @@ fn floaters() { a + match x { true => "yay!", false => "boo!", - } - .bar() + }.bar() } fn is_replaced_content() -> bool { @@ -137,33 +129,30 @@ fn issue587() { } fn issue_1389() { - let names = String::from_utf8(names)? - .split('|') - .map(str::to_owned) - .collect(); + let names = String::from_utf8(names)?.split('|') + .map(str::to_owned) + .collect(); } fn issue1217() -> Result { - let random_chars: String = OsRng::new()? - .gen_ascii_chars() - .take(self.bit_length) - .collect(); + let random_chars: String = OsRng::new()?.gen_ascii_chars() + .take(self.bit_length) + .collect(); Ok(Mnemonic::new(&random_chars)) } fn issue1236(options: Vec) -> Result> { - let process = Command::new("dmenu") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .chain_err(|| "failed to spawn dmenu")?; + let process = Command::new("dmenu").stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .chain_err(|| "failed to spawn dmenu")?; } fn issue1434() { for _ in 0..100 { - let prototype_id = PrototypeIdData::from_reader::<_, B>(&mut self.file_cursor) - .chain_err(|| { + let prototype_id = + PrototypeIdData::from_reader::<_, B>(&mut self.file_cursor).chain_err(|| { format!( "could not read prototype ID at offset {:#010x}", current_offset