Skip to content

Commit 8882929

Browse files
authored
Merge pull request #223 from Muscraft/buffer-replacements
fix: Don't attempt out of bounds buffer replacments
2 parents 07ee0ac + 22e1e54 commit 8882929

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/renderer/styled_buffer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ impl StyledBuffer {
103103
if start == end {
104104
return;
105105
}
106+
// If the replacement range would be out of bounds, do nothing, as we
107+
// can't replace things that don't exist.
108+
if start > self.lines[line].len() || end > self.lines[line].len() {
109+
return;
110+
}
106111
let _ = self.lines[line].drain(start..(end - string.chars().count()));
107112
for (i, c) in string.chars().enumerate() {
108113
self.lines[line][start + i] = StyledChar::new(c, ElementStyle::LineNumber);

tests/rustc_tests.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,3 +2889,78 @@ $DIR/short-error-format.rs:8:7: error[E0599]: no method named `salut` found for
28892889
.anonymized_line_numbers(true);
28902890
assert_data_eq!(renderer.render(input), expected);
28912891
}
2892+
2893+
#[test]
2894+
fn rustdoc_ui_diagnostic_width() {
2895+
// tests/rustdoc-ui/diagnostic-width.rs
2896+
2897+
let source_0 = r#"//@ compile-flags: --diagnostic-width=10
2898+
#![deny(rustdoc::bare_urls)]
2899+
2900+
/// This is a long line that contains a http://link.com
2901+
pub struct Foo; //~^ ERROR
2902+
"#;
2903+
let source_1 = r#"/// This is a long line that contains a http://link.com
2904+
"#;
2905+
2906+
let input = Level::ERROR
2907+
.header("this URL is not a hyperlink")
2908+
.group(
2909+
Group::new()
2910+
.element(
2911+
Snippet::source(source_0)
2912+
.origin("$DIR/diagnostic-width.rs")
2913+
.fold(true)
2914+
.annotation(AnnotationKind::Primary.span(111..126)),
2915+
)
2916+
.element(
2917+
Level::NOTE
2918+
.title("bare URLs are not automatically turned into clickable links"),
2919+
),
2920+
)
2921+
.group(
2922+
Group::new()
2923+
.element(Level::NOTE.title("the lint level is defined here"))
2924+
.element(
2925+
Snippet::source(source_0)
2926+
.origin("$DIR/diagnostic-width.rs")
2927+
.fold(true)
2928+
.annotation(AnnotationKind::Primary.span(49..67)),
2929+
),
2930+
)
2931+
.group(
2932+
Group::new()
2933+
.element(Level::HELP.title("use an automatic link instead"))
2934+
.element(
2935+
Snippet::source(source_1)
2936+
.origin("$DIR/diagnostic-width.rs")
2937+
.line_start(4)
2938+
.fold(true)
2939+
.patch(Patch::new(40..40, "<"))
2940+
.patch(Patch::new(55..55, ">")),
2941+
),
2942+
);
2943+
2944+
let expected = str![[r#"
2945+
error: this URL is not a hyperlink
2946+
--> $DIR/diagnostic-width.rs:4:41
2947+
|
2948+
LL | ... a http://link.com
2949+
| ^^^^^^^^^^^^^^^
2950+
|
2951+
= note: bare URLs are not automatically turned into clickable links
2952+
note: the lint level is defined here
2953+
--> $DIR/diagnostic-width.rs:2:9
2954+
|
2955+
LL | ...ny(ru...are_urls)]
2956+
| ^^...^^^^^^^^
2957+
help: use an automatic link instead
2958+
|
2959+
LL | /// This is a long line that contains a <http://link.com>
2960+
| + +
2961+
"#]];
2962+
let renderer = Renderer::plain()
2963+
.anonymized_line_numbers(true)
2964+
.term_width(10);
2965+
assert_data_eq!(renderer.render(input), expected);
2966+
}

0 commit comments

Comments
 (0)