Skip to content
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

rustfmt inconsistently formats blank lines in the beginning of blocks #5361

Open
wafrelka opened this issue Jun 1, 2022 · 2 comments
Open

Comments

@wafrelka
Copy link

wafrelka commented Jun 1, 2022

Description

The current implementation exhibits inconsistent behavior for blank lines in the beginning of blocks depending on the type of the block.

The formatter removes all blank lines in the beginning of enum, union, struct, impl and trait.
However, blank lines in fns are trimmed and inserted to match the number of blank lines to blank_lines_lower_bound.
In mod and extern, blank lines are clamped according to blank_lines_lower_bound and blank_lines_upper_bound.

This mismatch appears very strange with custom blank_lines_*_bound values, but even with default values it seems weird.

From the document on blank_lines_lower_bound in Configurations.md, the number of blank lines should be decided according to the blank_lines_*_bound options.
This matches to the current behavior for mod and extern.

On the other hand, some discussions in #2954 and #3382 concluded those options should not affect on it.
This matches to the current behavior for enum, union, struct, impl and trait.
#4295 tried to fix the v2 code, but it is not yet back-ported and still has some bugs like one reported in #5067.

Environment

version: rustfmt 1.4.38-nightly (3e38399e 2022-05-26)

Sample

Source

enum Enum {



    Variant(i64),
}

union Union {



    value: i64,
}

struct Struct {



    value: i64,
}

impl Struct {



    fn struct_func() {}
}

trait Trait {



    fn trait_func();
}

fn toplevel_func() {



    println!("");
}

mod Mod {



    fn mod_func() {}
}

extern "C" {



    fn extern_func();
}

Expected

enum Enum {
    Variant(i64),
}

union Union {
    value: i64,
}

struct Struct {
    value: i64,
}

impl Struct {
    fn struct_func() {}
}

trait Trait {
    fn trait_func();
}

fn toplevel_func() {
    println!("");
}

mod Mod {
    fn mod_func() {}
}

extern "C" {
    fn extern_func();
}

Actual

enum Enum {
    Variant(i64),
}

union Union {
    value: i64,
}

struct Struct {
    value: i64,
}

impl Struct {
    fn struct_func() {}
}

trait Trait {
    fn trait_func();
}

fn toplevel_func() {
    println!("");
}

mod Mod {

    fn mod_func() {}
}

extern "C" {

    fn extern_func();
}

Actual (with blank_lines_lower_bound=1,blank_lines_upper_bound=2)

enum Enum {
    Variant(i64),
}

union Union {
    value: i64,
}

struct Struct {
    value: i64,
}

impl Struct {
    fn struct_func() {}
}

trait Trait {
    fn trait_func();
}

fn toplevel_func() {

    println!("");
}

mod Mod {


    fn mod_func() {}
}

extern "C" {


    fn extern_func();

}

The extern block has an extra blank line in the end of the block, but this is a separate issue?

@wafrelka
Copy link
Author

wafrelka commented Jun 1, 2022

I noticed this while re-implementing #4303 into the current master branch.
It makes re-implementation trickier than I initially thought...

@calebcartwright
Copy link
Member

Thank you for looking at this @wafrelka. Truth be told, I suspect this may require a number of backports (properly sequenced) or perhaps even a re-imagining in the 1.x branch context. My recollection is that the relevant PRs built on some significant code changes that we actually can't (or don't want) to land on the mainline, so the latter may end up being required.

Given the age of both implementation and preceding discussion, it may even be worth trying to figure out what people want, as well as whether the style guide has (or should have) any prescriptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants