Rustfmt: Is blank_lines_lower_bound correct?

Created on 24 Aug 2018  路  3Comments  路  Source: rust-lang/rustfmt

The blank_lines_lower_bound documentation says:

Minimum number of blank lines which must be put between items. If two items have fewer blank lines between them, additional blank lines are inserted.

Then the example shows that by setting it to 1:

fn foo() {
    println!("a");
}
fn bar() {
    println!("b");
    println!("c");
}

is formatted into

fn foo() {

    println!("a");
}
fn bar() {

    println!("b");

    println!("c");
}

which is how rustfmt behaves. However, I am not sure this is correct because aren't those println! are statements? Not all statements are items, e.g. let-decl are not items IIUC (and for let this option produces the same behavior). I think it would be much more useful to output:

fn foo() {
    println!("a");
}

fn bar() {
    println!("b");
    println!("c");
}

That is, it would be much more useful for this to control the spacing between items that are not statements. I don't know how useful it would be to also be able to control the spacing between statements (independently of whether they are items or not), but if someone needs that it could be added as a different option.

bug only-with-option

Most helpful comment

I agree with the issue poster. The current functionality does not seem very useful. What I would like it to do is to separate type declarations, impl blocks, functions etc from each other. But not inserting a newline between two use or const or static. Basically keeping "blocks" of the same thing together. Personally I think it would be most helpful if it had this effect:

use std::io;
use std::net;
/// A module doing foo
mod foomodule;
#[cfg(windows)]
pub const CONST_NUMBER: u8 = 3;
pub static STATIC_NUMBER: u8 = 5;
fn function() {}
struct MyStruct;
impl MyStruct {}

Into:

use std::io;
use std::net;

/// A module doing foo
mod foomodule;

#[cfg(windows)]
pub const CONST_NUMBER: u8 = 3;
pub static STATIC_NUMBER: u8 = 5;

fn function() {}

struct MyStruct;

impl MyStruct {}

All 3 comments

The option appears to be completely broken, as in, it just inserts N blank lines between each line that contains source code, although not always:

#[cfg(test)]
mod tests { }

becomes

#[cfg(test)]

mod tests {}

and

for i in 0..10 {
    let x = i;
    let y = i;
}

becomes

for i in 0..10 {
    let x = i;

    let y = i;
}

Expected behaviour is that it should newlines between module-scoped items (even if they appear inside functions), but not between statements/expressions.

I agree with the issue poster. The current functionality does not seem very useful. What I would like it to do is to separate type declarations, impl blocks, functions etc from each other. But not inserting a newline between two use or const or static. Basically keeping "blocks" of the same thing together. Personally I think it would be most helpful if it had this effect:

use std::io;
use std::net;
/// A module doing foo
mod foomodule;
#[cfg(windows)]
pub const CONST_NUMBER: u8 = 3;
pub static STATIC_NUMBER: u8 = 5;
fn function() {}
struct MyStruct;
impl MyStruct {}

Into:

use std::io;
use std::net;

/// A module doing foo
mod foomodule;

#[cfg(windows)]
pub const CONST_NUMBER: u8 = 3;
pub static STATIC_NUMBER: u8 = 5;

fn function() {}

struct MyStruct;

impl MyStruct {}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonhoo picture jonhoo  路  4Comments

fulara picture fulara  路  4Comments

cramertj picture cramertj  路  4Comments

0x7CFE picture 0x7CFE  路  5Comments

Julian-Wollersberger picture Julian-Wollersberger  路  5Comments