Skip to content

Commit

Permalink
Avoid poor-formatting of the rhs of lazy_static macro (#4016)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro authored Feb 19, 2020
2 parents 4464ab7 + 5addca7 commit b676fd0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
4 changes: 1 addition & 3 deletions rustfmt-core/rustfmt-lib/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ pub(crate) fn format_expr(
ast::ExprKind::Block(ref block, opt_label) => {
match expr_type {
ExprType::Statement => {
if is_unsafe_block(block) {
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
} else if let rw @ Some(_) =
if let rw @ Some(_) =
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
{
// Rewrite block without trying to put it in a single line.
Expand Down
12 changes: 8 additions & 4 deletions rustfmt-core/rustfmt-lib/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::shape::{Indent, Shape};
use crate::source_map::SpanUtils;
use crate::spanned::Spanned;
use crate::utils::{
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
count_newlines, format_visibility, indent_next_line, is_empty_line, mk_sp,
remove_trailing_white_spaces, rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
};
use crate::visitor::FmtVisitor;

Expand Down Expand Up @@ -1473,7 +1473,7 @@ fn format_lazy_static(
parser.eat(&TokenKind::Colon);
let ty = parse_or!(parse_ty);
parser.eat(&TokenKind::Eq);
let expr = parse_or!(parse_expr);
let expr = parse_or!(parse_stmt)?;
parser.eat(&TokenKind::Semi);

// Rewrite as a static item.
Expand All @@ -1487,11 +1487,15 @@ fn format_lazy_static(
result.push_str(&crate::expr::rewrite_assign_rhs(
context,
stmt,
&*expr,
&expr,
nested_shape.sub_width(1)?,
)?);
result.push(';');
if parser.token.kind != TokenKind::Eof {
let snippet = context.snippet(mk_sp(parser.prev_span.hi(), parser.token.span.lo()));
if count_newlines(snippet) >= 2 {
result.push('\n');
}
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
}
}
Expand Down
10 changes: 10 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/lazy_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ static ref FOO: HashMap<String,
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
),> = HashMap::new();
}

lazy_static! {
pub static ref BLOCKING_POOL: tokio_threadpool::ThreadPool = {
tokio_threadpool::Builder::new().pool_size(1).build()
};

static ref FOO: Foo = unsafe {
very_long_function_name().another_function_with_really_long_name()
};
}
4 changes: 3 additions & 1 deletion rustfmt-core/rustfmt-lib/tests/target/catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ fn main() {

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

let x = try { unsafe { foo()? } };
let x = try {
unsafe { foo()? }
};

let y = match (try { foo()? }) {
_ => (),
Expand Down
4 changes: 3 additions & 1 deletion rustfmt-core/rustfmt-lib/tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ fn baz() {
// Regular unsafe block
}

unsafe { foo() }
unsafe {
foo()
}

unsafe {
foo();
Expand Down
10 changes: 10 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/lazy_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ lazy_static! {
),
> = HashMap::new();
}

lazy_static! {
pub static ref BLOCKING_POOL: tokio_threadpool::ThreadPool = {
tokio_threadpool::Builder::new().pool_size(1).build()
};

static ref FOO: Foo = unsafe {
very_long_function_name().another_function_with_really_long_name()
};
}
4 changes: 3 additions & 1 deletion rustfmt-core/rustfmt-lib/tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ fn issue383() {

fn issue507() {
match 1 {
1 => unsafe { std::intrinsics::abort() },
1 => unsafe {
std::intrinsics::abort()
},
_ => (),
}
}
Expand Down

0 comments on commit b676fd0

Please sign in to comment.