Skip to content

Commit

Permalink
Don't duplicate line numbers when annotating internal errors
Browse files Browse the repository at this point in the history
Closes 5463

The `annotate_snippets` crate adds line number and column info to the
output, so we don't need to append the line number to the `origin`.

Previously, internal errors would be output with the line number twice.
See how 478 is duplicated in the example below.

```
error[internal]: line formatted, but exceeded maximum width
   --> /path/to/file.rs:478:478:101
```

Now the line number is not duplicated.

```
error[internal]: line formatted, but exceeded maximum width
   --> /path/to/file.rs:478:101
```
  • Loading branch information
ytmimi committed Dec 9, 2023
1 parent 2174e60 commit e47728b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/format_report_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> Display for FormatReportFormatter<'a> {
None
};

let origin = format!("{}:{}", file, error.line);
let origin = file.to_string();
let slice = Slice {
source: &error.line_buffer.clone(),
line_start: error.line,
Expand Down
29 changes: 29 additions & 0 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,32 @@ where
f();
}
}

#[cfg(test)]
mod test {
use std::path::PathBuf;

use crate::FormatReportFormatterBuilder;

use super::*;

#[test]
fn test_internal_error_file_and_line_number_annotations() {
let mut config = Config::default();
config.set().error_on_unformatted(true);

let mut text = String::new();
text.push_str("fn main() {\n");
text.push_str(" println!(\"hello world!\"); \n"); // Note the space before the '\n'
text.push_str("}");

let report = FormatReport::new();
let name = FileName::Real(PathBuf::from("internal_error_test.rs"));

format_lines(&mut text, &name, &vec![], &config, &report);
let output = FormatReportFormatterBuilder::new(&report)
.build()
.to_string();
assert!(output.contains("--> internal_error_test.rs:2:30"));
}
}

0 comments on commit e47728b

Please sign in to comment.