parent
52f9f7bbe2
commit
d40ed146a5
@ -118,6 +118,22 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext) -> bo
|
||||
|| 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.
|
||||
fn rewrite_closure_with_block(
|
||||
body: &ast::Expr,
|
||||
@ -126,7 +142,7 @@ fn rewrite_closure_with_block(
|
||||
shape: Shape,
|
||||
) -> Option<String> {
|
||||
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 {
|
||||
return None;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use shape::Shape;
|
||||
use spanned::Spanned;
|
||||
use utils::{mk_sp, rewrite_ident};
|
||||
use utils::{is_same_visibility, mk_sp, rewrite_ident};
|
||||
use visitor::FmtVisitor;
|
||||
|
||||
use std::borrow::Cow;
|
||||
@ -485,10 +485,7 @@ fn same_visibility(&self, other: &UseTree) -> bool {
|
||||
}),
|
||||
)
|
||||
| (None, None) => true,
|
||||
(
|
||||
Some(codemap::Spanned { node: lnode, .. }),
|
||||
Some(codemap::Spanned { node: rnode, .. }),
|
||||
) => lnode == rnode,
|
||||
(Some(ref a), Some(ref b)) => is_same_visibility(a, b),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
20
src/utils.rs
20
src/utils.rs
@ -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.
|
||||
pub fn format_visibility(context: &RewriteContext, vis: &Visibility) -> Cow<'static, str> {
|
||||
match vis.node {
|
||||
|
Loading…
Reference in New Issue
Block a user