Skip to content

Commit dc9810e

Browse files
authored
Merge pull request #1942 from topecongiro/fixes
Some Fixes
2 parents f07dec3 + 47062c8 commit dc9810e

File tree

7 files changed

+54
-30
lines changed

7 files changed

+54
-30
lines changed

src/expr.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -630,26 +630,14 @@ fn rewrite_closure(
630630
false
631631
};
632632
if no_return_type && !needs_block {
633-
// lock.stmts.len() == 1
633+
// block.stmts.len() == 1
634634
if let Some(expr) = stmt_expr(&block.stmts[0]) {
635635
if let Some(rw) = rewrite_closure_expr(expr, &prefix, context, body_shape) {
636636
return Some(rw);
637637
}
638638
}
639639
}
640640

641-
if !needs_block {
642-
// We need braces, but we might still prefer a one-liner.
643-
let stmt = &block.stmts[0];
644-
// 4 = braces and spaces.
645-
if let Some(body_shape) = body_shape.sub_width(4) {
646-
// Checks if rewrite succeeded and fits on a single line.
647-
if let Some(rewrite) = and_one_line(stmt.rewrite(context, body_shape)) {
648-
return Some(format!("{} {{ {} }}", prefix, rewrite));
649-
}
650-
}
651-
}
652-
653641
// Either we require a block, or tried without and failed.
654642
rewrite_closure_block(block, &prefix, context, body_shape)
655643
} else {
@@ -882,13 +870,8 @@ impl Rewrite for ast::Stmt {
882870
""
883871
};
884872

885-
let expr_type = match self.node {
886-
ast::StmtKind::Expr(_) => ExprType::SubExpression,
887-
ast::StmtKind::Semi(_) => ExprType::Statement,
888-
_ => unreachable!(),
889-
};
890873
let shape = try_opt!(shape.sub_width(suffix.len()));
891-
format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
874+
format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix)
892875
}
893876
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
894877
};

src/visitor.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, C
2222
FindUncommented};
2323
use comment::rewrite_comment;
2424
use config::{BraceStyle, Config};
25-
use expr::{format_expr, ExprType};
2625
use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type,
2726
rewrite_static, rewrite_type_alias};
2827
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace,
@@ -77,12 +76,7 @@ impl<'a> FmtVisitor<'a> {
7776
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
7877
self.push_rewrite(stmt.span(), rewrite);
7978
}
80-
ast::StmtKind::Expr(ref expr) => {
81-
let rewrite =
82-
format_expr(expr, ExprType::Statement, &self.get_context(), self.shape());
83-
self.push_rewrite(stmt.span(), rewrite)
84-
}
85-
ast::StmtKind::Semi(..) => {
79+
ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
8680
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
8781
self.push_rewrite(stmt.span(), rewrite)
8882
}
@@ -979,7 +973,7 @@ impl<'a> Rewrite for [ast::Attribute] {
979973
Some(&(_, next_attr)) if is_derive(next_attr) => insert_new_line = false,
980974
// If not, rewrite the merged derives.
981975
_ => {
982-
result.push_str(&format!("#[derive({})]", derive_args.join(", ")));
976+
result.push_str(&try_opt!(format_derive(context, &derive_args, shape)));
983977
derive_args.clear();
984978
}
985979
}
@@ -996,6 +990,38 @@ impl<'a> Rewrite for [ast::Attribute] {
996990
}
997991
}
998992

993+
// Format `#[derive(..)]`, using visual indent & mixed style when we need to go multiline.
994+
fn format_derive(context: &RewriteContext, derive_args: &[String], shape: Shape) -> Option<String> {
995+
let mut result = String::with_capacity(128);
996+
result.push_str("#[derive(");
997+
// 11 = `#[derive()]`
998+
let initial_budget = try_opt!(shape.width.checked_sub(11));
999+
let mut budget = initial_budget;
1000+
let num = derive_args.len();
1001+
for (i, a) in derive_args.iter().enumerate() {
1002+
// 2 = `, ` or `)]`
1003+
let width = a.len() + 2;
1004+
if width > budget {
1005+
if i > 0 {
1006+
// Remove trailing whitespace.
1007+
result.pop();
1008+
}
1009+
result.push('\n');
1010+
// 9 = `#[derive(`
1011+
result.push_str(&(shape.indent + 9).to_string(context.config));
1012+
budget = initial_budget;
1013+
} else {
1014+
budget = budget.checked_sub(width).unwrap_or(0);
1015+
}
1016+
result.push_str(a);
1017+
if i != num - 1 {
1018+
result.push_str(", ")
1019+
}
1020+
}
1021+
result.push_str(")]");
1022+
Some(result)
1023+
}
1024+
9991025
fn is_derive(attr: &ast::Attribute) -> bool {
10001026
match attr.meta() {
10011027
Some(meta_item) => match meta_item.node {

tests/source/attrib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ fn attributes_on_statements() {
146146
# [ attr ( on ( mac ) ) ]
147147
foo!();
148148
}
149+
150+
// Large derive
151+
#[derive(Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize, Deserialize)]
152+
pub struct HP(pub u8);

tests/target/attrib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,8 @@ fn attributes_on_statements() {
146146
#[attr(on(mac))]
147147
foo!();
148148
}
149+
150+
// Large derive
151+
#[derive(Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize,
152+
Deserialize)]
153+
pub struct HP(pub u8);

tests/target/chains-visual.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ fn main() {
4242
});
4343

4444
fffffffffffffffffffffffffffffffffff(a, {
45-
SCRIPT_TASK_ROOT.with(|root| { *root.borrow_mut() = Some(&script_task); });
45+
SCRIPT_TASK_ROOT.with(|root| {
46+
*root.borrow_mut() = Some(&script_task);
47+
});
4648
});
4749

4850
let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum =

tests/target/chains.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ fn main() {
4545
});
4646

4747
fffffffffffffffffffffffffffffffffff(a, {
48-
SCRIPT_TASK_ROOT.with(|root| { *root.borrow_mut() = Some(&script_task); });
48+
SCRIPT_TASK_ROOT.with(|root| {
49+
*root.borrow_mut() = Some(&script_task);
50+
});
4951
});
5052

5153
let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum =

tests/target/hard-tabs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ fn main() {
8080
});
8181

8282
fffffffffffffffffffffffffffffffffff(a, {
83-
SCRIPT_TASK_ROOT.with(|root| { *root.borrow_mut() = Some(&script_task); });
83+
SCRIPT_TASK_ROOT.with(|root| {
84+
*root.borrow_mut() = Some(&script_task);
85+
});
8486
});
8587
a.b.c.d();
8688

0 commit comments

Comments
 (0)