Skip to content

Commit 8f944d0

Browse files
Account for previous double-counting of semicolon size within format_expr
1 parent 10e6db2 commit 8f944d0

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

src/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ force_multiline_blocks = false
673673
fn_params_layout = "Tall"
674674
brace_style = "SameLineWhere"
675675
control_brace_style = "AlwaysSameLine"
676-
trailing_semicolon = preserve
676+
trailing_semicolon = "Preserve"
677677
trailing_comma = "Vertical"
678678
match_block_trailing_comma = false
679679
blank_lines_upper_bound = 1

src/expr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::types::{rewrite_path, PathContext};
3232
use crate::utils::{
3333
colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
3434
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
35-
semicolon_for_expr, unicode_str_width, wrap_str,
35+
semicolon_for_expr_extra_hacky_double_counted_spacing, unicode_str_width, wrap_str,
3636
};
3737
use crate::vertical::rewrite_with_alignment;
3838
use crate::visitor::FmtVisitor;
@@ -64,7 +64,9 @@ pub(crate) fn format_expr(
6464
if contains_skip(&*expr.attrs) {
6565
return Some(context.snippet(expr.span()).to_owned());
6666
}
67-
let shape = if expr_type == ExprType::Statement && semicolon_for_expr(context, expr) {
67+
let shape = if expr_type == ExprType::Statement
68+
&& semicolon_for_expr_extra_hacky_double_counted_spacing(context, expr)
69+
{
6870
shape.sub_width(1)?
6971
} else {
7072
shape

src/stmt.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::rewrite::{Rewrite, RewriteContext};
88
use crate::shape::Shape;
99
use crate::source_map::LineRangeUtils;
1010
use crate::spanned::Spanned;
11-
use crate::utils::semicolon_for_stmt;
11+
use crate::utils::{semicolon_for_expr, semicolon_for_stmt};
1212

1313
pub(crate) struct Stmt<'a> {
1414
inner: &'a ast::Stmt,
@@ -116,7 +116,7 @@ fn format_stmt(
116116

117117
let result = match stmt.kind {
118118
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
119-
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
119+
ast::StmtKind::Semi(ref ex) => {
120120
let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) {
121121
";"
122122
} else {
@@ -126,6 +126,16 @@ fn format_stmt(
126126
let shape = shape.sub_width(suffix.len())?;
127127
format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
128128
}
129+
ast::StmtKind::Expr(ref ex) => {
130+
let suffix = if semicolon_for_expr(context, ex) {
131+
";"
132+
} else {
133+
""
134+
};
135+
136+
let shape = shape.sub_width(suffix.len())?;
137+
format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
138+
}
129139
ast::StmtKind::MacCall(..) | ast::StmtKind::Item(..) | ast::StmtKind::Empty => None,
130140
};
131141
result.and_then(|res| recover_comment_removed(res, stmt.span(), context))

src/utils.rs

+25
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr)
290290
}
291291
}
292292

293+
/// Previously, we used to have `trailing_semicolon = always` enabled, and due to
294+
/// a bug between `format_stmt` and `format_expr`, we used to subtract the size of
295+
/// `;` *TWICE* from the shape. This means that an expr that would fit onto a line
296+
/// of, e.g. 99 (limit 100) after subtracting one for the semicolon would still be
297+
/// wrapped.
298+
///
299+
/// This function reimplements the old heuristic of double counting the "phantom"
300+
/// semicolon that should have already been accounted for, to not break existing
301+
/// formatting with the `trailing_semicolon = preserve` behavior.
302+
#[inline]
303+
pub(crate) fn semicolon_for_expr_extra_hacky_double_counted_spacing(
304+
context: &RewriteContext<'_>,
305+
expr: &ast::Expr,
306+
) -> bool {
307+
match expr.kind {
308+
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => {
309+
match context.config.trailing_semicolon() {
310+
TrailingSemicolon::Always | TrailingSemicolon::Preserve => true,
311+
TrailingSemicolon::Never => false,
312+
}
313+
}
314+
_ => false,
315+
}
316+
}
317+
293318
#[inline]
294319
pub(crate) fn semicolon_for_stmt(
295320
context: &RewriteContext<'_>,

tests/target/expr.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,18 @@ fn issue3226() {
536536
{
537537
{
538538
{
539-
return Err(ErrorKind::ManagementInterfaceError("Server exited unexpectedly").into())
539+
return Err(
540+
ErrorKind::ManagementInterfaceError("Server exited unexpectedly").into(),
541+
)
540542
}
541543
}
542544
}
543545
{
544546
{
545547
{
546-
break Err(ErrorKind::ManagementInterfaceError("Server exited unexpectedlyy").into())
548+
break Err(
549+
ErrorKind::ManagementInterfaceError("Server exited unexpectedlyy").into(),
550+
)
547551
}
548552
}
549553
}

0 commit comments

Comments
 (0)