Skip to content

doc_suspicious_footnotes: lint text that looks like a footnote #14708

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

Merged
merged 1 commit into from
Jun 5, 2025

Conversation

notriddle
Copy link
Contributor

changelog: [doc_suspicious_footnotes]: lint for text that looks like a footnote reference but has no definition

This is an alternative to rust-lang/rust#137803, meant to address the concerns about false positives.

This lint only fires when the apparent footnote reference has a name that's made from pure ASCII digits. This choice is justified by running lintcheck on the top 200 crates, plus the clippy default set:

  1. I ran lintcheck with a modded version of this lint that didn't check for digits only. It produced a false positive warning on a line in mdbook that had a regex, and no true positives at all.
  2. I also ran lintcheck with a custom lint that fired on any valid footnote reference with a non-ascii-digit name. cargo uses one in its job_queue module, and that's all it found.

cc @GuillaumeGomez

@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 29, 2025
@notriddle notriddle force-pushed the doc_suspicious_footnotes branch 2 times, most recently from c56ef28 to 1356e76 Compare April 29, 2025 23:50
@GuillaumeGomez
Copy link
Member

Looks good to me, thanks!

r? samueltardieu

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2025

samueltardieu is on vacation.

Please choose another assignee.

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Great initial patch (and a really good lint idea 👍)! I left some suggestions and some questions.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 4, 2025
@notriddle
Copy link
Contributor Author

@blyxyas Okay, I've implemented variants on all your suggestions.

@notriddle notriddle force-pushed the doc_suspicious_footnotes branch from b0e3325 to 99b16ae Compare May 5, 2025 22:45
@@ -1147,7 +1186,8 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
// Don't check the text associated with external URLs
continue;
}
text_to_check.push((text, range, code_level));
text_to_check.push((text, range.clone(), code_level));
doc_suspicious_footnotes::check(cx, doc, range, &fragments);
Copy link
Member

Choose a reason for hiding this comment

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

I'm sure that you've already tried this, but is there a reason that you didn't use FootnoteReference from ķEvent? You would not have to do span magic and manually parse footnotes and it seems that this function doesn't actually use text

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The FootnoteReference event is only emitted if the code is actually a footnote. This lint fires for text that looks like a footnote reference but isn't.

@notriddle notriddle force-pushed the doc_suspicious_footnotes branch from 00f6841 to 40040e1 Compare May 19, 2025 23:53
@notriddle
Copy link
Contributor Author

@blyxyas

I've redone the suggestions using attr metadata instead of string manipulation.

This also has test cases for cfg_attr and block doc comments.

@notriddle notriddle force-pushed the doc_suspicious_footnotes branch 2 times, most recently from 6c70997 to 98426f5 Compare May 20, 2025 00:13
@notriddle notriddle force-pushed the doc_suspicious_footnotes branch from 02eda3b to a5f4d5d Compare May 21, 2025 18:52
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Seems pretty good overall, I'll go open the FCP. Although some refactoring is needed.

Comment on lines 23 to 32
&& let Some(this_fragment) = fragments
.fragments
.iter()
.find(|frag| {
let found = this_fragment_start < frag.doc.as_str().len();
if !found {
this_fragment_start -= frag.doc.as_str().len();
}
found
})
Copy link
Member

Choose a reason for hiding this comment

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

Could you refactor this piece of code so that it's clearer what its doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I've added some comments and simplified it down to just be a for loop.

.find(|frag| {
let found = this_fragment_start < frag.doc.as_str().len();
if !found {
this_fragment_start -= frag.doc.as_str().len();
Copy link
Member

Choose a reason for hiding this comment

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

Also, could you add a test for this specific line of code? Seems that no tests are triggering this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I've added a test called MultiFragmentFootnote that checks the behavior of that code.

@notriddle
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 27, 2025
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

The last nit (not a clear documentation) and I think this is to be merged! (+ We have team members backing this up)

@blyxyas
Copy link
Member

blyxyas commented Jun 2, 2025

After the change is done, could you squash these commits?

@notriddle notriddle force-pushed the doc_suspicious_footnotes branch from 8ba6d9f to fcfbcc8 Compare June 2, 2025 20:20
@notriddle
Copy link
Contributor Author

@blyxyas Okay, it's done.

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! ❤️

@blyxyas blyxyas added this pull request to the merge queue Jun 5, 2025
Merged via the queue into rust-lang:master with commit c90c7c4 Jun 5, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants