Fix empty body format, add fn_empty_single_line option, refactor block tests
This commit is contained in:
parent
fbd1398c92
commit
15ec5b2912
16 changed files with 50 additions and 62 deletions
|
@ -269,6 +269,7 @@ create_config! {
|
|||
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
|
||||
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
|
||||
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
|
||||
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
|
||||
fn_single_line: bool, false, "Put single-expression functions on a single line";
|
||||
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
|
||||
"Location of return type in function declaration";
|
||||
|
|
29
src/expr.rs
29
src/expr.rs
|
@ -705,36 +705,25 @@ fn single_line_if_else(context: &RewriteContext,
|
|||
None
|
||||
}
|
||||
|
||||
// Checks that a block contains no statements, an expression and no comments.
|
||||
fn is_simple_block(block: &ast::Block, codemap: &CodeMap) -> bool {
|
||||
if !block.stmts.is_empty() || block.expr.is_none() {
|
||||
return false;
|
||||
fn block_contains_comment(block: &ast::Block, codemap: &CodeMap) -> bool {
|
||||
let snippet = codemap.span_to_snippet(block.span).unwrap();
|
||||
contains_comment(&snippet)
|
||||
}
|
||||
|
||||
let snippet = codemap.span_to_snippet(block.span).unwrap();
|
||||
|
||||
!contains_comment(&snippet)
|
||||
// Checks that a block contains no statements, an expression and no comments.
|
||||
pub fn is_simple_block(block: &ast::Block, codemap: &CodeMap) -> bool {
|
||||
block.stmts.is_empty() && block.expr.is_some() && !block_contains_comment(block, codemap)
|
||||
}
|
||||
|
||||
/// Checks whether a block contains at most one statement or expression, and no comments.
|
||||
pub fn is_simple_block_stmt(block: &ast::Block, codemap: &CodeMap) -> bool {
|
||||
if (!block.stmts.is_empty() && block.expr.is_some()) ||
|
||||
(block.stmts.len() != 1 && block.expr.is_none()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let snippet = codemap.span_to_snippet(block.span).unwrap();
|
||||
!contains_comment(&snippet)
|
||||
(block.stmts.is_empty() || (block.stmts.len() == 1 && block.expr.is_none())) &&
|
||||
!block_contains_comment(block, codemap)
|
||||
}
|
||||
|
||||
/// Checks whether a block contains no statements, expressions, or comments.
|
||||
pub fn is_empty_block(block: &ast::Block, codemap: &CodeMap) -> bool {
|
||||
if !block.stmts.is_empty() || block.expr.is_some() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let snippet = codemap.span_to_snippet(block.span).unwrap();
|
||||
!contains_comment(&snippet)
|
||||
block.stmts.is_empty() && block.expr.is_none() && !block_contains_comment(block, codemap)
|
||||
}
|
||||
|
||||
// inter-match-arm-comment-rules:
|
||||
|
|
15
src/items.rs
15
src/items.rs
|
@ -448,19 +448,18 @@ impl<'a> FmtVisitor<'a> {
|
|||
}
|
||||
|
||||
pub fn rewrite_single_line_fn(&self,
|
||||
fn_rewrite: &Option<String>,
|
||||
fn_str: &str,
|
||||
block: &ast::Block)
|
||||
-> Option<String> {
|
||||
|
||||
let fn_str = match *fn_rewrite {
|
||||
Some(ref s) if !s.contains('\n') => s,
|
||||
_ => return None,
|
||||
};
|
||||
if fn_str.contains('\n') {
|
||||
return None;
|
||||
}
|
||||
|
||||
let codemap = self.get_context().codemap;
|
||||
|
||||
if is_empty_block(block, codemap) &&
|
||||
self.block_indent.width() + fn_str.len() + 3 <= self.config.max_width {
|
||||
if self.config.fn_empty_single_line && is_empty_block(block, codemap) &&
|
||||
self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width {
|
||||
return Some(format!("{}{{}}", fn_str));
|
||||
}
|
||||
|
||||
|
@ -488,7 +487,7 @@ impl<'a> FmtVisitor<'a> {
|
|||
};
|
||||
|
||||
if let Some(res) = rewrite {
|
||||
let width = self.block_indent.width() + fn_str.len() + res.len() + 3;
|
||||
let width = self.block_indent.width() + fn_str.len() + res.len() + 4;
|
||||
if !res.contains('\n') && width <= self.config.max_width {
|
||||
return Some(format!("{}{{ {} }}", fn_str, res));
|
||||
}
|
||||
|
|
|
@ -154,16 +154,15 @@ impl<'a> FmtVisitor<'a> {
|
|||
visit::FnKind::Closure => None,
|
||||
};
|
||||
|
||||
if let Some(ref single_line_fn) = self.rewrite_single_line_fn(&rewrite, &b) {
|
||||
if let Some(fn_str) = rewrite {
|
||||
self.format_missing_with_indent(s.lo);
|
||||
if let Some(ref single_line_fn) = self.rewrite_single_line_fn(&fn_str, &b) {
|
||||
self.buffer.push_str(single_line_fn);
|
||||
self.last_pos = b.span.hi;
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(fn_str) = rewrite {
|
||||
self.format_missing_with_indent(s.lo);
|
||||
} else {
|
||||
self.buffer.push_str(&fn_str);
|
||||
}
|
||||
} else {
|
||||
self.format_missing(b.span.lo);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue