Skip to content

Failure when a single line comment is placed between an attribute and a struct field #6699

@luisfonsivevo

Description

@luisfonsivevo

Notice the mixed tabs and spaces, mixed brace styling, internal error message.

Input:

pub fn create_thing() -> Thing
{
	Thing
	{
		#[attribute]
		//this comment's presence breaks rustfmt
		
		field: 1,
	}
}

Output (rustfmt fmt_bug.rs):

error[internal]: left behind trailing whitespace
 --> fmt_bug.rs:6:6:1
  |
6 | 		
  | 
  |

warning: rustfmt has failed to format. See previous 1 errors.
pub fn create_thing() -> Thing {
    Thing
	{
		#[attribute]
		//this comment's presence breaks rustfmt
		
		field: 1,
	}
}

Expected:

pub fn create_thing() -> Thing {
    Thing {
        #[attribute]
        //this comment's presence breaks rustfmt
        field: 1,
    }
}

Removing the empty line with whitespace fixes the error message, but the output is still malformed in the same way:
Input:

pub fn create_thing() -> Thing
{
	Thing
	{
		#[attribute]
		//this comment's presence breaks rustfmt
		field: 1,
	}
}

Output:

pub fn create_thing() -> Thing {
    Thing
	{
		#[attribute]
		//this comment's presence breaks rustfmt
		field: 1,
	}
}

With the comment removed, braces are correctly relocated and the empty line is collapsed.
Input:

pub fn create_thing() -> Thing
{
	Thing
	{
		#[attribute]
		
		field: 1,
	}
}

Output:

pub fn create_thing() -> Thing {
    Thing {
        #[attribute]
        field: 1,
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEI-trailing-whitespaceIssue: internal error; left behind trailing whitespaceS-has-mcveStatus: a Minimal Complete and Verifiable Example has been found for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions