Fix empty body format, add fn_empty_single_line option, refactor block tests
This commit is contained in:
parent
fbd1398c92
commit
15ec5b2912
@ -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)
|
||||
}
|
||||
|
||||
!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:
|
||||
|
17
src/items.rs
17
src/items.rs
@ -448,20 +448,19 @@ 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 {
|
||||
return Some(format!("{}{{ }}", fn_str));
|
||||
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));
|
||||
}
|
||||
|
||||
if self.config.fn_single_line && is_simple_block_stmt(block, codemap) {
|
||||
@ -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) {
|
||||
self.format_missing_with_indent(s.lo);
|
||||
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);
|
||||
self.buffer.push_str(&fn_str);
|
||||
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;
|
||||
} else {
|
||||
self.buffer.push_str(&fn_str);
|
||||
}
|
||||
} else {
|
||||
self.format_missing(b.span.lo);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ impl Bar {
|
||||
/// Blah blah blooo.
|
||||
/// Blah blah blooo.
|
||||
#[an_attribute]
|
||||
fn foo(&mut self) -> isize { }
|
||||
fn foo(&mut self) -> isize {}
|
||||
|
||||
/// Blah blah bing.
|
||||
/// Blah blah bing.
|
||||
@ -27,7 +27,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
#[another_attribute]
|
||||
fn f3(self) -> Dog { }
|
||||
fn f3(self) -> Dog {}
|
||||
|
||||
/// Blah blah bing.
|
||||
#[attrib1]
|
||||
@ -36,5 +36,5 @@ impl Bar {
|
||||
// Another comment that needs rewrite because it's tooooooooooooooooooooooooooooooo
|
||||
// loooooooooooong.
|
||||
/// Blah blah bing.
|
||||
fn f4(self) -> Cat { }
|
||||
fn f4(self) -> Cat {}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ fn test() {
|
||||
}
|
||||
|
||||
/// test123
|
||||
fn doc_comment() { }
|
||||
fn doc_comment() {}
|
||||
|
||||
fn chains() {
|
||||
foo.bar(|| {
|
||||
|
@ -16,6 +16,6 @@ fn foo<F, G>(a: aaaaaaaaaaaaa, // A comment
|
||||
|
||||
}
|
||||
|
||||
fn bar<F /* comment on F */, G /* comment on G */>() { }
|
||||
fn bar<F /* comment on F */, G /* comment on G */>() {}
|
||||
|
||||
fn baz() -> Baz /* Comment after return type */ { }
|
||||
fn baz() -> Baz /* Comment after return type */ {}
|
||||
|
@ -28,13 +28,13 @@ fn generic<T>(arg: T) -> &SomeType
|
||||
arg(a, b, c, d, e)
|
||||
}
|
||||
|
||||
fn foo() -> ! { }
|
||||
fn foo() -> ! {}
|
||||
|
||||
pub fn http_fetch_async(listener: Box<AsyncCORSResponseListener + Send>,
|
||||
script_chan: Box<ScriptChan + Send>) {
|
||||
}
|
||||
|
||||
fn some_func<T: Box<Trait + Bound>>(val: T) { }
|
||||
fn some_func<T: Box<Trait + Bound>>(val: T) {}
|
||||
|
||||
fn zzzzzzzzzzzzzzzzzzzz<Type, NodeType>(selff: Type,
|
||||
mut handle: node::Handle<IdRef<'id, Node<K, V>>,
|
||||
|
@ -9,7 +9,7 @@ fn foo_decl_local() { let z = 5; }
|
||||
|
||||
fn foo_decl_item(x: &mut i32) { x = 3; }
|
||||
|
||||
fn empty() { }
|
||||
fn empty() {}
|
||||
|
||||
fn foo_return() -> String { "yay" }
|
||||
|
||||
@ -55,9 +55,9 @@ fn lots_of_space() { 1 }
|
||||
fn mac() -> Vec<i32> { vec![] }
|
||||
|
||||
trait CoolTypes {
|
||||
fn dummy(&self) { }
|
||||
fn dummy(&self) {}
|
||||
}
|
||||
|
||||
trait CoolerTypes {
|
||||
fn dummy(&self) { }
|
||||
fn dummy(&self) {}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Tests different fns
|
||||
|
||||
fn foo(a: AAAA, b: BBB, c: CCC) -> RetType { }
|
||||
fn foo(a: AAAA, b: BBB, c: CCC) -> RetType {}
|
||||
|
||||
fn foo(a: AAAA, b: BBB /* some, weird, inline comment */, c: CCC) -> RetType
|
||||
where T: Blah
|
||||
@ -32,7 +32,7 @@ fn foo<U, T>(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
|
||||
|
||||
}
|
||||
|
||||
fn foo<U: Fn(A) -> B /* paren inside generics */>() { }
|
||||
fn foo<U: Fn(A) -> B /* paren inside generics */>() {}
|
||||
|
||||
impl Foo {
|
||||
fn with_no_errors<T, F>(&mut self, f: F) -> T
|
||||
@ -40,9 +40,9 @@ impl Foo {
|
||||
{
|
||||
}
|
||||
|
||||
fn foo(mut self, mut bar: u32) { }
|
||||
fn foo(mut self, mut bar: u32) {}
|
||||
|
||||
fn bar(self, mut bazz: u32) { }
|
||||
fn bar(self, mut bazz: u32) {}
|
||||
}
|
||||
|
||||
pub fn render<'a,
|
||||
@ -70,9 +70,9 @@ impl Foo {
|
||||
}
|
||||
}
|
||||
|
||||
fn homura<T: Deref<Target = i32>>(_: T) { }
|
||||
fn homura<T: Deref<Target = i32>>(_: T) {}
|
||||
|
||||
fn issue377() -> (Box<CompositorProxy + Send>, Box<CompositorReceiver>) { }
|
||||
fn issue377() -> (Box<CompositorProxy + Send>, Box<CompositorReceiver>) {}
|
||||
|
||||
fn main() {
|
||||
let _ = function(move || 5);
|
||||
|
@ -26,7 +26,7 @@ mod other;
|
||||
// sfdgfffffffffffffffffffffffffffffffffffffffffffffffffffffff
|
||||
// ffffffffffffffffffffffffffffffffffffffffff
|
||||
|
||||
fn foo(a: isize, b: u32 /* blah blah */, c: f64) { }
|
||||
fn foo(a: isize, b: u32 /* blah blah */, c: f64) {}
|
||||
|
||||
fn foo() -> Box<Write + 'static>
|
||||
where 'a: 'b,
|
||||
@ -75,7 +75,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
#[an_attribute]
|
||||
fn f3(self) -> Dog { }
|
||||
fn f3(self) -> Dog {}
|
||||
}
|
||||
|
||||
/// The `nodes` and `edges` method each return instantiations of
|
||||
@ -115,7 +115,7 @@ pub struct Foo<'a, Y: Baz>
|
||||
f: SomeType, // Comment beside a field
|
||||
}
|
||||
|
||||
fn foo(ann: &'a (PpAnn + 'a)) { }
|
||||
fn foo(ann: &'a (PpAnn + 'a)) {}
|
||||
|
||||
fn main() {
|
||||
for i in 0i32..4 {
|
||||
|
@ -1,3 +1,3 @@
|
||||
// A standard mod
|
||||
|
||||
fn a() { }
|
||||
fn a() {}
|
||||
|
@ -1,2 +1,2 @@
|
||||
// Another mod
|
||||
fn a() { }
|
||||
fn a() {}
|
||||
|
@ -3,4 +3,4 @@
|
||||
|
||||
use c::a;
|
||||
|
||||
fn foo() { }
|
||||
fn foo() {}
|
||||
|
@ -1 +1 @@
|
||||
fn main() { }
|
||||
fn main() {}
|
||||
|
@ -19,4 +19,4 @@ fn main() {
|
||||
let x: Foo<A>;
|
||||
}
|
||||
|
||||
fn op(foo: Bar, key: &[u8], upd: Fn(Option<&memcache::Item>, Baz) -> Result) -> MapResult { }
|
||||
fn op(foo: Bar, key: &[u8], upd: Fn(Option<&memcache::Item>, Baz) -> Result) -> MapResult {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user