Skip to content

Commit 72c2411

Browse files
committed
minor fix
1 parent 19a5e1c commit 72c2411

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/items.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ impl Rewrite for ast::Local {
128128

129129
if let Some((init, else_block)) = self.kind.init_else_opt() {
130130
// 1 = trailing semicolon;
131-
let nested_shape = shape.sub_width(1).unknown_error()?;
131+
let nested_shape = shape
132+
.sub_width(1)
133+
.max_width_error(shape.width, self.span())?;
132134

133135
result = rewrite_assign_rhs(
134136
context,
@@ -1862,6 +1864,7 @@ impl Rewrite for ast::FieldDef {
18621864
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
18631865
self.rewrite_result(context, shape).ok()
18641866
}
1867+
18651868
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
18661869
rewrite_struct_field(context, self, shape, 0)
18671870
}
@@ -1912,14 +1915,11 @@ pub(crate) fn rewrite_struct_field(
19121915
spacing.push(' ');
19131916
}
19141917

1915-
// Question. Why don't we immediately return None/Err with ?
1916-
// let orig_ty = shape
1917-
// .offset_left(overhead + spacing.len())
1918-
// .and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
1919-
let ty_shape = shape
1918+
let orig_ty = shape
19201919
.offset_left(overhead + spacing.len())
1921-
.max_width_error(shape.width, field.ty.span())?;
1922-
let orig_ty = field.ty.rewrite_result(context, ty_shape);
1920+
.unknown_error()
1921+
.and_then(|ty_shape| field.ty.rewrite_result(context, ty_shape));
1922+
19231923
if let Ok(ref ty) = orig_ty {
19241924
if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) {
19251925
return Ok(attr_prefix + &spacing + ty);
@@ -2101,25 +2101,26 @@ impl Rewrite for ast::FnRetTy {
21012101
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
21022102
self.rewrite_result(context, shape).ok()
21032103
}
2104+
21042105
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
21052106
match *self {
21062107
ast::FnRetTy::Default(_) => Ok(String::new()),
21072108
ast::FnRetTy::Ty(ref ty) => {
2109+
let arrow_width = "-> ".len();
21082110
if context.config.version() == Version::One
21092111
|| context.config.indent_style() == IndentStyle::Visual
21102112
{
21112113
let inner_width = shape
21122114
.width
2113-
.checked_sub(3)
2115+
.checked_sub(arrow_width)
21142116
.max_width_error(shape.width, self.span())?;
21152117
return ty
21162118
.rewrite_result(context, Shape::legacy(inner_width, shape.indent + 3))
21172119
.map(|r| format!("-> {}", r));
21182120
}
21192121

2120-
// 3 = "-> "
21212122
let shape = shape
2122-
.offset_left(3)
2123+
.offset_left(arrow_width)
21232124
.max_width_error(shape.width, self.span())?;
21242125

21252126
ty.rewrite_result(context, shape)

src/rewrite.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) type RewriteResult = Result<String, RewriteError>;
1818
pub(crate) trait Rewrite {
1919
/// Rewrite self into shape.
2020
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>;
21+
2122
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
2223
self.rewrite(context, shape).ok_or(RewriteError::Unknown)
2324
}
@@ -81,6 +82,7 @@ pub(crate) struct RewriteContext<'a> {
8182
pub(crate) skip_context: SkipContext,
8283
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
8384
}
85+
8486
pub(crate) struct InsideMacroGuard {
8587
is_nested_macro_context: bool,
8688
inside_macro_ref: Rc<Cell<bool>>,

0 commit comments

Comments
 (0)