Skip to content
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

new lint: doc_comment_double_space_linebreaks #12876

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Jacherr
Copy link
Contributor

@Jacherr Jacherr commented Jun 1, 2024

Fixes #12163

I decided to initially make this a restriction lint because it felt a bit niche and opinionated to be a warn-by-default style lint. It may be appropriate as a style lint if the standard or convention is to use \ as doc comment linebreaks - not sure if they are!

The wording on the help message could be improved, as well as the name of the lint itself since it's a bit wordy - suggestions welcome.

This lint works on both /// and //! doc comments.

changelog: new lint: doc_comment_double_space_linebreaks

@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2024

r? @xFrednet

rustbot has assigned @xFrednet.
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 Jun 1, 2024
Copy link

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Thanks a lot for implementing the lint I suggested!

I have added two small comments here, but feel free to consider them only suggestions since I am not a project member.

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

Very nice start! I left some comments, mostly for some additional tests. Once they have been addressed, this should be ready to be merged :D

Also thank you for the suggestions from @Marcono1234!

@notriddle
Copy link
Contributor

This lint also needs to make sure it's not firing in any of the cases where Markdown doesn't treat two spaces at the end of a line as a hard line break.

Here's a link to a markdown-it playground where almost every line ends in two spaces, but it contains no hard line breaks. It gets the same results in GitHub and Rustdoc.

@Jacherr
Copy link
Contributor Author

Jacherr commented Jun 28, 2024

That's quite a lot of cases to handle - I'm not sure building in a subset of a markdown parser just for this one lint is the right way forward? I suppose it's only a handful of cases, but seems like it'd be a lot of work to implement and test properly, for the sake of this lint. If push comes to shove I can do it, but perhaps there's an easier/more general way to cover these cases?

@notriddle
Copy link
Contributor

notriddle commented Jun 28, 2024

I think this can be done more easily than that, yes.

https://github.com/rust-lang/rust/blob/e9e6e2e444c30c23a9c878a88fbc3978c2acad95/src/tools/clippy/clippy_lints/src/doc/mod.rs#L660

In clippy_lints::doc::check_doc, the range variable gives you the start and end byte position of the event. By doing doc.as_bytes()[range.clone()], you can extract the source code for an event.

If the event is a HardBreak, and the source code is b" ", then you've got what you're looking for. No parsing required!

@Jacherr
Copy link
Contributor Author

Jacherr commented Jun 29, 2024

I'm trying to use the events to do this, but even though according to the markdown you provided most of those aren't valid linebreaks, I still seem to be getting HardBreak events for them? It's entirely possible I'm doing something wrong, but it doesn't seem to automatically "distinguish" between a valid linebreak and an invalid one.

@notriddle
Copy link
Contributor

notriddle commented Jun 29, 2024

It’s an outdated version of pulldown. The current version does it right.

pulldown-cmark/pulldown-cmark#318

@Jacherr
Copy link
Contributor Author

Jacherr commented Jun 29, 2024

Given the fact that this is pretty niche, and that this is a restriction lint, and the pulldown crate is not up-to-date (with no simple way of updating it), I think the best course of action is leaving this as a FIXME in the lint for future improvement?

@xFrednet any thoughts on this? Personally I feel like this is a niche enough issue to not be too important, since the double spaces in these cases do nothing and don't need to be there at all in tables etc.

@notriddle
Copy link
Contributor

Once rust-lang/rust#127127 is merged, those issues should go away?

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 30, 2024
…-0.11, r=GuillaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang/rust-clippy#12876
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 30, 2024
…-0.11, r=GuillaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang/rust-clippy#12876
@xFrednet
Copy link
Member

The changes from the pull request will need some time to get synced back to this PR (Should be ~2 weeks). For now you can edit the Crates.toml file here to use the updated version.

@notriddle Thank you for the update and comments! ❤️

@xFrednet
Copy link
Member

@xFrednet any thoughts on this? Personally I feel like this is a niche enough issue to not be too important, since the double spaces in these cases do nothing and don't need to be there at all in tables etc.

It depends a bit on how much work it is. Personally, I would prefer it to use pulldown since we have it. It will likely also be more maintainable.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 1, 2024
….11, r=GuillaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang/rust-clippy#12876
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 2, 2024
….11, r=GuillaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang/rust-clippy#12876
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 4, 2024
….11, r=GuillaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang/rust-clippy#12876
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jul 4, 2024
…illaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang/rust#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang/rust-clippy#12876
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jul 11, 2024
…illaumeGomez

rustdoc: update to pulldown-cmark 0.11

r? rustdoc

This pull request updates rustdoc to the latest version of pulldown-cmark. Along with adding new markdown extensions (which this PR doesn't enable), the new pulldown-cmark version also fixes a large number of bugs. Because all text files successfully parse as markdown, these bugfixes change the output, which can break people's existing docs.

A crater run, rust-lang/rust#121659, has already been run for this change.

The first commit upgrades and fixes rustdoc. The second commit adds a lint for the footnote and block quote parser changes, which break the largest numbers of docs in the Crater run. The strikethrough change was mitigated in pulldown-cmark itself.

Unblocks rust-lang#12876
@bors
Copy link
Contributor

bors commented Jul 17, 2024

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

@xFrednet
Copy link
Member

xFrednet commented Aug 3, 2024

Hey @Jacherr, this is a ping from triage, since there hasn't been any activity in some time. The new version should now be available if you rebase on master.

Do you plan on continuing this implementation?

@rustbot author

@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 Aug 3, 2024
@Jacherr
Copy link
Contributor Author

Jacherr commented Aug 3, 2024

Hey, apologies for the silence, been rather busy the last few weeks. I will complete this implementation but unfortunately will be a week or two before I get to it, if that’s okay.

@Jacherr
Copy link
Contributor Author

Jacherr commented Aug 9, 2024

I almost have this working properly, the one case I'm struggling to handle is detecting if a doc comment is in a macro definition, which we don't want to lint on. Is there any way to detect this from a span?

@Jacherr
Copy link
Contributor Author

Jacherr commented Aug 9, 2024

@rustbot ready

Hopefully got enough test-cases and caught any edge cases now. I'm not sure if how I extended check_doc is considered "correct" - it can pretty easily be changed if needed, I imagine.

@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 Aug 9, 2024
span_lint_and_then(
cx,
DOC_COMMENT_DOUBLE_SPACE_LINEBREAK,
lo_span.to(hi_span),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do things work if you avoid pointing at the line break? The version that spans the whole thing looks really weird when it's formatted.

Suggested change
lo_span.to(hi_span),
lo_span.shrink_to_lo(),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I imagine this will only work if every line is linted individually. This could be done but not sure it's desired behaviour as it will drastically increase the amount of warnings per paragraph, usually.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense. 👍

Copy link
Member

Choose a reason for hiding this comment

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

I would be in favor of having a warning for each double space. Looking at the lintcheck output in our CI, it's hard to see what the actual problem is.

In this window it looks like a normal line break:

   --> target/lintcheck/sources/arrayvec-0.7.4/src/arrayvec.rs:380:53
    |
380 |       /// This is a checked version of `.swap_remove`.  
    |  _____________________________________________________^
381 | |     /// This operation is O(1).
    | |________^

If the message only spanned the two spaces, it would look like this:

   --> target/lintcheck/sources/arrayvec-0.7.4/src/arrayvec.rs:380:53
    |
380 |       /// This is a checked version of `.swap_remove`.  
    |                                                       ^^
    |

This is much clearer IMO.

It also makes it easier to apply the suggestion automatically as there is less chance to have the span overlap with other lint emissions.

@xFrednet xFrednet changed the title new restriction lint: doc_comment_double_space_linebreak new lint: doc_comment_double_space_linebreak Aug 12, 2024
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

Two smaller comments, but this implementation already looks good :D

I also commented on previous reviews:

Also, big thanks to @notriddle and @Marcono1234 for their reviews ❤️

@bors
Copy link
Contributor

bors commented Aug 24, 2024

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

@bors
Copy link
Contributor

bors commented Nov 21, 2024

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

@Jacherr
Copy link
Contributor Author

Jacherr commented Jan 27, 2025

@xFrednet this has been sat for a while, does all look good on your end? Seems that all that was left were a few nits which I cleaned up so once the conflicts are solved would this be ready to be merged? No rush of course

@xFrednet
Copy link
Member

Woops, this totally slipped by my inbox. Sorry, thank you for pinging me! 😅

I'll put it in my reviewing queue. Currently it takes a bit longer due to RL stuff, but you should have a review in 1-2 weeks.

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

I believe a previous comment is still unresolved, at least according to the .stderr file, otherwise LGTM

Comment on lines 2 to 6
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:7:43
|
LL | //! Should warn on double space linebreaks
| ___________________________________________^
LL | | //! in file/module doc comment
| |____^
|
Copy link
Member

Choose a reason for hiding this comment

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

Have you tried changing the error span, like suggested here?

#12876 (comment)

Suggested change
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:7:43
|
LL | //! Should warn on double space linebreaks
| ___________________________________________^
LL | | //! in file/module doc comment
| |____^
|
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:7:43
|
LL | //! Should warn on double space linebreaks
| ^^
|
|

It should be something simple, like taking a span from lo_span to lo_span + 2

@Jacherr
Copy link
Contributor Author

Jacherr commented Feb 13, 2025

Just to be clear - is this fine as a restriction lint? I have no idea if the preferred way is to use a backslash or double space. If the correct method is a backslash maybe this would be better as a warn by default?

@Jacherr Jacherr force-pushed the issue-12163 branch 2 times, most recently from 286ab79 to ba6aeb1 Compare February 13, 2025 17:03
@xFrednet
Copy link
Member

Just to be clear - is this fine as a restriction lint?

Yes, I think this category is perfect for the lint. That's why I didn't comment on it further.

I have no idea if the preferred way is to use a backslash or double space. If the correct method is a backslash maybe this would be better as a warn by default?

I think a backslash should be the default if you need a random line break, for the reasons described in the lint description.

@Jacherr
Copy link
Contributor Author

Jacherr commented Feb 26, 2025

So just to be completely sure, it's in pedantic right now and that's all good? Just don't want to miss anything before getting it looked at again and hopefully merged 😅

@xFrednet
Copy link
Member

xFrednet commented Mar 1, 2025

Currently, it's a pedantic lint. I'll ask for feedback on the category as part of the Final Comment Period (FCP) :)

@xFrednet xFrednet changed the title new lint: doc_comment_double_space_linebreak new lint: doc_comment_double_space_linebreaks Mar 1, 2025
Jacherr and others added 3 commits March 1, 2025 12:01
fix typo

change replacement character in example, remove extraneous space from suggested change

add additional testcases; check doc comment not from expansion

do not lint on macros, add more testcases

fix wording, remove commented out code, add additonal testcase

uibless

fix doc comments, use optional snippets

Remove unneeded additional space
@xFrednet
Copy link
Member

xFrednet commented Mar 1, 2025

Hej @Jacherr, I wanted to play a bit around with the error message to have the best of both worlds, one lint per paragraph, but also just the two spaces highlighted. I pushed my fix now and also adjusted the lint name, by adding an s to the end.

Now this should be good to go, I'll create an FCP and also ask for feedback on the lint category.

(I hope it was okay to push to your branch and also squash your commits 🙈 )

Edit: FCP: https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/FCP.20new.20lint.3A.20.60doc_comment_double_space_linebreaks.60

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.

[rustdoc] Suggest trailing \ instead of two spaces for hard line break
6 participants