Skip to content

Commit 983aa18

Browse files
committed
impl rewrite_result for ChainItem
1 parent 57c532b commit 983aa18

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/chains.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::config::{IndentStyle, StyleEdition};
6666
use crate::expr::rewrite_call;
6767
use crate::lists::extract_pre_comment;
6868
use crate::macros::convert_try_mac;
69-
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteResult};
69+
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
7070
use crate::shape::Shape;
7171
use crate::source_map::SpanUtils;
7272
use crate::utils::{
@@ -269,7 +269,13 @@ impl ChainItemKind {
269269
impl Rewrite for ChainItem {
270270
// TODO impl rewrite_result after rebase
271271
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
272-
let shape = shape.sub_width(self.tries)?;
272+
self.rewrite_result(context, shape).ok()
273+
}
274+
275+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
276+
let shape = shape
277+
.sub_width(self.tries)
278+
.max_width_error(shape.width, self.span)?;
273279
let rewrite = match self.kind {
274280
ChainItemKind::Parent {
275281
ref expr,
@@ -278,10 +284,9 @@ impl Rewrite for ChainItem {
278284
ChainItemKind::Parent {
279285
ref expr,
280286
parens: false,
281-
} => expr.rewrite(context, shape)?,
287+
} => expr.rewrite_result(context, shape)?,
282288
ChainItemKind::MethodCall(ref segment, ref types, ref exprs) => {
283-
Self::rewrite_method_call(segment.ident, types, exprs, self.span, context, shape)
284-
.ok()?
289+
Self::rewrite_method_call(segment.ident, types, exprs, self.span, context, shape)?
285290
}
286291
ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)),
287292
ChainItemKind::TupleField(ident, nested) => format!(
@@ -295,10 +300,10 @@ impl Rewrite for ChainItem {
295300
),
296301
ChainItemKind::Await => ".await".to_owned(),
297302
ChainItemKind::Comment(ref comment, _) => {
298-
rewrite_comment(comment, false, shape, context.config).ok()?
303+
rewrite_comment(comment, false, shape, context.config)?
299304
}
300305
};
301-
Some(format!("{rewrite}{}", "?".repeat(self.tries)))
306+
Ok(format!("{rewrite}{}", "?".repeat(self.tries)))
302307
}
303308
}
304309

src/expr.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub(crate) fn format_expr(
9797
let callee_str = callee.rewrite(context, shape)?;
9898
rewrite_call(context, &callee_str, args, inner_span, shape).ok()
9999
}
100-
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span),
100+
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span).ok(),
101101
ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
102102
// FIXME: format comments between operands and operator
103103
rewrite_all_pairs(expr, shape, context)
@@ -1484,7 +1484,7 @@ pub(crate) fn rewrite_paren(
14841484
mut subexpr: &ast::Expr,
14851485
shape: Shape,
14861486
mut span: Span,
1487-
) -> Option<String> {
1487+
) -> RewriteResult {
14881488
debug!("rewrite_paren, shape: {:?}", shape);
14891489

14901490
// Extract comments within parens.
@@ -1497,8 +1497,8 @@ pub(crate) fn rewrite_paren(
14971497
// 1 = "(" or ")"
14981498
pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span().lo());
14991499
post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
1500-
pre_comment = rewrite_missing_comment(pre_span, shape, context).ok()?;
1501-
post_comment = rewrite_missing_comment(post_span, shape, context).ok()?;
1500+
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
1501+
post_comment = rewrite_missing_comment(post_span, shape, context)?;
15021502

15031503
// Remove nested parens if there are no comments.
15041504
if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.kind {
@@ -1513,11 +1513,14 @@ pub(crate) fn rewrite_paren(
15131513
}
15141514

15151515
// 1 = `(` and `)`
1516-
let sub_shape = shape.offset_left(1)?.sub_width(1)?;
1517-
let subexpr_str = subexpr.rewrite(context, sub_shape)?;
1516+
let sub_shape = shape
1517+
.offset_left(1)
1518+
.and_then(|s| s.sub_width(1))
1519+
.max_width_error(shape.width, span)?;
1520+
let subexpr_str = subexpr.rewrite_result(context, sub_shape)?;
15181521
let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
15191522
if fits_single_line {
1520-
Some(format!("({pre_comment}{subexpr_str}{post_comment})"))
1523+
Ok(format!("({pre_comment}{subexpr_str}{post_comment})"))
15211524
} else {
15221525
rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
15231526
}
@@ -1529,12 +1532,12 @@ fn rewrite_paren_in_multi_line(
15291532
shape: Shape,
15301533
pre_span: Span,
15311534
post_span: Span,
1532-
) -> Option<String> {
1535+
) -> RewriteResult {
15331536
let nested_indent = shape.indent.block_indent(context.config);
15341537
let nested_shape = Shape::indented(nested_indent, context.config);
1535-
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context).ok()?;
1536-
let post_comment = rewrite_missing_comment(post_span, nested_shape, context).ok()?;
1537-
let subexpr_str = subexpr.rewrite(context, nested_shape)?;
1538+
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
1539+
let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
1540+
let subexpr_str = subexpr.rewrite_result(context, nested_shape)?;
15381541

15391542
let mut result = String::with_capacity(subexpr_str.len() * 2);
15401543
result.push('(');
@@ -1551,7 +1554,7 @@ fn rewrite_paren_in_multi_line(
15511554
result.push_str(&shape.indent.to_string_with_newline(context.config));
15521555
result.push(')');
15531556

1554-
Some(result)
1557+
Ok(result)
15551558
}
15561559

15571560
fn rewrite_index(

0 commit comments

Comments
 (0)