rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.03k stars 889 forks source link

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

Open Boscop opened 3 years ago

Boscop commented 3 years ago

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)
ytmimi commented 2 years ago

confirming this is still an issue with rustfmt 1.5.1-nightly (5c41baf8 2022-07-14) and I don't need any configuration options besides version=Two to trigger it.

Linking tracking issue for version #3383