rustdoc: clean up some unneeded table lint code

The essential problem is that, with this table:

```text
one |
----|
  a | b | c
  a | b |
  a | b
  a |
```

And this logic:

```rust
let too_many_pipes = divider_count > expected_cells + 1;
```

`expected_cells + 1` winds up as 2, so you get this warning:

```text
error: unused content after last table cell
  --> $DIR/invalid_markdown_table.rs:81:14
   |
LL |     //!   a | b | c
   |              ^^^^^^ this content is discarded

error: unused content after last table cell
  --> $DIR/invalid_markdown_table.rs:83:14
   |
LL |     //!   a | b |
   |              ^^^^ this content is discarded

error: unused content after last table cell
  --> $DIR/invalid_markdown_table.rs:85:14
   |
LL |     //!   a | b
   |              ^^ this content is discarded
```

We really want our warning to give the suggest-escaping flow, like this:

```text
error: table row has too many columns
  --> $DIR/invalid_markdown_table.rs:81:13
   |
LL |     //!   a | b | c
   |             ^ any content after this column divider is discarded
   |
   = help: to escape `|` characters in tables, add a `\` before them like `\|`

error: table row has too many columns
  --> $DIR/invalid_markdown_table.rs:83:13
   |
LL |     //!   a | b |
   |             ^ any content after this column divider is discarded
   |
   = help: to escape `|` characters in tables, add a `\` before them like `\|`

error: unused content after last table cell
  --> $DIR/invalid_markdown_table.rs:85:14
   |
LL |     //!   a | b
   |              ^^ this content is discarded
```

By only scanning the text between the end of the last cell and the row,
instead of doing the entire row, we don't have to re-implement as much
of pulldown-cmark's logic.
This commit is contained in:
Michael Howell
2026-07-24 14:06:31 -07:00
committed by Guillaume Gomez
parent 67bfe3ad49
commit df734af6ae
3 changed files with 120 additions and 25 deletions

View File

@@ -33,17 +33,7 @@ pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &
let mut p = Parser::new_ext(dox, main_body_opts()).into_offset_iter();
while let Some((event, _range)) = p.next() {
if let Event::Start(Tag::Table(_)) = event
&& let Some((Event::Start(Tag::TableHead), _)) = p.next()
{
let mut expected_cells = 0;
while let Some((event, _)) = p.next() {
match event {
Event::End(TagEnd::TableCell) => expected_cells += 1,
Event::End(TagEnd::TableHead) => break,
_ => {}
}
}
if Event::Start(Tag::TableRow) == event {
let mut prev_range = None;
while let Some((event, range)) = p.next() {
match event {
@@ -67,21 +57,20 @@ pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &
// Seems all good so let's ignore it and continue;.
continue;
}
// We now check the number of unescaped `|`.
let row = &dox[range.clone()];
let mut iter = row.chars();
let mut divider_count = 0;
// Check if any pipes appear after the end of the row.
let mut iter = dox[after_last_cell_range.clone()].bytes().peekable();
let mut found_divider = false;
while let Some(c) = iter.next() {
if c == '\\' {
// the sequence `\\|` still escapes the pipe because GFM
// processes block structures like tables in its own pass
if c == b'\\' && iter.peek() == Some(&b'|') {
iter.next();
} else if c == '|' {
divider_count += 1;
} else if c == b'|' {
found_divider = true;
break;
}
}
// + 1 is to handle the `|` at the end of the table row.
let too_many_pipes = divider_count > expected_cells + 1;
if too_many_pipes {
if found_divider {
// Seems like a pipe was not escaped as it should have been.
let last_cell_separator =
Range { start: prev_range.end, end: prev_range.end + 1 };

View File

@@ -27,6 +27,15 @@ mod c {
//! | col |
//! | ---- |
//! | a \| still same cell |
//!
//! now with double-backslashes;
//! yes, it really does work this way,
//! but it's *weird* compared to what
//! you would naively expect
//!
//! | col |
//! | ---- |
//! | a \\| still same cell |
}
// We check that content after a table row also emits.
@@ -37,6 +46,18 @@ mod d {
//~^ ERROR invalid_markdown_table
//! blob
//!
//! | col |
//! | ---- |
//! | code_with | \|
//~^ ERROR invalid_markdown_table
//! blob
//!
//! | col |
//! | ---- |
//! | code_with | \\|
//~^ ERROR invalid_markdown_table
//! blob
//!
//! one | two
//! -|-
//! a | b | c
@@ -51,3 +72,32 @@ mod e {
//! | a |
//! | b
}
// Weird corner case where the table ends with a pipe,
// but doesn't start with it
mod f {
//! one |
//! ----|
//! a | b | c
//~^ ERROR invalid_markdown_table
//! a | b |
//~^ ERROR invalid_markdown_table
//! a | b
//~^ ERROR invalid_markdown_table
//! a |
}
// Weird corner case where the table ends with a pipe,
// but doesn't start with it
mod g {
//! | one
//! |----
//! | a | b | c
//~^ ERROR invalid_markdown_table
//! | a | b |
//~^ ERROR invalid_markdown_table
//! | a | b
//~^ ERROR invalid_markdown_table
//! | a |
//! | a
}

View File

@@ -28,16 +28,72 @@ LL | //! | `code_with(|arg| arg)` |
= help: to escape `|` characters in tables, add a `\` before them like `\|`
error: unused content after last table cell
--> $DIR/invalid_markdown_table.rs:36:22
--> $DIR/invalid_markdown_table.rs:45:22
|
LL | //! | code_with | aaaaa
| ^^^^^^ this content is discarded
error: unused content after last table cell
--> $DIR/invalid_markdown_table.rs:42:16
--> $DIR/invalid_markdown_table.rs:51:22
|
LL | //! | code_with | \|
| ^^^ this content is discarded
error: unused content after last table cell
--> $DIR/invalid_markdown_table.rs:57:22
|
LL | //! | code_with | \|
| ^^^^ this content is discarded
error: unused content after last table cell
--> $DIR/invalid_markdown_table.rs:63:16
|
LL | //! a | b | c
| ^^ this content is discarded
error: aborting due to 5 previous errors
error: table row has too many columns
--> $DIR/invalid_markdown_table.rs:81:13
|
LL | //! a | b | c
| ^ any content after this column divider is discarded
|
= help: to escape `|` characters in tables, add a `\` before them like `\|`
error: table row has too many columns
--> $DIR/invalid_markdown_table.rs:83:13
|
LL | //! a | b |
| ^ any content after this column divider is discarded
|
= help: to escape `|` characters in tables, add a `\` before them like `\|`
error: unused content after last table cell
--> $DIR/invalid_markdown_table.rs:85:14
|
LL | //! a | b
| ^^ this content is discarded
error: table row has too many columns
--> $DIR/invalid_markdown_table.rs:95:14
|
LL | //! | a | b | c
| ^ any content after this column divider is discarded
|
= help: to escape `|` characters in tables, add a `\` before them like `\|`
error: table row has too many columns
--> $DIR/invalid_markdown_table.rs:97:14
|
LL | //! | a | b |
| ^ any content after this column divider is discarded
|
= help: to escape `|` characters in tables, add a `\` before them like `\|`
error: unused content after last table cell
--> $DIR/invalid_markdown_table.rs:99:15
|
LL | //! | a | b
| ^^ this content is discarded
error: aborting due to 13 previous errors