diff --git a/src/formatting.rs b/src/formatting.rs index 60361505a3f..6ba56ff72da 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -359,15 +359,16 @@ impl FormattingError { | ErrorKind::DeprecatedAttr | ErrorKind::BadAttr | ErrorKind::LostComment => { - let trailing_ws_start = self - .line_buffer - .rfind(|c: char| !c.is_whitespace()) - .map(|pos| pos + 1) - .unwrap_or(0); - ( - trailing_ws_start, - self.line_buffer.len() - trailing_ws_start, - ) + let mut last_non_whitespace = 0; + let mut end = 0; + // annotate-snippet uses char-length, this code needs to as well + for (ind, ch) in self.line_buffer.chars().enumerate() { + if !ch.is_whitespace() { + last_non_whitespace = ind + 1; + } + end = ind + 1; + } + (last_non_whitespace, end - last_non_whitespace) } _ => unreachable!(), } diff --git a/tests/issue-6083/issue_6083.rs b/tests/issue-6083/issue_6083.rs new file mode 100644 index 00000000000..ff6a92175b0 --- /dev/null +++ b/tests/issue-6083/issue_6083.rs @@ -0,0 +1,6 @@ +// rustfmt-error_on_unformatted: false +fn dummy() { + let my_var = 5 /* öäåöäåöäåöäåöäåöå */ + / m as f64 + + 0f64; +} diff --git a/tests/rustfmt/main.rs b/tests/rustfmt/main.rs index 11fb4786e82..8e464dcf88b 100644 --- a/tests/rustfmt/main.rs +++ b/tests/rustfmt/main.rs @@ -176,7 +176,7 @@ fn rustfmt_emits_error_on_line_overflow_true() { #[test] #[allow(non_snake_case)] fn dont_emit_ICE() { - let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs", "tests/target/issue_6069.rs"]; + let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs", "tests/target/issue_6069.rs", "tests/issue-6083/issue_6083.rs"]; for file in files { let args = [file];