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.
|
@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. |
| //! Detects specific markdown syntax that's different between pulldown-cmark | ||
| //! 0.9 and 0.11. | ||
| //! | ||
| //! This is a mitigation for old parser bugs that affected some | ||
| //! real crates' docs. The old parser claimed to comply with CommonMark, | ||
| //! but it did not. These warnings will eventually be removed, | ||
| //! though some of them may become Clippy lints. | ||
| //! | ||
| //! <https://github.com/rust-lang/rust/pull/121659#issuecomment-1992752820> | ||
| //! | ||
| //! <https://rustc-dev-guide.rust-lang.org/bug-fix-procedure.html#add-the-lint-to-the-list-of-removed-lists> |
There was a problem hiding this comment.
This doc comment is wrong.
There was a problem hiding this comment.
I will simply remove it.
| .unwrap_or_else(|| (item.attr_span(tcx), false)); | ||
|
|
||
| tcx.node_span_lint(crate::lint::BROKEN_FOOTNOTE, hir_id, ref_span, |lint| { | ||
| lint.primary_message("no footnote definition matching this footnote"); |
There was a problem hiding this comment.
This lint is redundant with the doc_suspicious_footnotes clippy lint, but it doesn't implement the number checking the same way. Shouldn't that be added?
There was a problem hiding this comment.
The check for clippy is wrong: it ignores footnotes like [^bla] although rustdoc supports them. I even added a test in this PR for this case. :)
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 |
Follow-up of #137803 (where the two first commits come from).
It adds a new lint which checks for unused footnote definitions.
r? @notriddle