diff --git a/src/test/parser.rs b/src/test/parser.rs index d951c8469e6..a4ce27708a4 100644 --- a/src/test/parser.rs +++ b/src/test/parser.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use super::read_config; use crate::modules::{ModuleResolutionError, ModuleResolutionErrorKind}; +use crate::FormatReportFormatterBuilder; use crate::{ErrorKind, Input, Session}; #[test] @@ -69,3 +70,21 @@ fn crate_parsing_stashed_diag2() { let filename = "tests/parser/stashed-diag2.rs"; assert_parser_error(filename); } + +#[test] +fn indexing_mismatch_with_annotated_snippet() { + // See also https://github.com/rust-lang/rustfmt/issues/4968 + let filename = "tests/parser/issue_4968.rs"; + let file = PathBuf::from(filename); + let config = read_config(&file); + let mut session = Session::::new(config, None); + let report = session.format(Input::File(filename.into())).unwrap(); + let report = FormatReportFormatterBuilder::new(&report).build(); + { + // Panic can only be triggered if we actually try to write the report + // and call into the annotated_snippet dependency. + use std::io::Write; + + write!(&mut Vec::new(), "{report}").unwrap(); + } +} diff --git a/tests/parser/issue_4968.rs b/tests/parser/issue_4968.rs new file mode 100644 index 00000000000..eb0c65756ef --- /dev/null +++ b/tests/parser/issue_4968.rs @@ -0,0 +1,12 @@ +// rustfmt-hard_tabs: true +// rustfmt-max_width: 40 +// rustfmt-error_on_unformatted: true +// rustfmt-error_on_line_overflow: true + +fn foo(x: u32) { + if x > 10 { + if x > 20 { + println!("0123456789abcdefghijklmnopqrstuvwxyz"); + } + } +}