mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-01 03:03:40 +00:00
Merge pull request #1016 from rust-lang-nursery/try-double-indent
Treat chains with just expr? specially.
This commit is contained in:
commit
0087306761
@ -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();
|
||||
|
@ -134,3 +134,18 @@ fn try_shorthand() {
|
||||
|tcx| tcx.lookup_item_type(def_id).generics)?;
|
||||
fooooooooooooooooooooooooooo()?.bar()?.baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz()?;
|
||||
}
|
||||
|
||||
fn issue_1004() {
|
||||
match *self {
|
||||
ty::ImplOrTraitItem::MethodTraitItem(ref i) => write!(f, "{:?}", i),
|
||||
ty::ImplOrTraitItem::ConstTraitItem(ref i) => write!(f, "{:?}", i),
|
||||
ty::ImplOrTraitItem::TypeTraitItem(ref i) => write!(f, "{:?}", i),
|
||||
}
|
||||
?;
|
||||
|
||||
ty::tls::with(|tcx| {
|
||||
let tap = ty::Binder(TraitAndProjections(principal, projections));
|
||||
in_binder(f, tcx, &ty::Binder(""), Some(tap))
|
||||
})
|
||||
?;
|
||||
}
|
||||
|
@ -163,3 +163,16 @@ fn try_shorthand() {
|
||||
.bar()?
|
||||
.baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz()?;
|
||||
}
|
||||
|
||||
fn issue_1004() {
|
||||
match *self {
|
||||
ty::ImplOrTraitItem::MethodTraitItem(ref i) => write!(f, "{:?}", i),
|
||||
ty::ImplOrTraitItem::ConstTraitItem(ref i) => write!(f, "{:?}", i),
|
||||
ty::ImplOrTraitItem::TypeTraitItem(ref i) => write!(f, "{:?}", i),
|
||||
}?;
|
||||
|
||||
ty::tls::with(|tcx| {
|
||||
let tap = ty::Binder(TraitAndProjections(principal, projections));
|
||||
in_binder(f, tcx, &ty::Binder(""), Some(tap))
|
||||
})?;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user