Skip to content

Commit b676fd0

Browse files
authored
Avoid poor-formatting of the rhs of lazy_static macro (#4016)
2 parents 4464ab7 + 5addca7 commit b676fd0

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

rustfmt-core/rustfmt-lib/src/expr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ pub(crate) fn format_expr(
127127
ast::ExprKind::Block(ref block, opt_label) => {
128128
match expr_type {
129129
ExprType::Statement => {
130-
if is_unsafe_block(block) {
131-
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
132-
} else if let rw @ Some(_) =
130+
if let rw @ Some(_) =
133131
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
134132
{
135133
// Rewrite block without trying to put it in a single line.

rustfmt-core/rustfmt-lib/src/macros.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use crate::shape::{Indent, Shape};
3131
use crate::source_map::SpanUtils;
3232
use crate::spanned::Spanned;
3333
use crate::utils::{
34-
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
35-
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
34+
count_newlines, format_visibility, indent_next_line, is_empty_line, mk_sp,
35+
remove_trailing_white_spaces, rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
3636
};
3737
use crate::visitor::FmtVisitor;
3838

@@ -1473,7 +1473,7 @@ fn format_lazy_static(
14731473
parser.eat(&TokenKind::Colon);
14741474
let ty = parse_or!(parse_ty);
14751475
parser.eat(&TokenKind::Eq);
1476-
let expr = parse_or!(parse_expr);
1476+
let expr = parse_or!(parse_stmt)?;
14771477
parser.eat(&TokenKind::Semi);
14781478

14791479
// Rewrite as a static item.
@@ -1487,11 +1487,15 @@ fn format_lazy_static(
14871487
result.push_str(&crate::expr::rewrite_assign_rhs(
14881488
context,
14891489
stmt,
1490-
&*expr,
1490+
&expr,
14911491
nested_shape.sub_width(1)?,
14921492
)?);
14931493
result.push(';');
14941494
if parser.token.kind != TokenKind::Eof {
1495+
let snippet = context.snippet(mk_sp(parser.prev_span.hi(), parser.token.span.lo()));
1496+
if count_newlines(snippet) >= 2 {
1497+
result.push('\n');
1498+
}
14951499
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
14961500
}
14971501
}

rustfmt-core/rustfmt-lib/tests/source/lazy_static.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ static ref FOO: HashMap<String,
4343
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
4444
),> = HashMap::new();
4545
}
46+
47+
lazy_static! {
48+
pub static ref BLOCKING_POOL: tokio_threadpool::ThreadPool = {
49+
tokio_threadpool::Builder::new().pool_size(1).build()
50+
};
51+
52+
static ref FOO: Foo = unsafe {
53+
very_long_function_name().another_function_with_really_long_name()
54+
};
55+
}

rustfmt-core/rustfmt-lib/tests/target/catch.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ fn main() {
66

77
let x = try /* Invisible comment */ { foo()? };
88

9-
let x = try { unsafe { foo()? } };
9+
let x = try {
10+
unsafe { foo()? }
11+
};
1012

1113
let y = match (try { foo()? }) {
1214
_ => (),

rustfmt-core/rustfmt-lib/tests/target/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ fn baz() {
152152
// Regular unsafe block
153153
}
154154

155-
unsafe { foo() }
155+
unsafe {
156+
foo()
157+
}
156158

157159
unsafe {
158160
foo();

rustfmt-core/rustfmt-lib/tests/target/lazy_static.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,13 @@ lazy_static! {
4747
),
4848
> = HashMap::new();
4949
}
50+
51+
lazy_static! {
52+
pub static ref BLOCKING_POOL: tokio_threadpool::ThreadPool = {
53+
tokio_threadpool::Builder::new().pool_size(1).build()
54+
};
55+
56+
static ref FOO: Foo = unsafe {
57+
very_long_function_name().another_function_with_really_long_name()
58+
};
59+
}

rustfmt-core/rustfmt-lib/tests/target/match.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ fn issue383() {
243243

244244
fn issue507() {
245245
match 1 {
246-
1 => unsafe { std::intrinsics::abort() },
246+
1 => unsafe {
247+
std::intrinsics::abort()
248+
},
247249
_ => (),
248250
}
249251
}

0 commit comments

Comments
 (0)