[ty] Fix panic on malformed mdtest assertion (#22724)

This commit is contained in:
Hugo
2026-01-19 16:56:42 +01:00
committed by GitHub
parent c5902a3cdb
commit 8fab35019b
2 changed files with 33 additions and 3 deletions

View File

@@ -418,10 +418,14 @@ impl<'a> ErrorAssertionParser<'a> {
// message text
'"' => {
let comment_source = self.comment_source.trim();
let comment_source = self.comment_source.trim_end();
return if comment_source.ends_with('"') {
let rest = &comment_source
[self.cursor.offset().to_usize()..comment_source.len() - 1];
let start = self.cursor.offset().to_usize();
let end = comment_source.len() - 1;
if start > end {
return Err(ErrorAssertionParseError::DanglingMessageQuote);
}
let rest = &comment_source[start..end];
Ok(ErrorAssertion {
rule,
column,
@@ -489,6 +493,8 @@ pub(crate) enum ErrorAssertionParseError<'a> {
MultipleRuleCodes,
#[error("expected '\"' to be the final character in an assertion with an error message")]
UnclosedMessage,
#[error("expected message text and closing '\"' after opening '\"'")]
DanglingMessageQuote,
#[error(
"unexpected character `{character}` at offset {offset} (relative to the `:` in the assertion comment)"
)]

View File

@@ -1395,4 +1395,28 @@ mod tests {
)],
);
}
#[test]
fn trailing_quote_without_message_not_allowed() {
let source = "x # error: [some-rule]\"";
let result = get_result(
source,
vec![ExpectedDiagnostic::new(
DiagnosticId::lint("some-rule"),
"some message",
0,
)],
);
assert_fail(
result,
&[(
0,
&[
"invalid assertion: expected message text and closing '\"' after opening '\"'",
r#"unexpected error: 1 [some-rule] "some message""#,
],
)],
);
}
}