Merge pull request #1889 from topecongiro/match-arm

Do not put if on the same line as match arm
This commit is contained in:
Nick Cameron 2017-12-01 11:56:01 +13:00 committed by GitHub
commit c18ba569df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 131 additions and 76 deletions

View File

@ -94,11 +94,13 @@ fn execute() -> i32 {
print_usage_to_stderr(&opts, &e.to_string());
failure
}
Ok(status) => if status.success() {
success
} else {
status.code().unwrap_or(failure)
},
Ok(status) => {
if status.success() {
success
} else {
status.code().unwrap_or(failure)
}
}
}
}

View File

@ -568,9 +568,11 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
// Return the error if it's something other than `NotFound`; otherwise we didn't
// find the project file yet, and continue searching.
Err(e) => if e.kind() != ErrorKind::NotFound {
return Err(e);
},
Err(e) => {
if e.kind() != ErrorKind::NotFound {
return Err(e);
}
}
_ => {}
}
}

View File

@ -462,18 +462,20 @@ where
None => DefinitiveListTactic::Vertical,
}
}
IndentStyle::Visual => if has_long_item || items.iter().any(ListItem::is_multiline) {
definitive_tactic(
&items,
ListTactic::LimitedHorizontalVertical(
context.config.width_heuristics().array_width,
),
Separator::Comma,
nested_shape.width,
)
} else {
DefinitiveListTactic::Mixed
},
IndentStyle::Visual => {
if has_long_item || items.iter().any(ListItem::is_multiline) {
definitive_tactic(
&items,
ListTactic::LimitedHorizontalVertical(
context.config.width_heuristics().array_width,
),
Separator::Comma,
nested_shape.width,
)
} else {
DefinitiveListTactic::Mixed
}
}
};
let ends_with_newline = tactic.ends_with_newline(context.config.indent_style());
@ -1500,8 +1502,8 @@ fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bo
{
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
(
!context.config.force_multiline_blocks() && expr.can_be_overflowed(context, 1),
&**expr,
!context.config.force_multiline_blocks() && can_extend_match_arm_body(expr),
&*expr,
)
} else {
(false, &*body)
@ -1723,6 +1725,33 @@ fn rewrite_pat_expr(
.map(|expr_rw| format!("\n{}{}", nested_indent_str, expr_rw))
}
fn can_extend_match_arm_body(body: &ast::Expr) -> bool {
match body.node {
// We do not allow `if` to stay on the same line, since we could easily mistake
// `pat => if cond { ... }` and `pat if cond => { ... }`.
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => false,
ast::ExprKind::ForLoop(..)
| ast::ExprKind::Loop(..)
| ast::ExprKind::While(..)
| ast::ExprKind::WhileLet(..)
| ast::ExprKind::Match(..)
| ast::ExprKind::Block(..)
| ast::ExprKind::Closure(..)
| ast::ExprKind::Array(..)
| ast::ExprKind::Call(..)
| ast::ExprKind::MethodCall(..)
| ast::ExprKind::Mac(..)
| ast::ExprKind::Struct(..)
| ast::ExprKind::Tup(..) => true,
ast::ExprKind::AddrOf(_, ref expr)
| ast::ExprKind::Box(ref expr)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Cast(ref expr, _) => can_extend_match_arm_body(expr),
_ => false,
}
}
pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option<String> {
match l.node {
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),

View File

@ -190,19 +190,25 @@ impl BadIssueSeeker {
}
match part {
NumberPart::OpenParen => if c != '(' {
return IssueClassification::Bad(issue);
} else {
part = NumberPart::Pound;
},
NumberPart::Pound => if c == '#' {
part = NumberPart::Number;
},
NumberPart::Number => if c >= '0' && c <= '9' {
part = NumberPart::CloseParen;
} else {
return IssueClassification::Bad(issue);
},
NumberPart::OpenParen => {
if c != '(' {
return IssueClassification::Bad(issue);
} else {
part = NumberPart::Pound;
}
}
NumberPart::Pound => {
if c == '#' {
part = NumberPart::Number;
}
}
NumberPart::Number => {
if c >= '0' && c <= '9' {
part = NumberPart::CloseParen;
} else {
return IssueClassification::Bad(issue);
}
}
NumberPart::CloseParen => {}
}

View File

@ -550,12 +550,14 @@ impl<'a> FmtVisitor<'a> {
self.block_indent,
Some(one_line_width),
)?,
ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr {
let lhs = format!("{} =", field.node.name);
rewrite_assign_rhs(&context, lhs, &**expr, shape)?
} else {
field.node.name.to_string()
},
ast::VariantData::Unit(..) => {
if let Some(ref expr) = field.node.disr_expr {
let lhs = format!("{} =", field.node.name);
rewrite_assign_rhs(&context, lhs, &**expr, shape)?
} else {
field.node.name.to_string()
}
}
};
let attrs_extendable = attrs_str.is_empty()
@ -643,11 +645,13 @@ pub fn format_impl(
_ if last_line_contains_single_line_comment(&result) => result.push_str(&sep),
BraceStyle::AlwaysNextLine => result.push_str(&sep),
BraceStyle::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => if !where_clause_str.is_empty() {
result.push_str(&sep);
} else {
result.push(' ');
},
BraceStyle::SameLineWhere => {
if !where_clause_str.is_empty() {
result.push_str(&sep);
} else {
result.push(' ');
}
}
}
result.push('{');
@ -1039,14 +1043,16 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
result.push_str(&offset.to_string(context.config));
}
BraceStyle::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => if !where_clause_str.is_empty()
&& (!trait_items.is_empty() || result.contains('\n'))
{
result.push('\n');
result.push_str(&offset.to_string(context.config));
} else {
result.push(' ');
},
BraceStyle::SameLineWhere => {
if !where_clause_str.is_empty()
&& (!trait_items.is_empty() || result.contains('\n'))
{
result.push('\n');
result.push_str(&offset.to_string(context.config));
} else {
result.push(' ');
}
}
}
result.push('{');

View File

@ -126,11 +126,13 @@ pub fn rewrite_macro(
let macro_name = match extra_ident {
None => format!("{}!", mac.node.path),
Some(ident) => if ident == symbol::keywords::Invalid.ident() {
format!("{}!", mac.node.path)
} else {
format!("{}! {}", mac.node.path, ident)
},
Some(ident) => {
if ident == symbol::keywords::Invalid.ident() {
format!("{}!", mac.node.path)
} else {
format!("{}! {}", mac.node.path, ident)
}
}
};
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {

View File

@ -51,11 +51,13 @@ impl Rewrite for Pat {
Some(format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat))
}
PatKind::Wild => if 1 <= shape.width {
Some("_".to_owned())
} else {
None
},
PatKind::Wild => {
if 1 <= shape.width {
Some("_".to_owned())
} else {
None
}
}
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
let infix = match *end_kind {
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",

View File

@ -720,11 +720,13 @@ impl Rewrite for ast::Ty {
SeparatorPlace::Back,
)
}
ast::TyKind::Infer => if shape.width >= 1 {
Some("_".to_owned())
} else {
None
},
ast::TyKind::Infer => {
if shape.width >= 1 {
Some("_".to_owned())
} else {
None
}
}
ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
ast::TyKind::Never => Some(String::from("!")),
ast::TyKind::Mac(..) => None,

View File

@ -321,10 +321,12 @@ impl<'a> FmtVisitor<'a> {
attrs = &filtered_attrs;
}
}
_ => if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
self.push_rewrite(item.span, None);
return;
},
_ => {
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
self.push_rewrite(item.span, None);
return;
}
}
}
match item.node {

View File

@ -3,9 +3,11 @@
fn main() {
match lorem {
Lorem::Ipsum => if ipsum {
println!("dolor");
},
Lorem::Ipsum => {
if ipsum {
println!("dolor");
}
}
Lorem::Dolor => println!("amet"),
}
}