Skip to content

Add new unused_footnote_definition rustdoc lint - #137858

Merged
rust-bors[bot] merged 10 commits into
rust-lang:mainfrom
GuillaumeGomez:unused_footnote_def
Aug 13, 2026
Merged

Add new unused_footnote_definition rustdoc lint#137858
rust-bors[bot] merged 10 commits into
rust-lang:mainfrom
GuillaumeGomez:unused_footnote_def

Conversation

@GuillaumeGomez

@GuillaumeGomezGuillaumeGomez commented Mar 1, 2025

Copy link
Copy Markdown
Member

View all comments

Follow-up of #137803 (where the two first commits come from).

It adds a new lint which checks for unused footnote definitions.

r? @notriddle

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Mar 1, 2025
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez

Copy link
Copy Markdown
MemberAuthor

Fixed tidy.

@GuillaumeGomez
GuillaumeGomezforce-pushed the unused_footnote_def branch 2 times, most recently from 40b5fa2 to 27c7ec4CompareMarch 11, 2025 14:14
@GuillaumeGomez

Copy link
Copy Markdown
MemberAuthor

Updated to use new footnote lint code instead.

@bors

bors commented May 7, 2025

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (presumably #140726) made this pull request unmergeable. Please resolve the merge conflicts.

@lolbinarycatlolbinarycat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment threadsrc/librustdoc/passes/lint/footnotes.rs Outdated
Comment on lines +35 to +44
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 });
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs are outdated. FootnoteReference is only emitted if the footnote definition exists.

pulldown-cmark/pulldown-cmark#1038

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glad to see the docs getting fixed, but i still believe this code handles code blocks incorrectly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment threadtests/rustdoc-ui/lints/unused-footnote.rs Outdated
Comment threadtests/rustdoc-ui/lints/unused-footnote.rs Outdated
Comment threadsrc/librustdoc/lint.rs Outdated
@lolbinarycat

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 21, 2025
@notriddle

notriddle commented Aug 21, 2025

Copy link
Copy Markdown
Contributor

This PR is still based on #137803, which I thought was a bad idea because of the false positives.

The unused_footnote_definition lint shouldn't have any false positives, so it's fine, but this PR needs rebased to remove the other, rejected lint.

@rustbot

This comment has been minimized.

@GuillaumeGomez

Copy link
Copy Markdown
MemberAuthor

Sorry for the delay, finally applied suggestions.

Comment threadsrc/librustdoc/passes/lint/footnotes.rs Outdated
Comment threadsrc/librustdoc/passes/lint/footnotes.rs Outdated
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbotrustbot added the has-merge-commits PR has merge commits, merge with caution. label Feb 25, 2026
@rustbot

This comment has been minimized.

@rustbotrustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 25, 2026
@GuillaumeGomez

Copy link
Copy Markdown
MemberAuthor

@rustbot ready

@rustbotrustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 25, 2026
Comment on lines +24 to +29
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 == "]" =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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\]pubstructBackslashEscape;

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 footnotepubstructSpecials;

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.

Comment threadtests/rustdoc-ui/lints/broken-footnote.rs
@@ -0,0 +1,7 @@
#![deny(rustdoc::broken_footnote)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rustbotrustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 10, 2026
@rust-bors

rust-borsBot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Try build cancelled. Cancelled workflows:

@GuillaumeGomez

Copy link
Copy Markdown
MemberAuthor

Hopefully this time it'll work out.

@bors try jobs=i686-gnu-nopt-1

@rust-bors

This comment has been minimized.

rust-borsBot pushed a commit that referenced this pull request Aug 12, 2026
Add new `unused_footnote_definition` rustdoc lint
try-job: i686-gnu-nopt-1
@rust-bors

rust-borsBot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: cd6c798 (cd6c798f92f2779a147129bee56a0d19d15e3337)
Base parent: 41fb9d4 (41fb9d458726b5effbd64c7c1beced947ea03242)

@GuillaumeGomez

Copy link
Copy Markdown
MemberAuthor

Ah finally. \o/

@bors r=notriddle

@rust-bors

rust-borsBot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 116fdd1 has been approved by notriddle

It is now in the queue for this repository.

@rust-borsrust-borsBot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 12, 2026
rust-borsBot pushed a commit that referenced this pull request Aug 12, 2026
…uwer
Rollup of 4 pull requests
Successful merges:
- #137803 (Add new rustdoc `broken_footnote` lint)
- #160975 (Remove target argument from get_proc_macros)
- #160861 (rustc_parse: suggest removing semicolon before `if` block)
- #160990 (Remove old cfg parser which is now dead code)
Failed merges:
- #137858 (Add new `unused_footnote_definition` rustdoc lint)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 12, 2026
…, r=notriddle
Add new `unused_footnote_definition` rustdoc lint
Follow-up of rust-lang#137803 (where the two first commits come from).
It adds a new lint which checks for unused footnote definitions.
r? @notriddle
rust-borsBot pushed a commit that referenced this pull request Aug 12, 2026
…uwer
Rollup of 5 pull requests
Successful merges:
- #160975 (Remove target argument from get_proc_macros)
- #160985 (self-profile more of borrowck)
- #137858 (Add new `unused_footnote_definition` rustdoc lint)
- #160861 (rustc_parse: suggest removing semicolon before `if` block)
- #160990 (Remove old cfg parser which is now dead code)
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 12, 2026
…, r=notriddle
Add new `unused_footnote_definition` rustdoc lint
Follow-up of rust-lang#137803 (where the two first commits come from).
It adds a new lint which checks for unused footnote definitions.
r? @notriddle
@jhprattjhpratt mentioned this pull request Aug 13, 2026
rust-borsBot pushed a commit that referenced this pull request Aug 13, 2026
Rollup of 13 pull requests
Successful merges:
- #160976 (Ensure TLS accesses don't call the global allocator through panic (part 2))
- #160438 ([rustdoc] Change table odd table rows background color to not make it the same as inline code)
- #160985 (self-profile more of borrowck)
- #137858 (Add new `unused_footnote_definition` rustdoc lint)
- #159566 (Add support for generics in `offload`and remove `no_mangle` attribute)
- #160676 (rustc_public: split `def`s out of `ty`)
- #160861 (rustc_parse: suggest removing semicolon before `if` block)
- #160958 (Remove unused `#[non_exhaustive]` in library)
- #160978 (move resolve_path to Session inherent method)
- #160990 (Remove old cfg parser which is now dead code)
- #160991 (Add offload component on nightly)
- #160999 (doc changes to expect messages in process.rs)
- #161003 (Also warn if an invalid `doc` attribute is used on a macro invocation)
rust-borsBot pushed a commit that referenced this pull request Aug 13, 2026
Rollup of 13 pull requests
Successful merges:
- #160976 (Ensure TLS accesses don't call the global allocator through panic (part 2))
- #160438 ([rustdoc] Change table odd table rows background color to not make it the same as inline code)
- #160985 (self-profile more of borrowck)
- #137858 (Add new `unused_footnote_definition` rustdoc lint)
- #159566 (Add support for generics in `offload`and remove `no_mangle` attribute)
- #160676 (rustc_public: split `def`s out of `ty`)
- #160861 (rustc_parse: suggest removing semicolon before `if` block)
- #160958 (Remove unused `#[non_exhaustive]` in library)
- #160978 (move resolve_path to Session inherent method)
- #160990 (Remove old cfg parser which is now dead code)
- #160991 (Add offload component on nightly)
- #160999 (doc changes to expect messages in process.rs)
- #161003 (Also warn if an invalid `doc` attribute is used on a macro invocation)
@rust-bors
rust-borsBot merged commit 42ef497 into rust-lang:mainAug 13, 2026
14 checks passed
@rustbotrustbot added this to the 1.99.0 milestone Aug 13, 2026
rust-timer added a commit that referenced this pull request Aug 13, 2026
Rollup merge of #137858 - GuillaumeGomez:unused_footnote_def, r=notriddle
Add new `unused_footnote_definition` rustdoc lint
Follow-up of #137803 (where the two first commits come from).
It adds a new lint which checks for unused footnote definitions.
r? @notriddle
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (47c7eb4): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.3%[0.1%, 0.8%]10
Regressions ❌
(secondary)
0.1%[0.1%, 0.1%]2
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)0.3%[0.1%, 0.8%]10

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

Results (primary -1.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
--0
Improvements ✅
(primary)
-1.4%[-1.4%, -1.4%]1
Improvements ✅
(secondary)
--0
All ❌✅ (primary)-1.4%[-1.4%, -1.4%]1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 455.642s -> 455.618s (-0.01%)
Artifact size: 396.40 MiB -> 396.36 MiB (-0.01%)

@rustbotrustbot added the perf-regression Performance regression. label Aug 18, 2026
@Kobzol

Copy link
Copy Markdown
Member

This caused a tiny overall regression for doc builds, which makes sense, as it is a new lint.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Aug 18, 2026
@lcnrlcnr added the relnotes Marks issues that should be documented in the release notes of the next release. label Aug 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.relnotesMarks issues that should be documented in the release notes of the next release.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@GuillaumeGomez@rust-log-analyzer@bors@lolbinarycat@notriddle@rustbot@JonathanBrouwer@rust-timer@Kobzol@lcnr