-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Add new unescaped_pipe_in_table_cell rustdoc lint
#159583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,99 @@ | ||||||
| //! Detects table rows where some content seems to have been discarded because there are too many | ||||||
| //! pipe characters. | ||||||
|
|
||||||
| use std::ops::Range; | ||||||
|
|
||||||
| use rustc_hir::HirId; | ||||||
| use rustc_macros::Diagnostic; | ||||||
| use rustc_resolve::rustdoc::pulldown_cmark::{Event, Parser, Tag, TagEnd}; | ||||||
| use rustc_resolve::rustdoc::source_span_for_markdown_range; | ||||||
|
|
||||||
| use crate::clean::*; | ||||||
| use crate::core::DocContext; | ||||||
| use crate::html::markdown::main_body_opts; | ||||||
|
|
||||||
| #[derive(Diagnostic)] | ||||||
| #[diag("table row has too many columns")] | ||||||
| #[help("to escape `|` characters in tables, add a `\\` before them like `\\|`")] | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| struct UnescapedPipeInTableCell { | ||||||
| #[primary_span] | ||||||
| #[label("any content after this column divider is discarded")] | ||||||
| span: rustc_span::Span, | ||||||
| } | ||||||
|
|
||||||
| pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &str) { | ||||||
| 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() | ||||||
|
Urgau marked this conversation as resolved.
|
||||||
| { | ||||||
| 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, | ||||||
| _ => {} | ||||||
| } | ||||||
| } | ||||||
| let mut prev_range = None; | ||||||
| while let Some((event, range)) = p.next() { | ||||||
| match event { | ||||||
| Event::End(TagEnd::TableCell) => { | ||||||
| prev_range = Some(range); | ||||||
| } | ||||||
| Event::End(TagEnd::TableRow) => { | ||||||
| if let Some(prev_range) = &prev_range | ||||||
| // So here what is happening: when `pulldown-cmark` is parsing a table | ||||||
| // and a table row has too many cells, it doesn't emit events for the | ||||||
| // extra cells. So the only way for us to know these extra cells exist | ||||||
| // is to compare the row's span with the last emitted cell event's span. | ||||||
| // If the span ends don't match, then there are extra cells. | ||||||
| && prev_range.end + 1 != range.end | ||||||
|
Urgau marked this conversation as resolved.
|
||||||
| { | ||||||
| // Something seems wrong, the range diff doesn't match, some content | ||||||
| // was left out. We now check the number of unescaped `|`. | ||||||
| let row = &dox[range.clone()]; | ||||||
| let mut iter = row.chars(); | ||||||
| let mut divider_count = 0; | ||||||
| while let Some(c) = iter.next() { | ||||||
| if c == '\\' { | ||||||
| iter.next(); | ||||||
|
Urgau marked this conversation as resolved.
|
||||||
| } else if c == '|' { | ||||||
| divider_count += 1; | ||||||
| } | ||||||
| } | ||||||
| // + 1 is to handle the `|` at the end of the table row. | ||||||
| if divider_count <= expected_cells + 1 | ||||||
| || dox[Range { start: prev_range.end + 1, end: range.end }] | ||||||
| .trim() | ||||||
| .is_empty() | ||||||
| { | ||||||
| // Seems all good so let's ignore it and continue;. | ||||||
| continue; | ||||||
| } | ||||||
| let last_cell_separator = | ||||||
| Range { start: prev_range.end, end: prev_range.end + 1 }; | ||||||
|
|
||||||
| if let Some((span, _)) = source_span_for_markdown_range( | ||||||
| cx.tcx, | ||||||
| dox, | ||||||
| &last_cell_separator, | ||||||
| &item.attrs.doc_strings, | ||||||
| ) { | ||||||
| cx.tcx.emit_node_span_lint( | ||||||
| crate::lint::UNESCAPED_PIPE_IN_TABLE_CELL, | ||||||
| hir_id, | ||||||
| span, | ||||||
| UnescapedPipeInTableCell { span }, | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Event::End(TagEnd::Table) => break, | ||||||
| _ => {} | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| one | two |
|-|-|
| a |
| b
one | two
-|-
a | b | c
a |
*[View changes since the review](https://triagebot.infra.rust-lang.org/gh-changes-since/rust-lang/rust/159583/cde3f8aee5a30928872e2438e3f18238a3fa306c..6fd9b1bce2dd4cd99e32dc333104810bef776cc0)* |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,38 @@ | ||||
| #![deny(rustdoc::unescaped_pipe_in_table_cell)] | ||||
|
|
||||
| //! | col1 | | ||||
|
Urgau marked this conversation as resolved.
|
||||
| //! | ---- | | ||||
| //! | `code_with(|arg| arg)` | | ||||
| //~^ ERROR unescaped_pipe_in_table_cell | ||||
| //! | one `|` b | | ||||
| //~^ ERROR unescaped_pipe_in_table_cell | ||||
| //! | ||||
| // Testing another lint emission on the same doc comment. | ||||
| //! | ||||
| //! | col1 | | ||||
| //! | ---- | | ||||
| //! | `code_with(|arg| arg)` | | ||||
| //~^ ERROR unescaped_pipe_in_table_cell | ||||
|
|
||||
| // We check that the extra whitespace characters at the end of the line won't trigger | ||||
| // the lint. | ||||
| mod b { | ||||
| //! | col | | ||||
| //! | ---- | | ||||
| #![doc = "| code_with | "] | ||||
| } | ||||
|
|
||||
| // We check that the `\|` is correctly handled as well (ie not emitting the lint). | ||||
| mod c { | ||||
| //! | col | | ||||
| //! | ---- | | ||||
| //! | a \| still same cell | | ||||
| } | ||||
|
|
||||
| // We check that content after a table row is ignored. | ||||
| mod d { | ||||
| //! | col | | ||||
| //! | ---- | | ||||
| //! | code_with | aaaaa | ||||
| //^ the "aaaaa" part will be ignored. | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it ignored? It seems like an example of the thing you’re trying to warn about.
|
||||
| } | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| error: table row has too many columns | ||
| --> $DIR/unescaped_pipe_in_table_cell.rs:5:18 | ||
| | | ||
| LL | //! | `code_with(|arg| arg)` | | ||
| | ^ any content after this column divider is discarded | ||
| | | ||
| = help: to escape `|` characters in tables, add a `\` before them like `\|` | ||
| note: the lint level is defined here | ||
| --> $DIR/unescaped_pipe_in_table_cell.rs:1:9 | ||
| | | ||
| LL | #![deny(rustdoc::unescaped_pipe_in_table_cell)] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: table row has too many columns | ||
| --> $DIR/unescaped_pipe_in_table_cell.rs:7:12 | ||
| | | ||
| LL | //! | one `|` b | | ||
| | ^ 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/unescaped_pipe_in_table_cell.rs:14:18 | ||
| | | ||
| LL | //! | `code_with(|arg| arg)` | | ||
| | ^ any content after this column divider is discarded | ||
| | | ||
| = help: to escape `|` characters in tables, add a `\` before them like `\|` | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow TIL that this is how GFM works. Very strange design...
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was super confused as well.