Skip to content

Commit e47728b

Browse files
committed
Don't duplicate line numbers when annotating internal errors
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 ```
1 parent 2174e60 commit e47728b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/format_report_formatter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'a> Display for FormatReportFormatter<'a> {
7878
None
7979
};
8080

81-
let origin = format!("{}:{}", file, error.line);
81+
let origin = file.to_string();
8282
let slice = Slice {
8383
source: &error.line_buffer.clone(),
8484
line_start: error.line,

src/formatting.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,32 @@ where
645645
f();
646646
}
647647
}
648+
649+
#[cfg(test)]
650+
mod test {
651+
use std::path::PathBuf;
652+
653+
use crate::FormatReportFormatterBuilder;
654+
655+
use super::*;
656+
657+
#[test]
658+
fn test_internal_error_file_and_line_number_annotations() {
659+
let mut config = Config::default();
660+
config.set().error_on_unformatted(true);
661+
662+
let mut text = String::new();
663+
text.push_str("fn main() {\n");
664+
text.push_str(" println!(\"hello world!\"); \n"); // Note the space before the '\n'
665+
text.push_str("}");
666+
667+
let report = FormatReport::new();
668+
let name = FileName::Real(PathBuf::from("internal_error_test.rs"));
669+
670+
format_lines(&mut text, &name, &vec![], &config, &report);
671+
let output = FormatReportFormatterBuilder::new(&report)
672+
.build()
673+
.to_string();
674+
assert!(output.contains("--> internal_error_test.rs:2:30"));
675+
}
676+
}

0 commit comments

Comments
 (0)