Fix breaking changes

cc https://github.com/rust-lang/rust/pull/51829.
This commit is contained in:
Seiichi Uchida 2018-07-20 16:18:45 +09:00
parent 52f9f7bbe2
commit d40ed146a5
3 changed files with 39 additions and 6 deletions

View file

@ -118,6 +118,22 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext) -> bo
|| prefix.contains('\n') || prefix.contains('\n')
} }
fn veto_block(e: &ast::Expr) -> bool {
match e.node {
ast::ExprKind::Call(..)
| ast::ExprKind::Binary(..)
| ast::ExprKind::Cast(..)
| ast::ExprKind::Type(..)
| ast::ExprKind::Assign(..)
| ast::ExprKind::AssignOp(..)
| ast::ExprKind::Field(..)
| ast::ExprKind::Index(..)
| ast::ExprKind::Range(..)
| ast::ExprKind::Try(..) => true,
_ => false,
}
}
// Rewrite closure with a single expression wrapping its body with block. // Rewrite closure with a single expression wrapping its body with block.
fn rewrite_closure_with_block( fn rewrite_closure_with_block(
body: &ast::Expr, body: &ast::Expr,
@ -126,7 +142,7 @@ fn rewrite_closure_with_block(
shape: Shape, shape: Shape,
) -> Option<String> { ) -> Option<String> {
let left_most = left_most_sub_expr(body); let left_most = left_most_sub_expr(body);
let veto_block = left_most != body && !classify::expr_requires_semi_to_be_stmt(left_most); let veto_block = veto_block(body) && !classify::expr_requires_semi_to_be_stmt(left_most);
if veto_block { if veto_block {
return None; return None;
} }

View file

@ -21,7 +21,7 @@ use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListIte
use rewrite::{Rewrite, RewriteContext}; use rewrite::{Rewrite, RewriteContext};
use shape::Shape; use shape::Shape;
use spanned::Spanned; use spanned::Spanned;
use utils::{mk_sp, rewrite_ident}; use utils::{is_same_visibility, mk_sp, rewrite_ident};
use visitor::FmtVisitor; use visitor::FmtVisitor;
use std::borrow::Cow; use std::borrow::Cow;
@ -485,10 +485,7 @@ impl UseTree {
}), }),
) )
| (None, None) => true, | (None, None) => true,
( (Some(ref a), Some(ref b)) => is_same_visibility(a, b),
Some(codemap::Spanned { node: lnode, .. }),
Some(codemap::Spanned { node: rnode, .. }),
) => lnode == rnode,
_ => false, _ => false,
} }
} }

View file

@ -37,6 +37,26 @@ pub fn extra_offset(text: &str, shape: Shape) -> usize {
} }
} }
pub fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool {
match (&a.node, &b.node) {
(
VisibilityKind::Restricted { path: p, .. },
VisibilityKind::Restricted { path: q, .. },
) => format!("{}", p) == format!("{}", q),
(VisibilityKind::Public, VisibilityKind::Public)
| (VisibilityKind::Inherited, VisibilityKind::Inherited)
| (
VisibilityKind::Crate(CrateSugar::PubCrate),
VisibilityKind::Crate(CrateSugar::PubCrate),
)
| (
VisibilityKind::Crate(CrateSugar::JustCrate),
VisibilityKind::Crate(CrateSugar::JustCrate),
) => true,
_ => false,
}
}
// Uses Cow to avoid allocating in the common cases. // Uses Cow to avoid allocating in the common cases.
pub fn format_visibility(context: &RewriteContext, vis: &Visibility) -> Cow<'static, str> { pub fn format_visibility(context: &RewriteContext, vis: &Visibility) -> Cow<'static, str> {
match vis.node { match vis.node {