Skip to content

Add new comment_within_doc lint #15260

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

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

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Jul 12, 2025

Fixes #15245.

Considering how many false positive we might trigger with this lint, I put it into pedantic.

cc @ojeda
r? @samueltardieu

changelog: Add new comment_within_doc lint

Summary Notes

Managed by @rustbot—see help for details

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 12, 2025

Some changes occurred in clippy_lints/src/doc

cc @notriddle

Copy link

github-actions bot commented Jul 12, 2025

Lintcheck changes for 938e647

Lint Added Removed Changed
clippy::comment_within_doc 3 0 0

This comment will be updated if you push new changes

@rustbot rustbot added S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work A-lint Area: New lints and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 12, 2025
@ojeda
Copy link
Contributor

ojeda commented Jul 12, 2025

If I understand correctly, this finds surrounding cases like:

/// ...
// ...
/// ...

But at least #15245 was intended to only lint on empty ones like:

/// ...
//
/// ...

The reason was to be more conservative to avoid false positives -- at least in the Linux kernel we expect to be able to write comments together to docs, and we already have 6 adjacent cases we would hit, e.g. one of them:

/// Use the given initializer to in-place initialize a `T`.
///
/// If `T: !Unpin` it will not be able to move afterwards.
// We don't implement `InPlaceInit` because `ListArc` is implicitly pinned. This is similar to
// what we do for `Arc`.
#[inline]
pub fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self, E>

Moreover, I think we should also probably want to lint on adjacent cases, not just surrounded cases, e.g.

/// ...
//

So I think there are these cases:

  • Surrounding, empty in all lines.
  • Surrounding, with some content in at least one line.
  • Adjacent, empty in all lines.
  • Adjacent, with some content in at least one line.

Of those, 1 and 3 seem both pretty safe to enable and that was my original intention with the issue.

4 is not something we would want at least for the Linux kernel, since we expect to write those (and we have 6 cases already).

And finally 3 could perhaps be interesting for some projects -- in the Linux kernel we don't have any case yet (if I grepped correctly), but when I wrote our coding guidelines about comments I expected it to be possible (https://docs.kernel.org/rust/coding-guidelines.html#comments):

/// # Examples
///
// TODO: Find a better example.
/// ```
/// let foo = f(42);
/// ```

Copy link
Contributor

@notriddle notriddle left a comment

Choose a reason for hiding this comment

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

Not sure if this us supposed to be empty comments only, or if it's supposed to be any comment sandwiched between doc comment lines.

I'm not sure if blank-lines-only would have enough true positives to justify maintaining it. Has this happened more than once?

@@ -0,0 +1,23 @@
#![warn(clippy::comment_within_doc)]

//! Hello//!/ oups
Copy link
Contributor

Choose a reason for hiding this comment

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

That "fix" is wrong, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint to detect empty comments within docs ("// within ///")
5 participants