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

Add new style lint: Warn if first paragraph in docstring is too long. #12989

Open
emilk opened this issue Jun 23, 2024 · 1 comment · May be fixed by #12993
Open

Add new style lint: Warn if first paragraph in docstring is too long. #12989

emilk opened this issue Jun 23, 2024 · 1 comment · May be fixed by #12993
Labels
A-lint Area: New lints

Comments

@emilk
Copy link

emilk commented Jun 23, 2024

What it does

Warn when the first paragraph of a docstring is overly long.

cargo doc will show the first paragraph of the doscstring in the summary page on a crate and module, so having a nice, short summary in the first paragraph is part of writing good docs.

A very long first paragraph can be easily created by mistake. That's because cargo doc (and most markdown viewers) ignores single newlines and requires a double newline to register as a new paragraph. So this is one paragraph:

/// A very short summary.
/// A much longer explanation that goes into a lot more detail about
/// how the thing works, possibly with doclinks and so one,
/// and probably spanning a many rows.
struct Foo {}

while this is two paragraphs:

/// A very short summary.
///
/// A much longer explanation that goes into a lot more detail about
/// how the thing works, possibly with doclinks and so one,
/// and probably spanning a many rows.
struct Foo {}

Advantage

An overly long first paragraph makes for a bad summary, and a bad overview in a docs page for a crate:

Screenshot 2024-06-23 at 22 44 27

Here's how it looks with an added ///:
Screenshot 2024-06-23 at 22 52 57

Drawbacks

It might be annoying if the length limit is set too low.

I suggest a default of 80 chars, but configurable in clippy.toml.

Example

/// A rectangular region of space.
/// Usually a [`Rect`] has a positive (or zero) size,
/// and then [`Self::min`] `<=` [`Self::max`].
/// In these cases [`Self::min`] is the left-top corner
/// and [`Self::max`] is the right-bottom corner.
/// A rectangle is allowed to have a negative size, which happens when the order
/// of `min` and `max` are swapped. These are usually a sign of an error.
/// Normally the unit is points (logical pixels) in screen space coordinates.
/// `Rect` does NOT implement `Default`, because there is no obvious default value.
/// [`Rect::ZERO`] may seem reasonable, but when used as a bounding box, [`Rect::NOTHING`]
/// is a better default - so be explicit instead!
#[derive(Clone, Copy, Eq, PartialEq)]
struct Rect {}

Could be written as:

/// A rectangular region of space.
/// 
/// Usually a [`Rect`] has a positive (or zero) size,
/// and then [`Self::min`] `<=` [`Self::max`].
/// In these cases [`Self::min`] is the left-top corner
/// and [`Self::max`] is the right-bottom corner.
/// A rectangle is allowed to have a negative size, which happens when the order
/// of `min` and `max` are swapped. These are usually a sign of an error.
/// Normally the unit is points (logical pixels) in screen space coordinates.
/// `Rect` does NOT implement `Default`, because there is no obvious default value.
/// [`Rect::ZERO`] may seem reasonable, but when used as a bounding box, [`Rect::NOTHING`]
/// is a better default - so be explicit instead!
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Rect {

Note that we should suggest the added extra newline, as no doubt a lot of programmers aren't aware of this.

@emilk emilk added the A-lint Area: New lints label Jun 23, 2024
@GuillaumeGomez
Copy link
Member

This is a good point. Gonna take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants