diff --git a/src/chains.rs b/src/chains.rs index aa6034aa687..3f306abfdca 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -9,7 +9,7 @@ // except according to those terms. /// Formatting of chained expressions, i.e. expressions which are chained by -/// dots: struct and enum field access and method calls. +/// dots: struct and enum field access, method calls, and try shorthand (?). /// /// Instead of walking these subexpressions one-by-one, as is our usual strategy /// for expression formatting, we collect maximal sequences of these expressions @@ -81,24 +81,23 @@ /// true, then we allow the last method call to spill over multiple lines without /// forcing the rest of the chain to be split. - use Indent; use rewrite::{Rewrite, RewriteContext}; use utils::{wrap_str, first_line_width}; use expr::rewrite_call; use config::BlockIndentStyle; +use macros::convert_try_mac; use syntax::{ast, ptr}; use syntax::codemap::{mk_sp, Span}; - pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, width: usize, offset: Indent) -> Option { let total_span = expr.span; - let (parent, subexpr_list) = make_subexpr_list(expr); + let (parent, subexpr_list) = make_subexpr_list(expr, context); // Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`. let parent_block_indent = chain_base_indent(context, offset); @@ -107,11 +106,16 @@ pub fn rewrite_chain(expr: &ast::Expr, // Decide how to layout the rest of the chain. `extend` is true if we can // put the first non-parent item on the same line as the parent. - let (indent, extend) = if !parent_rewrite.contains('\n') && is_continuable(parent) || + let (indent, extend) = if !parent_rewrite.contains('\n') && is_continuable(&parent) || parent_rewrite.len() <= context.config.tab_spaces { - // Try and put at least the first two items on the same line. - (chain_indent(context, offset + Indent::new(0, parent_rewrite.len())), true) - } else if is_block_expr(parent, &parent_rewrite) { + + let indent = if let ast::ExprKind::Try(..) = subexpr_list.last().unwrap().node { + parent_block_indent.block_indent(context.config) + } else { + chain_indent(context, offset + Indent::new(0, parent_rewrite.len())) + }; + (indent, true) + } else if is_block_expr(&parent, &parent_rewrite) { // The parent is a block, so align the rest of the chain with the closing // brace. (parent_block_indent, false) @@ -184,12 +188,29 @@ pub fn rewrite_chain(expr: &ast::Expr, wrap_str(format!("{}{}{}", parent_rewrite, first_connector, - rewrites.join(&connector)), + join_rewrites(&rewrites, &subexpr_list, &connector)), context.config.max_width, width, offset) } +fn join_rewrites(rewrites: &[String], subexps: &[ast::Expr], connector: &str) -> String { + let mut rewrite_iter = rewrites.iter(); + let mut result = rewrite_iter.next().unwrap().clone(); + let mut subexpr_iter = subexps.iter().rev(); + subexpr_iter.next(); + + for (rewrite, expr) in rewrite_iter.zip(subexpr_iter) { + match expr.node { + ast::ExprKind::Try(_) => (), + _ => result.push_str(connector), + }; + result.push_str(&rewrite[..]); + } + + result +} + // States whether an expression's last line exclusively consists of closing // parens, braces, and brackets in its idiomatic formatting. fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool { @@ -213,21 +234,11 @@ fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool { // Returns the root of the chain and a Vec of the prefixes of the rest of the chain. // E.g., for input `a.b.c` we return (`a`, [`a.b.c`, `a.b`]) -fn make_subexpr_list(mut expr: &ast::Expr) -> (&ast::Expr, Vec<&ast::Expr>) { - fn pop_expr_chain(expr: &ast::Expr) -> Option<&ast::Expr> { - match expr.node { - ast::ExprKind::MethodCall(_, _, ref expressions) => Some(&expressions[0]), - ast::ExprKind::TupField(ref subexpr, _) | - ast::ExprKind::Field(ref subexpr, _) => Some(subexpr), - _ => None, - } - } - - let mut subexpr_list = vec![expr]; +fn make_subexpr_list(expr: &ast::Expr, context: &RewriteContext) -> (ast::Expr, Vec) { + let mut subexpr_list = vec![expr.clone()]; - while let Some(subexpr) = pop_expr_chain(expr) { - subexpr_list.push(subexpr); - expr = subexpr; + while let Some(subexpr) = pop_expr_chain(subexpr_list.last().unwrap(), context) { + subexpr_list.push(subexpr.clone()); } let parent = subexpr_list.pop().unwrap(); @@ -293,6 +304,33 @@ fn rewrite_method_call_with_overflow(expr_kind: &ast::ExprKind, } } +// Returns the expression's subexpression, if it exists. When the subexpr +// is a try! macro, we'll convert it to shorthand when the option is set. +fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext) -> Option { + match expr.node { + ast::ExprKind::MethodCall(_, _, ref expressions) => { + Some(convert_try(&expressions[0], context)) + } + ast::ExprKind::TupField(ref subexpr, _) | + ast::ExprKind::Field(ref subexpr, _) | + ast::ExprKind::Try(ref subexpr) => Some(convert_try(subexpr, context)), + _ => None, + } +} + +fn convert_try(expr: &ast::Expr, context: &RewriteContext) -> ast::Expr { + match expr.node { + ast::ExprKind::Mac(ref mac) if context.config.use_try_shorthand => { + if let Some(subexpr) = convert_try_mac(mac, context) { + subexpr + } else { + expr.clone() + } + } + _ => expr.clone(), + } +} + // Rewrite the last element in the chain `expr`. E.g., given `a.b.c` we rewrite // `.c`. fn rewrite_chain_subexpr(expr: &ast::Expr, @@ -328,6 +366,13 @@ fn rewrite_chain_subexpr(expr: &ast::Expr, None } } + ast::ExprKind::Try(_) => { + if width >= 1 { + Some("?".into()) + } else { + None + } + } _ => unreachable!(), } } diff --git a/src/config.rs b/src/config.rs index 333835b66cd..5e4dbefad45 100644 --- a/src/config.rs +++ b/src/config.rs @@ -392,6 +392,7 @@ create_config! { match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm"; closure_block_indent_threshold: isize, 5, "How many lines a closure must have before it is \ block indented. -1 means never use block indent."; + use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand"; write_mode: WriteMode, WriteMode::Replace, "What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage"; } diff --git a/src/expr.rs b/src/expr.rs index 32fd5f561c3..ebe34400014 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -136,18 +136,25 @@ impl Rewrite for ast::Expr { Some(ident) => format!(" {}", ident.node), None => String::new(), }; - wrap_str(format!("continue{}", id_str), context.config.max_width, width, offset) + wrap_str(format!("continue{}", id_str), + context.config.max_width, + width, + offset) } ast::ExprKind::Break(ref opt_ident) => { let id_str = match *opt_ident { Some(ident) => format!(" {}", ident.node), None => String::new(), }; - wrap_str(format!("break{}", id_str), context.config.max_width, width, offset) + wrap_str(format!("break{}", id_str), + context.config.max_width, + width, + offset) } ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => { rewrite_closure(capture, fn_decl, body, self.span, context, width, offset) } + ast::ExprKind::Try(..) | ast::ExprKind::Field(..) | ast::ExprKind::TupField(..) | ast::ExprKind::MethodCall(..) => rewrite_chain(self, context, width, offset), @@ -199,11 +206,7 @@ impl Rewrite for ast::Expr { rewrite_unary_prefix(context, delim, &**rhs, width, offset) } (Some(ref lhs), None) => { - Some(format!("{}{}", - try_opt!(lhs.rewrite(context, - try_opt!(width.checked_sub(delim.len())), - offset)), - delim)) + rewrite_unary_suffix(context, delim, &**lhs, width, offset) } (None, None) => wrap_str(delim.into(), context.config.max_width, width, offset), } @@ -211,9 +214,7 @@ impl Rewrite for ast::Expr { // We do not format these expressions yet, but they should still // satisfy our width restrictions. ast::ExprKind::InPlace(..) | - ast::ExprKind::InlineAsm(..) | - // TODO(#867): Handle try shorthand - ast::ExprKind::Try(_) => { + ast::ExprKind::InlineAsm(..) => { wrap_str(context.snippet(self.span), context.config.max_width, width, @@ -689,11 +690,8 @@ fn extract_comment(span: Span, -> Option { let comment_str = context.snippet(span); if contains_comment(&comment_str) { - let comment = try_opt!(rewrite_comment(comment_str.trim(), - false, - width, - offset, - context.config)); + let comment = + try_opt!(rewrite_comment(comment_str.trim(), false, width, offset, context.config)); Some(format!("\n{indent}{}\n{indent}", comment, indent = offset.to_string(context.config))) @@ -793,14 +791,11 @@ fn rewrite_if_else(context: &RewriteContext, } }; - let between_if_else_block = mk_sp(if_block.span.hi, - context.codemap.span_before(mk_sp(if_block.span.hi, - else_block.span.lo), - "else")); - let between_if_else_block_comment = extract_comment(between_if_else_block, - &context, - offset, - width); + let between_if_else_block = + mk_sp(if_block.span.hi, + context.codemap.span_before(mk_sp(if_block.span.hi, else_block.span.lo), "else")); + let between_if_else_block_comment = + extract_comment(between_if_else_block, &context, offset, width); let after_else = mk_sp(context.codemap .span_after(mk_sp(if_block.span.hi, else_block.span.lo), @@ -927,11 +922,8 @@ fn rewrite_match_arm_comment(context: &RewriteContext, } let missed_str = missed_str[first..].trim(); if !missed_str.is_empty() { - let comment = try_opt!(rewrite_comment(&missed_str, - false, - width, - arm_indent, - context.config)); + let comment = + try_opt!(rewrite_comment(&missed_str, false, width, arm_indent, context.config)); result.push('\n'); result.push_str(arm_indent_str); result.push_str(&comment); @@ -1155,10 +1147,9 @@ impl Rewrite for ast::Arm { let body_budget = try_opt!(width.checked_sub(context.config.tab_spaces)); let indent = context.block_indent.block_indent(context.config); let inner_context = &RewriteContext { block_indent: indent, ..*context }; - let next_line_body = try_opt!(nop_block_collapse(body.rewrite(inner_context, - body_budget, - indent), - body_budget)); + let next_line_body = + try_opt!(nop_block_collapse(body.rewrite(inner_context, body_budget, indent), + body_budget)); let indent_str = offset.block_indent(context.config).to_string(context.config); let (body_prefix, body_suffix) = if context.config.wrap_match_arms { if context.config.match_block_trailing_comma { @@ -1762,6 +1753,21 @@ pub fn rewrite_unary_prefix(context: &RewriteContext, .map(|r| format!("{}{}", prefix, r)) } +// FIXME: this is probably not correct for multi-line Rewrites. we should +// subtract suffix.len() from the last line budget, not the first! +pub fn rewrite_unary_suffix(context: &RewriteContext, + suffix: &str, + rewrite: &R, + width: usize, + offset: Indent) + -> Option { + rewrite.rewrite(context, try_opt!(width.checked_sub(suffix.len())), offset) + .map(|mut r| { + r.push_str(suffix); + r + }) +} + fn rewrite_unary_op(context: &RewriteContext, op: &ast::UnOp, expr: &ast::Expr, @@ -1817,24 +1823,42 @@ pub fn rewrite_assign_rhs>(context: &RewriteContext, let max_width = try_opt!(width.checked_sub(last_line_width + 1)); let rhs = ex.rewrite(&context, max_width, offset + last_line_width + 1); + fn count_line_breaks(src: &str) -> usize { + src.chars().filter(|&x| x == '\n').count() + } + match rhs { - Some(new_str) => { + Some(ref new_str) if count_line_breaks(new_str) < 2 => { result.push(' '); - result.push_str(&new_str) + result.push_str(new_str); } - None => { - // Expression did not fit on the same line as the identifier. Retry - // on the next line. + _ => { + // Expression did not fit on the same line as the identifier or is + // at least three lines big. Try splitting the line and see + // if that works better. let new_offset = offset.block_indent(context.config); - result.push_str(&format!("\n{}", new_offset.to_string(context.config))); - - // FIXME: we probably should related max_width to width instead of - // config.max_width where is the 1 coming from anyway? - let max_width = try_opt!(context.config.max_width.checked_sub(new_offset.width() + 1)); + let max_width = try_opt!((width + offset.width()).checked_sub(new_offset.width())); let inner_context = context.nested_context(); - let rhs = ex.rewrite(&inner_context, max_width, new_offset); - - result.push_str(&&try_opt!(rhs)); + let new_rhs = ex.rewrite(&inner_context, max_width, new_offset); + + // FIXME: DRY! + match (rhs, new_rhs) { + (Some(ref orig_rhs), Some(ref replacement_rhs)) + if count_line_breaks(orig_rhs) > + count_line_breaks(replacement_rhs) + 1 => { + result.push_str(&format!("\n{}", new_offset.to_string(context.config))); + result.push_str(replacement_rhs); + } + (None, Some(ref final_rhs)) => { + result.push_str(&format!("\n{}", new_offset.to_string(context.config))); + result.push_str(final_rhs); + } + (None, None) => return None, + (Some(ref orig_rhs), _) => { + result.push(' '); + result.push_str(orig_rhs); + } + } } } diff --git a/src/items.rs b/src/items.rs index f3b4d1859a1..b4b200e8b05 100644 --- a/src/items.rs +++ b/src/items.rs @@ -66,11 +66,8 @@ impl Rewrite for ast::Local { let budget = try_opt!(width.checked_sub(context.block_indent.width() + 1)); // 1 = trailing semicolon; - result = try_opt!(rewrite_assign_rhs(&context, - result, - ex, - budget, - context.block_indent)); + result = + try_opt!(rewrite_assign_rhs(&context, result, ex, budget, context.block_indent)); } result.push(';'); @@ -656,18 +653,17 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) let has_body = !trait_items.is_empty(); - let where_density = if (context.config.where_density == Density::Compressed && - (!result.contains('\n') || - context.config.fn_args_layout == FnArgLayoutStyle::Block)) || - (context.config.fn_args_layout == FnArgLayoutStyle::Block && - result.is_empty()) || - (context.config.where_density == Density::CompressedIfEmpty && - !has_body && - !result.contains('\n')) { - Density::Compressed - } else { - Density::Tall - }; + let where_density = + if (context.config.where_density == Density::Compressed && + (!result.contains('\n') || + context.config.fn_args_layout == FnArgLayoutStyle::Block)) || + (context.config.fn_args_layout == FnArgLayoutStyle::Block && result.is_empty()) || + (context.config.where_density == Density::CompressedIfEmpty && !has_body && + !result.contains('\n')) { + Density::Compressed + } else { + Density::Tall + }; let where_budget = try_opt!(context.config .max_width @@ -1134,9 +1130,8 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf, let mut_str = format_mutability(m); match lt { Some(ref l) => { - let lifetime_str = try_opt!(l.rewrite(context, - usize::max_value(), - Indent::empty())); + let lifetime_str = + try_opt!(l.rewrite(context, usize::max_value(), Indent::empty())); Some(format!("&{} {}self", lifetime_str, mut_str)) } None => Some(format!("&{}self", mut_str)), diff --git a/src/lib.rs b/src/lib.rs index b825059ee20..61ac2626e02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -403,11 +403,8 @@ pub fn format_input(input: Input, config: &Config) -> (Summary, FileMap, FormatR let mut summary = Summary::new(); let codemap = Rc::new(CodeMap::new()); - let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto, - None, - true, - false, - codemap.clone()); + let tty_handler = + Handler::with_tty_emitter(ColorConfig::Auto, None, true, false, codemap.clone()); let mut parse_session = ParseSess::with_span_handler(tty_handler, codemap.clone()); let main_file = match input { diff --git a/src/lists.rs b/src/lists.rs index f966e41ce99..b35ccfe3d69 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -307,11 +307,8 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option comment.trim().contains('\n') || comment.trim().len() > width; - let formatted_comment = try_opt!(rewrite_comment(comment, - block_style, - width, - offset, - formatting.config)); + let formatted_comment = + try_opt!(rewrite_comment(comment, block_style, width, offset, formatting.config)); result.push(' '); result.push_str(&formatted_comment); diff --git a/src/macros.rs b/src/macros.rs index a0c7cd51bb7..e9992fb0eb0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -25,7 +25,7 @@ use syntax::parse::tts_to_parser; use syntax::codemap::{mk_sp, BytePos}; use Indent; -use rewrite::RewriteContext; +use rewrite::{Rewrite, RewriteContext}; use expr::{rewrite_call, rewrite_array}; use comment::{FindUncommented, contains_comment}; use utils::{CodeMapSpanUtils, wrap_str}; @@ -56,6 +56,12 @@ pub fn rewrite_macro(mac: &ast::Mac, width: usize, offset: Indent) -> Option { + if context.config.use_try_shorthand { + if let Some(expr) = convert_try_mac(mac, context) { + return expr.rewrite(context, width, offset); + } + } + let original_style = macro_style(mac, context); let macro_name = match extra_ident { None | @@ -141,6 +147,24 @@ pub fn rewrite_macro(mac: &ast::Mac, } } +/// Tries to convert a macro use into a short hand try expression. Returns None +/// when the macro is not an instance of try! (or parsing the inner expression +/// failed). +pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option { + if &format!("{}", mac.node.path)[..] == "try" { + let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone(), Vec::new()); + + Some(ast::Expr { + id: 0, // dummy value + node: ast::ExprKind::Try(try_opt!(parser.parse_expr().ok())), + span: mac.span, // incorrect span, but shouldn't matter too much + attrs: None, + }) + } else { + None + } +} + fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle { let snippet = context.snippet(mac.span); let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value()); diff --git a/src/patterns.rs b/src/patterns.rs index ba3621fc6d5..4881ff76a99 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -144,10 +144,8 @@ impl Rewrite for Pat { |f| f.node.rewrite(context, budget, offset), context.codemap.span_after(self.span, "{"), self.span.hi); - let mut field_string = try_opt!(format_item_list(items, - budget, - offset, - context.config)); + let mut field_string = + try_opt!(format_item_list(items, budget, offset, context.config)); if elipses { if field_string.contains('\n') { field_string.push_str(",\n"); diff --git a/src/types.rs b/src/types.rs index cf65f4426b3..0ead12a9f98 100644 --- a/src/types.rs +++ b/src/types.rs @@ -375,12 +375,8 @@ impl Rewrite for ast::WherePredicate { // 3 = " = ".len() let used_width = 3 + ty_str.len(); let budget = try_opt!(width.checked_sub(used_width)); - let path_str = try_opt!(rewrite_path(context, - false, - None, - path, - budget, - offset + used_width)); + let path_str = + try_opt!(rewrite_path(context, false, None, path, budget, offset + used_width)); format!("{} = {}", path_str, ty_str) } }; @@ -538,9 +534,8 @@ impl Rewrite for ast::Ty { Some(match *lifetime { Some(ref lifetime) => { let lt_budget = try_opt!(width.checked_sub(2 + mut_len)); - let lt_str = try_opt!(lifetime.rewrite(context, - lt_budget, - offset + 2 + mut_len)); + let lt_str = + try_opt!(lifetime.rewrite(context, lt_budget, offset + 2 + mut_len)); let lt_len = lt_str.len(); let budget = try_opt!(width.checked_sub(2 + mut_len + lt_len)); format!("&{} {}{}", diff --git a/tests/source/chains.rs b/tests/source/chains.rs index e7d9783a51c..48de948865b 100644 --- a/tests/source/chains.rs +++ b/tests/source/chains.rs @@ -112,3 +112,16 @@ fn issue587() { std::mem::transmute(dl.symbol::<()>("init").unwrap()) } + +fn try_shorthand() { + let x = expr?; + let y = expr.kaas()?.test(); + let loooooooooooooooooooooooooooooooooooooooooong = does_this?.look?.good?.should_we_break?.after_the_first_question_mark?; + let yyyy = expr?.another?.another?.another?.another?.another?.another?.another?.another?.test(); + let zzzz = expr?.another?.another?.another?.another?; + let aaa = x ???????????? ?????????????? ???? ????? ?????????????? ????????? ?????????????? ??; + + let y = a.very .loooooooooooooooooooooooooooooooooooooong() .chain() + .inside() .weeeeeeeeeeeeeee()? .test() .0 + .x; +} diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 16953a932a3..40fd3309a4c 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -249,7 +249,6 @@ fn ranges() { let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; let z = ... x ; - let infi_range_2 = ... ; a ... b diff --git a/tests/source/single-line-if-else.rs b/tests/source/single-line-if-else.rs index 42629ab8e37..2f9c19086ff 100644 --- a/tests/source/single-line-if-else.rs +++ b/tests/source/single-line-if-else.rs @@ -36,7 +36,7 @@ fn main() { do_something() } - let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb }; + let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb }; let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb }; diff --git a/tests/source/string-lit-2.rs b/tests/source/string-lit-2.rs index 8ba60ccac1d..c19df093176 100644 --- a/tests/source/string-lit-2.rs +++ b/tests/source/string-lit-2.rs @@ -1,9 +1,5 @@ fn main() -> &'static str { - let too_many_lines = "H\ - e\ - l\ - l\ - o"; + let too_many_lines = "Hello"; let leave_me = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\ s jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"; diff --git a/tests/source/try-conversion.rs b/tests/source/try-conversion.rs new file mode 100644 index 00000000000..addf2f5d5e8 --- /dev/null +++ b/tests/source/try-conversion.rs @@ -0,0 +1,11 @@ +// rustfmt-use_try_shorthand: true + +fn main() { + let x = try!(some_expr()); + + let y = try!(a.very.loooooooooooooooooooooooooooooooooooooong().chain().inside().weeeeeeeeeeeeeee()).test().0.x; +} + +fn test() { + a? +} diff --git a/tests/target/chains.rs b/tests/target/chains.rs index aacdb8e93e1..7d25046279f 100644 --- a/tests/target/chains.rs +++ b/tests/target/chains.rs @@ -133,3 +133,22 @@ fn issue587() { std::mem::transmute(dl.symbol::<()>("init").unwrap()) } + +fn try_shorthand() { + let x = expr?; + let y = expr.kaas()?.test(); + let loooooooooooooooooooooooooooooooooooooooooong = + does_this?.look?.good?.should_we_break?.after_the_first_question_mark?; + let yyyy = expr?.another?.another?.another?.another?.another?.another?.another?.another?.test(); + let zzzz = expr?.another?.another?.another?.another?; + let aaa = x??????????????????????????????????????????????????????????????????????????; + + let y = a.very + .loooooooooooooooooooooooooooooooooooooong() + .chain() + .inside() + .weeeeeeeeeeeeeee()? + .test() + .0 + .x; +} diff --git a/tests/target/closure.rs b/tests/target/closure.rs index 5f40610d603..8fe290b955a 100644 --- a/tests/target/closure.rs +++ b/tests/target/closure.rs @@ -46,23 +46,18 @@ fn main() { do_something_else(); }; - let arg_test = |big_argument_name, test123| { - looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame() - }; + let arg_test = + |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame(); - let arg_test = |big_argument_name, test123| { - looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame() - }; + let arg_test = + |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame(); let simple_closure = move || -> () {}; let closure = |input: Ty| -> Option { foo() }; - let closure_with_return_type = |aaaaaaaaaaaaaaaaaaaaaaarg1, - aaaaaaaaaaaaaaaaaaaaaaarg2| - -> Strong { - "sup".to_owned() - }; + let closure_with_return_type = + |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() }; |arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3; diff --git a/tests/target/expr.rs b/tests/target/expr.rs index e9a051711f5..1378b4bf8f8 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -202,10 +202,8 @@ fn arrays() { item: 3, }]); - let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, - yyyyyyyyyyyyyyyyyyyyyyyyyyy, - zzzzzzzzzzzzzzzzzz, - q]; + let z = + [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q]; [1 + 3, 4, 5, 6, 7, 7, fncall::>(3 - 1)] } @@ -273,7 +271,6 @@ fn ranges() { let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; let z = ...x; - let infi_range_2 = ...; a...b diff --git a/tests/target/hard-tabs.rs b/tests/target/hard-tabs.rs index 0914959ceb1..1e00559b3f3 100644 --- a/tests/target/hard-tabs.rs +++ b/tests/target/hard-tabs.rs @@ -55,10 +55,8 @@ fn main() { .go_to_next_line_with_tab() .go_to_next_line_with_tab(); - let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, - yyyyyyyyyyyyyyyyyyyyyyyyyyy, - zzzzzzzzzzzzzzzzzz, - q]; + let z = + [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q]; fn generic(arg: T) -> &SomeType where T: Fn(// First arg diff --git a/tests/target/single-line-if-else.rs b/tests/target/single-line-if-else.rs index 27b6d773da5..ec4daa727a6 100644 --- a/tests/target/single-line-if-else.rs +++ b/tests/target/single-line-if-else.rs @@ -41,7 +41,7 @@ fn main() { } let x = if veeeeeeeeery_loooooong_condition() { - aaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb }; diff --git a/tests/target/static.rs b/tests/target/static.rs index f3dbdad3e76..f2421c1444c 100644 --- a/tests/target/static.rs +++ b/tests/target/static.rs @@ -1,13 +1,11 @@ const FILE_GENERIC_READ: DWORD = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE; -static boolnames: &'static [&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc", - "km", "hs", "in", "db", "da", "mir", "msgr", "os", - "eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i", - "chts", "nrrmc", "npc", "ndscr", "ccc", "bce", - "hls", "xhpa", "crxm", "daisy", "xvpa", "sam", - "cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT", - "OTNL", "OTpt", "OTxr"]; +static boolnames: &'static [&'static str] = + &["bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc", "km", "hs", "in", "db", "da", "mir", + "msgr", "os", "eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i", "chts", "nrrmc", "npc", + "ndscr", "ccc", "bce", "hls", "xhpa", "crxm", "daisy", "xvpa", "sam", "cpix", "lpix", + "OTbs", "OTns", "OTnc", "OTMT", "OTNL", "OTpt", "OTxr"]; static mut name: SomeType = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; diff --git a/tests/target/string-lit-2.rs b/tests/target/string-lit-2.rs index c636f878ef1..7deb4215d2e 100644 --- a/tests/target/string-lit-2.rs +++ b/tests/target/string-lit-2.rs @@ -1,9 +1,5 @@ fn main() -> &'static str { - let too_many_lines = "H\ - e\ - l\ - l\ - o"; + let too_many_lines = "Hello"; let leave_me = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\ s jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"; diff --git a/tests/target/try-conversion.rs b/tests/target/try-conversion.rs new file mode 100644 index 00000000000..d4422cf9621 --- /dev/null +++ b/tests/target/try-conversion.rs @@ -0,0 +1,18 @@ +// rustfmt-use_try_shorthand: true + +fn main() { + let x = some_expr()?; + + let y = a.very + .loooooooooooooooooooooooooooooooooooooong() + .chain() + .inside() + .weeeeeeeeeeeeeee()? + .test() + .0 + .x; +} + +fn test() { + a? +} diff --git a/tests/target/type-ascription.rs b/tests/target/type-ascription.rs index 22f03713388..de8d97d7b67 100644 --- a/tests/target/type-ascription.rs +++ b/tests/target/type-ascription.rs @@ -1,7 +1,6 @@ fn main() { - let xxxxxxxxxxx = yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: SomeTrait; + let xxxxxxxxxxx = + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: SomeTrait; let xxxxxxxxxxxxxxx = yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;