about summary refs log tree commit diff
path: root/src/chains.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/chains.rs')
-rw-r--r--src/chains.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/chains.rs b/src/chains.rs
index faf368d41c1..00895561ded 100644
--- a/src/chains.rs
+++ b/src/chains.rs
@@ -88,6 +88,7 @@ use expr::rewrite_call;
 use config::BlockIndentStyle;
 use macros::convert_try_mac;
 
+use std::iter;
 use syntax::{ast, ptr};
 use syntax::codemap::{mk_sp, Span};
 
@@ -99,6 +100,12 @@ pub fn rewrite_chain(expr: &ast::Expr,
     let total_span = expr.span;
     let (parent, subexpr_list) = make_subexpr_list(expr, context);
 
+    // Bail out if the chain is just try sugar, i.e., an expression followed by
+    // any number of `?`s.
+    if chain_only_try(&subexpr_list) {
+        return rewrite_try(&parent, subexpr_list.len(), context, width, offset);
+    }
+
     // Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`.
     let parent_block_indent = chain_base_indent(context, offset);
     let parent_context = &RewriteContext { block_indent: parent_block_indent, ..*context };
@@ -196,6 +203,27 @@ pub fn rewrite_chain(expr: &ast::Expr,
              offset)
 }
 
+// True if the chain is only `?`s.
+fn chain_only_try(exprs: &[ast::Expr]) -> bool {
+    exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node {
+        true
+    } else {
+        false
+    })
+}
+
+pub fn rewrite_try(expr: &ast::Expr,
+                   try_count: usize,
+                   context: &RewriteContext,
+                   width: usize,
+                   offset: Indent)
+                   -> Option<String> {
+    let sub_expr = try_opt!(expr.rewrite(context, width - try_count, offset));
+    Some(format!("{}{}",
+                 sub_expr,
+                 iter::repeat("?").take(try_count).collect::<String>()))
+}
+
 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();