rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Add new `too_long_first_doc_paragraph` first paragraph lint

Open GuillaumeGomez opened this issue 1 year ago • 3 comments

Fixes https://github.com/rust-lang/rust-clippy/issues/12989.

changelog: Add new too_long_first_doc_paragraph first paragraph lint

GuillaumeGomez avatar Jun 24 '24 16:06 GuillaumeGomez

r? @Centri3

rustbot has assigned @Centri3. 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 avatar Jun 24 '24 16:06 rustbot

I increased the limit to 100 and I also applied suggestions from @flip1995 to suggest to add an empty doc line to separate the first sentence from the rest when applicable.

GuillaumeGomez avatar Jun 28 '24 16:06 GuillaumeGomez

Any idea how I could disable just this lint on clippy tests by any chance?

GuillaumeGomez avatar Jul 01 '24 09:07 GuillaumeGomez

I would rather take a closer look at these and improve the lint a bit. E.g. this seems like a FP to me: https://github.com/rust-lang/rust-clippy/actions/runs/9715600206/job/26817360799?pr=12993#step:5:3958

error: first doc comment paragraph is too long
  --> tests/ui/index_refutable_slice/if_let_slice_binding.rs:168:1
   |
LL | / /// The lint will ignore bindings with sub patterns as it would be hard
LL | | /// to build correct suggestions for these instances :)
   | |_

This is still a really short comment and IMO fine to have in documentation.

flip1995 avatar Jul 02 '24 17:07 flip1995

But it's not a short description. In this case it should be:

/// The lint will ignore bindings with sub patterns.

For the second part (the "why?"), it should be part of the rest of the doc comment.

GuillaumeGomez avatar Jul 04 '24 09:07 GuillaumeGomez

I disagree. If the explanation for the first statement is as short as this, I'd rather be able to read it in the docs.rs overview, than having to click on the item to try to understand why this is the case.

image

image

I think it is hard to argue that the first (long) version is bad doc writing. This is a screenshot when using half my screen. It's barely 2 lines in the summary, but I have all information I need, without having to click on the item.


In any case, I don't think this should warn on private items, that won't be shown in the documentation. Or at least, this should be configurable, with the default being not to lint on private items.

flip1995 avatar Jul 04 '24 10:07 flip1995

I can config for the length of the line (default to 120?) and if it should warn on private items (default to false?). What do you think?

GuillaumeGomez avatar Jul 08 '24 13:07 GuillaumeGomez

I think that would be a good step. However, I don't know how much it helps to make the line length configurable.

flip1995 avatar Jul 08 '24 14:07 flip1995

I don't either. We either need to pick the maximum length or allow users to change it however they want. But it seems like we can't agree on a line length here already. :laughing:

GuillaumeGomez avatar Jul 08 '24 14:07 GuillaumeGomez

I think it's a wise principle for lints to err on the side of being conservative (that is, giving the user's code the benefit of the doubt). A lint that triggers too often will soon be silenced, whereas a lint that's a little too easy-going is still useful.

I looked again at a few samples from the standard library, and the longest intro paragraphs that seemed to me to keep the spirit of "one short sentence" were no longer than about 150 characters. Example:

Returns vector content as a slice of T, along with the remaining spare capacity of the vector as a slice of MaybeUninit<T>.

Clearly if that sentence were longer, it would not be short! But it also seems quite reasonable to me. Adding a 50% safety margin would give us a limit of 225.

There are lots of cases in the stdlib docs where this limit is exceeded, but I think those cases should trigger the lint! For example:

Consumes and leaks the Vec, returning a mutable reference to the contents, &'a mut [T]. Note that the type T must outlive the chosen lifetime 'a. If the type has only static references, or none at all, then this may be chosen to be 'static.

The second and third sentences here should really move to the next paragraph, so the lint would already be delivering value here. A limit of 250 characters would have missed this, so that's clearly too high. A limit of less than 200 would already be incorrectly catching lots of cases that, on inspection, are quite reasonable.

Thus, anything within the 200-250 range is defensible, but it seems hard to argue for a limit outside that.

bitfield avatar Jul 08 '24 15:07 bitfield

Let's go for 200 then. We can always add the config later on if needed.

GuillaumeGomez avatar Jul 08 '24 16:07 GuillaumeGomez

200 sounds good to me. The default comment width is 80 in rustfmt. So 200 is 2.5x of that. Which is in line with @bitfield argument to add a 50% safety margin, just from a different starting point. 2 lines of first line documentation seems fine to me, as weird as that sounds.

If you think there's value in making this configurable, go for it. It doesn't cost much.

I still haven't looked at the implementation, but if the lint produces a suggestion, I think a good idea would be to suggest a break after the first . or other punctuation found in the too-long-line.

flip1995 avatar Jul 09 '24 09:07 flip1995

Updated the limit to 200. Added configuration for private items and fixed dogfood.

GuillaumeGomez avatar Jul 09 '24 14:07 GuillaumeGomez

Lintcheck diff summary looks good and those all seem to be cases where the documentation could be improved. Sometimes trivially.

I'll let @Centri3 do the code review of the lint, but the behavior now seems good to me.

flip1995 avatar Jul 10 '24 09:07 flip1995

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

bors avatar Jul 17 '24 19:07 bors

Fixed merge conflict.

GuillaumeGomez avatar Jul 17 '24 19:07 GuillaumeGomez

Updated!

GuillaumeGomez avatar Jul 21 '24 15:07 GuillaumeGomez

Can't believe I forgot to run uibless... Fixed the CI.

GuillaumeGomez avatar Jul 26 '24 09:07 GuillaumeGomez

Updated!

GuillaumeGomez avatar Aug 18 '24 18:08 GuillaumeGomez

@bors r+

Centri3 avatar Aug 24 '24 15:08 Centri3

:pushpin: Commit a2033428c4ca9883a2b7cbbd1f167d481b0d240c has been approved by Centri3

It is now in the queue for this repository.

bors avatar Aug 24 '24 15:08 bors

:hourglass: Testing commit a2033428c4ca9883a2b7cbbd1f167d481b0d240c with merge 30e0b69908332ea5556511b42f66e50e9fd3fbc8...

bors avatar Aug 24 '24 15:08 bors

:sunny: Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test Approved by: Centri3 Pushing 30e0b69908332ea5556511b42f66e50e9fd3fbc8 to master...

bors avatar Aug 24 '24 15:08 bors