1
Fork 0

Merge pull request #358 from eefriedman/rewrite-return

Add support for formatting "return" expressions.
This commit is contained in:
Nick Cameron 2015-09-24 18:19:37 +12:00
commit d941fe20b4
4 changed files with 43 additions and 9 deletions

View file

@ -157,9 +157,22 @@ impl Rewrite for ast::Expr {
width,
offset))
}
ast::Expr_::ExprRet(None) => {
wrap_str("return".to_owned(), context.config.max_width, width, offset)
}
ast::Expr_::ExprRet(Some(ref expr)) => {
rewrite_unary_prefix(context, "return ", &expr, width, offset)
}
// We do not format these expressions yet, but they should still
// satisfy our width restrictions.
_ => wrap_str(context.snippet(self.span), context.config.max_width, width, offset),
ast::Expr_::ExprBox(..) |
ast::Expr_::ExprCast(..) |
ast::Expr_::ExprIndex(..) |
ast::Expr_::ExprAddrOf(..) |
ast::Expr_::ExprInlineAsm(..) |
ast::Expr_::ExprRepeat(..) => {
wrap_str(context.snippet(self.span), context.config.max_width, width, offset)
}
}
}
}
@ -1318,6 +1331,16 @@ fn rewrite_binary_op(context: &RewriteContext,
rhs_result))
}
fn rewrite_unary_prefix(context: &RewriteContext,
prefix: &str,
expr: &ast::Expr,
width: usize,
offset: Indent)
-> Option<String> {
expr.rewrite(context, try_opt!(width.checked_sub(prefix.len())), offset + prefix.len())
.map(|r| format!("{}{}", prefix, r))
}
fn rewrite_unary_op(context: &RewriteContext,
op: &ast::UnOp,
expr: &ast::Expr,
@ -1331,10 +1354,7 @@ fn rewrite_unary_op(context: &RewriteContext,
ast::UnOp::UnNot => "!",
ast::UnOp::UnNeg => "-",
};
let operator_len = operator_str.len();
expr.rewrite(context, try_opt!(width.checked_sub(operator_len)), offset + operator_len)
.map(|r| format!("{}{}", operator_str, r))
rewrite_unary_prefix(context, operator_str, expr, width, offset)
}
fn rewrite_assignment(context: &RewriteContext,