Skip to content
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
23 changes: 12 additions & 11 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::{Indent, Shape};
use crate::source_map::{LineRangeUtils, SpanUtils};
use crate::spanned::Spanned;
use crate::stmt;
use crate::string::{rewrite_string, StringFormat};
use crate::types::{rewrite_path, PathContext};
use crate::utils::{
Expand Down Expand Up @@ -515,9 +516,9 @@ fn rewrite_single_line_block(
label: Option<ast::Label>,
shape: Shape,
) -> Option<String> {
if is_simple_block(context, block, attrs) {
if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
let expr_shape = shape.offset_left(last_line_width(prefix))?;
let expr_str = block.stmts[0].rewrite(context, expr_shape)?;
let expr_str = block_expr.rewrite(context, expr_shape)?;
let label_str = rewrite_label(label);
let result = format!("{}{}{{ {} }}", prefix, label_str, expr_str);
if result.len() <= shape.width && !result.contains('\n') {
Expand Down Expand Up @@ -799,19 +800,19 @@ impl<'a> ControlFlow<'a> {
let fixed_cost = self.keyword.len() + " { } else { }".len();

if let ast::ExprKind::Block(ref else_node, _) = else_block.kind {
if !is_simple_block(context, self.block, None)
|| !is_simple_block(context, else_node, None)
|| pat_expr_str.contains('\n')
{
return None;
}
let (if_expr, else_expr) = match (
stmt::Stmt::from_simple_block(context, self.block, None),
stmt::Stmt::from_simple_block(context, else_node, None),
pat_expr_str.contains('\n'),
) {
(Some(if_expr), Some(else_expr), false) => (if_expr, else_expr),
_ => return None,
};

let new_width = width.checked_sub(pat_expr_str.len() + fixed_cost)?;
let expr = &self.block.stmts[0];
let if_str = expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;
let if_str = if_expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;

let new_width = new_width.checked_sub(if_str.len())?;
let else_expr = &else_node.stmts[0];
let else_str = else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;

if if_str.contains('\n') || else_str.contains('\n') {
Expand Down
34 changes: 25 additions & 9 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_span::Span;

use crate::comment::recover_comment_removed;
use crate::config::Version;
use crate::expr::{format_expr, ExprType};
use crate::expr::{format_expr, is_simple_block, ExprType};
use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::Shape;
use crate::source_map::LineRangeUtils;
Expand Down Expand Up @@ -33,6 +33,21 @@ impl<'a> Stmt<'a> {
}
}

pub(crate) fn from_simple_block(
context: &RewriteContext<'_>,
block: &'a ast::Block,
attrs: Option<&[ast::Attribute]>,
) -> Option<Self> {
if is_simple_block(context, block, attrs) {
let inner = &block.stmts[0];
// Simple blocks only contain one expr and no stmts
let is_last = true;
Some(Stmt { inner, is_last })
} else {
None
}
}

pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_last: bool) -> Self {
Stmt { inner, is_last }
}
Expand Down Expand Up @@ -80,13 +95,13 @@ impl<'a> Rewrite for Stmt<'a> {
} else {
ExprType::Statement
};
format_stmt(context, shape, self.as_ast_node(), expr_type)
}
}

impl Rewrite for ast::Stmt {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
format_stmt(context, shape, self, ExprType::Statement)
format_stmt(
context,
shape,
self.as_ast_node(),
expr_type,
self.is_last_expr(),
)
}
}

Expand All @@ -95,13 +110,14 @@ fn format_stmt(
shape: Shape,
stmt: &ast::Stmt,
expr_type: ExprType,
is_last_expr: bool,
) -> Option<String> {
skip_out_of_file_lines_range!(context, stmt.span());

let result = match stmt.kind {
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
let suffix = if semicolon_for_stmt(context, stmt) {
let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) {
";"
} else {
""
Expand Down
10 changes: 8 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,20 @@ pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr)
}

#[inline]
pub(crate) fn semicolon_for_stmt(context: &RewriteContext<'_>, stmt: &ast::Stmt) -> bool {
pub(crate) fn semicolon_for_stmt(
context: &RewriteContext<'_>,
stmt: &ast::Stmt,
is_last_expr: bool,
) -> bool {
match stmt.kind {
ast::StmtKind::Semi(ref expr) => match expr.kind {
ast::ExprKind::While(..) | ast::ExprKind::Loop(..) | ast::ExprKind::ForLoop(..) => {
false
}
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
context.config.trailing_semicolon()
// The only time we can skip the semi-colon is if the config option is set to false
// **and** this is the last expr (even though any following exprs are unreachable)
context.config.trailing_semicolon() || !is_last_expr
}
_ => true,
},
Expand Down
7 changes: 7 additions & 0 deletions tests/target/issue-5797/retain_trailing_semicolon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-trailing_semicolon: false

fn foo() {}
fn main() {
return;
foo()
}