Skip to content

Format the if expression at the end of the block in a single line #3338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,7 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<
}

fn choose_matcher(pats: &[&ast::Pat]) -> &'static str {
if pats.is_empty() {
""
} else {
"let"
}
if pats.is_empty() { "" } else { "let" }
}

impl<'a> ControlFlow<'a> {
Expand Down Expand Up @@ -1192,6 +1188,16 @@ pub fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
}
}

pub(crate) fn stmt_is_if(stmt: &ast::Stmt) -> bool {
match stmt.node {
ast::StmtKind::Expr(ref e) => match e.node {
ast::ExprKind::If(..) => true,
_ => false,
},
_ => false,
}
}

pub fn is_unsafe_block(block: &ast::Block) -> bool {
if let ast::BlockCheckMode::Unsafe(..) = block.rules {
true
Expand Down
25 changes: 20 additions & 5 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use syntax::{ast, visit};

use crate::attr::*;
use crate::comment::{CodeCharKind, CommentCodeSlices, FindUncommented};
use crate::config::{BraceStyle, Config};
use crate::config::{BraceStyle, Config, Version};
use crate::expr::{format_expr, ExprType};
use crate::items::{
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
rewrite_associated_impl_type, rewrite_associated_type, rewrite_existential_impl_type,
Expand All @@ -30,7 +31,7 @@ use crate::source_map::{LineRangeUtils, SpanUtils};
use crate::spanned::Spanned;
use crate::utils::{
self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec,
rewrite_ident, DEPR_SKIP_ANNOTATION,
rewrite_ident, stmt_expr, DEPR_SKIP_ANNOTATION,
};
use crate::{ErrorKind, FormatReport, FormattingError};

Expand Down Expand Up @@ -187,7 +188,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.walk_block_stmts(b);

if !b.stmts.is_empty() {
if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
if let Some(expr) = stmt_expr(&b.stmts[b.stmts.len() - 1]) {
if utils::semicolon_for_expr(&self.get_context(), expr) {
self.push_str(";");
}
Expand Down Expand Up @@ -703,8 +704,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
.collect();

if items.is_empty() {
self.visit_stmt(&stmts[0]);
self.walk_stmts(&stmts[1..]);
// The `if` expression at the end of the block should be formatted in a single
// line if possible.
if self.config.version() == Version::Two
&& stmts.len() == 1
&& crate::expr::stmt_is_if(&stmts[0])
&& !contains_skip(get_attrs_from_stmt(&stmts[0]))
{
let shape = self.shape();
let rewrite = self.with_context(|ctx| {
format_expr(stmt_expr(&stmts[0])?, ExprType::SubExpression, ctx, shape)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in theory applicable to any expression kind rather than to if only.
Basically, the rule is "print trailing expressions as expressions, rather than statements".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where else that would make a difference though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think that's right. The only thing to take into account is if the last expression is one of continue, return or break, then we need to add a trailing semicolon. I'll see if I can simplify this a bit more.

});
self.push_rewrite(stmts[0].span(), rewrite);
} else {
self.visit_stmt(&stmts[0]);
self.walk_stmts(&stmts[1..]);
}
} else {
self.visit_items_with_reordering(&items);
self.walk_stmts(&stmts[items.len()..]);
Expand Down
42 changes: 42 additions & 0 deletions tests/source/one_line_if_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// rustfmt-version: One

fn plain_if(x: bool) -> u8 {
if x {
0
} else {
1
}
}

fn paren_if(x: bool) -> u8 {
(if x { 0 } else { 1 })
}

fn let_if(x: bool) -> u8 {
let x = if x {
foo()
} else {
bar()
};
x
}

fn return_if(x: bool) -> u8 {
return if x {
0
} else {
1
};
}

fn multi_if() {
use std::io;
if x { foo() } else { bar() }
if x { foo() } else { bar() }
}

fn middle_if() {
use std::io;
if x { foo() } else { bar() }
let x = 1;
}
42 changes: 42 additions & 0 deletions tests/source/one_line_if_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// rustfmt-version: Two

fn plain_if(x: bool) -> u8 {
if x {
0
} else {
1
}
}

fn paren_if(x: bool) -> u8 {
(if x { 0 } else { 1 })
}

fn let_if(x: bool) -> u8 {
let x = if x {
foo()
} else {
bar()
};
x
}

fn return_if(x: bool) -> u8 {
return if x {
0
} else {
1
};
}

fn multi_if() {
use std::io;
if x { foo() } else { bar() }
if x { foo() } else { bar() }
}

fn middle_if() {
use std::io;
if x { foo() } else { bar() }
let x = 1;
}
46 changes: 46 additions & 0 deletions tests/target/one_line_if_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// rustfmt-version: One

fn plain_if(x: bool) -> u8 {
if x {
0
} else {
1
}
}

fn paren_if(x: bool) -> u8 {
(if x { 0 } else { 1 })
}

fn let_if(x: bool) -> u8 {
let x = if x { foo() } else { bar() };
x
}

fn return_if(x: bool) -> u8 {
return if x { 0 } else { 1 };
}

fn multi_if() {
use std::io;
if x {
foo()
} else {
bar()
}
if x {
foo()
} else {
bar()
}
}

fn middle_if() {
use std::io;
if x {
foo()
} else {
bar()
}
let x = 1;
}
38 changes: 38 additions & 0 deletions tests/target/one_line_if_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// rustfmt-version: Two

fn plain_if(x: bool) -> u8 {
if x { 0 } else { 1 }
}

fn paren_if(x: bool) -> u8 {
(if x { 0 } else { 1 })
}

fn let_if(x: bool) -> u8 {
let x = if x { foo() } else { bar() };
x
}

fn return_if(x: bool) -> u8 {
return if x { 0 } else { 1 };
}

fn multi_if() {
use std::io;
if x {
foo()
} else {
bar()
}
if x { foo() } else { bar() }
}

fn middle_if() {
use std::io;
if x {
foo()
} else {
bar()
}
let x = 1;
}