Add new unused_footnote_definition rustdoc lint#137858
Add new unused_footnote_definition rustdoc lint#137858GuillaumeGomez wants to merge 6 commits intorust-lang:mainfrom
unused_footnote_definition rustdoc lint#137858Conversation
This comment has been minimized.
This comment has been minimized.
c17e182 to
8550b0a
Compare
|
Fixed tidy. |
40b5fa2 to
27c7ec4
Compare
|
Updated to use new |
|
☔ The latest upstream changes (presumably #140726) made this pull request unmergeable. Please resolve the merge conflicts. |
lolbinarycat
left a comment
There was a problem hiding this comment.
One main issue (possibly parsing footnotes as code blocks) and a bunch of small nits.
| let mut footnote_definitions = FxHashMap::default(); | ||
|
|
||
| let options = Options::ENABLE_FOOTNOTES; | ||
| let mut parser = Parser::new_ext(dox, options).into_offset_iter().peekable(); |
There was a problem hiding this comment.
Should we be making sure the lint is enabled before invoking the parser? I know the other lints don't do this, but maybe they should?
| Event::Text(text) | ||
| if &*text == "[" | ||
| && let Some((Event::Text(text), _)) = parser.peek() | ||
| && text.trim_start().starts_with('^') | ||
| && parser.next().is_some() | ||
| && let Some((Event::Text(text), end_span)) = parser.peek() | ||
| && &**text == "]" => | ||
| { | ||
| missing_footnote_references.insert(Range { start: span.start, end: end_span.end }); | ||
| } |
There was a problem hiding this comment.
It is quite odd that pulldown_cmark isn't emmitting some form of FootnoteReference here despite the docs saying they might not map to an actual definition.
In any case, I don't think this implementation is correct, since Text events are emitted for the bodies of all blocks. Crucially, this includes code blocks, which certainly should not be parsed for footnotes. An integration test should be added to show that we are not wrongfully parsing footnotes within code blocks.
One way to handle this is to track the type of the last Start event and make sure it isn't CodeBlock. luckily other blocks can't appear within code blocks so we don't have to track the full stack of tags. We might want to clear that variable whenever we reach an End event, but I'm not sure if the text after a code block will always get its own separate Paragraph event or not. An integration test with an unused footnote directly after a code block should clear this up.
There was a problem hiding this comment.
The docs are outdated. FootnoteReference is only emitted if the footnote definition exists.
There was a problem hiding this comment.
glad to see the docs getting fixed, but i still believe this code handles code blocks incorrectly.
There was a problem hiding this comment.
This could be easily answered by some tests that exercise the case of broken markdown inside a code block, as well as a footnote definition outside the code block referenced by markdown inside the block, and the other way around.
|
@rustbot author |
|
This PR is still based on #137803, which I thought was a bad idea because of the false positives. The |
27c7ec4 to
2cdc54b
Compare
This comment has been minimized.
This comment has been minimized.
|
Sorry for the delay, finally applied suggestions. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
6074417 to
8f2b963
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
| if &*text == "[" | ||
| && let Some((Event::Text(text), _)) = parser.peek() | ||
| && text.trim_start().starts_with('^') | ||
| && parser.next().is_some() | ||
| && let Some((Event::Text(text), end_span)) = parser.peek() | ||
| && &**text == "]" => |
There was a problem hiding this comment.
This parsing logic doesn't fully account for backslashes or special characters. This test case fails (it's a false positive, because it produces a warning when it shouldn't):
/// Backslash escaped footnotes should not be recognized:
///
/// [\^4]
///
/// [^5\]
pub struct BackslashEscape;And so does this one (it's a false negative, since it's supposed to produce a warning, but it doesn't):
/// Footnotes can contain asterisks, underscores, and other specials:
///
/// [^*]
//~^ ERROR: no footnote definition matching this footnote
///
/// [^_]
//~^ ERROR: no footnote definition matching this footnote
///
/// [^<inside></inside>]
//~^ ERROR: no footnote definition matching this footnote
pub struct Specials;To do this correctly, you need to parse the source text, not the returned event stream. Mostly copy scan_link_label, but strip out everything unrelated to footnotes.
There was a problem hiding this comment.
These moved into tests/rustdoc-html now...
| @@ -0,0 +1,7 @@ | |||
| #![deny(rustdoc::broken_footnote)] | |||
There was a problem hiding this comment.
A selection of many good corner cases to test this lint against can be found here: https://pulldown-cmark.github.io/pulldown-cmark/specs/footnotes.html
Follow-up of #137803 (where the two first commits come from).
It adds a new lint which checks for unused footnote definitions.
r? @notriddle