rust-lang / rust-lang/rustfmt

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

Open
#6,699 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments C-bug I-trailing-whitespace S-has-mcve
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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,
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named; begin by running rustfmt on the supplied reproducer and comparing the comment, attribute, brace, and whitespace cases. Trace the formatter path handling a single-line comment between an attribute and struct field, and consider the issue resolved when the expected formatting is produced without an internal error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.