rust-lang / rust-lang/rustfmt

Multiline comment causes `error[internal]: left behind trailing whitespace`

Open
#4,824 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-2024-style-edition C-bug I-trailing-whitespace
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

This input file:

#[cfg(windows)]
fn main() {
	use common_util::get_version_for_rc;
	use std::env;
	use winres::{VersionInfo, WindowsResource};

	println!("cargo:rerun-if-changed=favicon.ico");
	println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
	/*let profile = env::var("PROFILE").unwrap();
	if profile == "release"*/ {
		let mut res = WindowsResource::new();
		res.set_icon("favicon.ico");
		let version = get_version_for_rc!();
		res.set_version_info(VersionInfo::PRODUCTVERSION, version);
		res.set_version_info(VersionInfo::FILEVERSION, version);
		res.compile().unwrap();
	}
}

#[cfg(not(windows))]
fn main() {}

causes this error:

error[internal]: left behind trailing whitespace
  --> \\?\D:\projects\myproject\build.rs:9:9:0
   |
 9 |
   | ^
   |

warning: rustfmt has failed to format. See previous 1 errors.

and this formatted file:

#[cfg(windows)]
fn main() {
	use common_util::get_version_for_rc;
	use std::env;
	use winres::{VersionInfo, WindowsResource};

	println!("cargo:rerun-if-changed=favicon.ico");
	println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
	
	/*let profile = env::var("PROFILE").unwrap();
	if profile == "release"*/
	{
		let mut res = WindowsResource::new();
		res.set_icon("favicon.ico");
		let version = get_version_for_rc!();
		res.set_version_info(VersionInfo::PRODUCTVERSION, version);
		res.set_version_info(VersionInfo::FILEVERSION, version);
		res.compile().unwrap();
	}
}

#[cfg(not(windows))]
fn main() {}

(I'm invoking it with cargo +nightly fmt.)
Note that there was a new line inserted before the multiline comment, but it's not empty, it contains a TAB character (I'm using tabs for indentation).
This is the trailing whitespace that the error is referring to.

My config:

edition = "2018"
version = "Two"
max_width = 110
newline_style = "Unix"
use_small_heuristics = "Max"
hard_tabs = true
merge_imports = true
reorder_impl_items = true
use_field_init_shorthand = true
use_try_shorthand = true
spaces_around_ranges = true
overflow_delimited_expr = true

rustfmt version:

> cargo +nightly fmt --version
rustfmt 1.4.37-nightly (0bd2b192 2021-04-03)

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

Reproduce the report with the shown build.rs input, configuration, and cargo +nightly fmt. Start by tracing rustfmt's handling of the tab-only line before the multiline comment. Done means formatting completes without the internal trailing-whitespace error and does not leave the inserted indentation as trailing whitespace.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.