Skip to content

diagnostics: emit unused_doc_comments on fn nest#158514

Open
notriddle wants to merge 3 commits into
rust-lang:mainfrom
notriddle:unused-doc-comments-nested
Open

diagnostics: emit unused_doc_comments on fn nest#158514
notriddle wants to merge 3 commits into
rust-lang:mainfrom
notriddle:unused-doc-comments-nested

Conversation

@notriddle

Copy link
Copy Markdown
Contributor

When a doc comment is nested within an item:

fn foo() {
    /// doc comment
    fn bar() {}
}

When that happens, emit a warning, because they aren't rendered, even with --document-private-items

warning: unused doc comment
 --> src/lib.rs:3:3
  |
3 |     /// doc comment
  |     ^^^^^^^^^^^^^^^
4 |     fn bar() {}
  |     ----------- rustdoc does not generate documentation for items defined in functions
  |
  = help: use `//` for a plain comment
  = note: `#[warn(unused_doc_comments)]` (part of `#[warn(unused)]`) on by default

Fixes #158513

When a doc comment is nested within an item:

    fn foo() {
        /// doc comment
        fn bar() {}
    }

When that happens, emit a warning, because they aren't rendered, even
with --document-private-items

    warning: unused doc comment
     --> src/lib.rs:3:3
      |
    3 |     /// nested fn
      |     ^^^^^^^^^^^^^
    4 |     pub fn bar() {
      |     -------------- rustdoc does not generate documentation for items defined in functions
      |
      = help: use `//` for a plain comment
      = note: `#[warn(unused_doc_comments)]` (part of `#[warn(unused)]`) on by default
@rustbot

rustbot commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

compiler-builtins is developed in its own repository. If possible, consider making this change to rust-lang/compiler-builtins instead.

cc @tgross35

@rustbot rustbot added A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jun 28, 2026
@rustbot

rustbot commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@notriddle notriddle removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) labels Jun 28, 2026
@notriddle

Copy link
Copy Markdown
Contributor Author

r? rustdoc

@rustbot rustbot assigned GuillaumeGomez and unassigned jackh726 Jun 28, 2026
@rust-log-analyzer

This comment has been minimized.

@notriddle notriddle force-pushed the unused-doc-comments-nested branch from fe51138 to 9d780f5 Compare June 28, 2026 08:16
@rustbot rustbot added the A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) label Jun 28, 2026
@rust-log-analyzer

This comment has been minimized.

@notriddle notriddle force-pushed the unused-doc-comments-nested branch from 9d780f5 to 7a91d72 Compare June 28, 2026 08:38
@rust-log-analyzer

This comment has been minimized.

}

fn main() {}
fn main() {

@GuillaumeGomez GuillaumeGomez Jun 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a test for an impl inside a block to ensure it's not emitted for this case

View changes since the review

@GuillaumeGomez GuillaumeGomez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks all good, just one missing testcase.

View changes since this review

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 28, 2026
@rustbot

rustbot commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 28, 2026
@notriddle notriddle force-pushed the unused-doc-comments-nested branch from 7a91d72 to 9ff4703 Compare June 28, 2026 09:02

@fmease fmease Jun 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The same holds for const items:

pub const X: () = { /** X */ pub fn x() {} };
const _: () = { /** X */ pub fn x() {} };

Ideally we would lint here, too, wouldn't we?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or rather anonymous constants in general:

type X = [(); { /** X */ pub fn x() {} 0 }];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but be careful about impls where we shouldn't fire even if nested in an anonymous constant / constant item:

const _: () = { /** X */ impl Trait for () {} };
pub trait Trait {}

Above, the doc comment does get rendered.

fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
if let ast::ItemKind::ForeignMod(_) = item.kind {
warn_if_doc(cx, item.span, "extern blocks", &item.attrs);
} else if self.in_fn != 0 {

@fmease fmease Jun 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this introduces false positives with impl blocks. Does your PR now warn on the following?

fn f() { /** X */ impl Trait for () {} }
pub trait Trait {}

Above, the doc comment does get rendered.

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@notriddle notriddle force-pushed the unused-doc-comments-nested branch from 9ff4703 to 93b8b3d Compare June 28, 2026 09:21
@rustbot

rustbot commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@RalfJung

RalfJung commented Jun 28, 2026

Copy link
Copy Markdown
Member

I don't think we should warn here. These doc comments are still useful: they are rendered by RA when hovering a call site. IOW they are pretty much exactly like a doc comment for a private item, and we don't warn against those either.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  --> compiler/rustc_ty_utils/src/layout/invariant.rs:38:5
   |
38 |       /// Yields non-ZST fields of the type
   |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39 | /     fn non_zst_fields<'tcx, 'a>(
40 | |         cx: &'a LayoutCx<'tcx>,
41 | |         layout: &'a TyAndLayout<'tcx>,
42 | |     ) -> impl Iterator<Item = (Size, TyAndLayout<'tcx>)> {
...  |
51 | |         })
52 | |     }
   | |_____- rustdoc does not generate documentation for items defined in functions
   |
---
   Compiling rustc_codegen_ssa v0.0.0 (/checkout/compiler/rustc_codegen_ssa)
warning: unused doc comment
   --> compiler/rustc_codegen_ssa/src/back/metadata.rs:425:5
    |
425 | /     /// The `object` crate demands "X.Y.Z encoded in nibbles as xxxx.yy.zz"
426 | |     /// e.g. minOS 14.0 = 0x000E0000, or SDK 16.2 = 0x00100200
    | |______________________________________________________________^
427 | /     fn pack_version(apple::OSVersion { major, minor, patch }: apple::OSVersion) -> u32 {
428 | |         let (major, minor, patch) = (major as u32, minor as u32, patch as u32);
429 | |         (major << 16) | (minor << 8) | patch
430 | |     }
    | |_____- rustdoc does not generate documentation for items defined in functions
    |
    = help: use `//` for a plain comment
    = note: `#[warn(unused_doc_comments)]` (part of `#[warn(unused)]`) on by default
---

warning: unused doc comment
   --> compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs:464:5
    |
464 |       /// MSVC names enums differently than other platforms so that the debugging visualization
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
468 | /     fn msvc_enum_fallback<'tcx>(
469 | |         tcx: TyCtxt<'tcx>,
470 | |         ty_and_layout: TyAndLayout<'tcx>,
471 | |         push_inner: &dyn Fn(/*output*/ &mut String, /*visited*/ &mut FxHashSet<Ty<'tcx>>),
...   |
478 | |         push_close_angle_bracket(true, output);
479 | |     }
    | |_____- rustdoc does not generate documentation for items defined in functions
    |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unused_doc_comment doesn't fire on fn-nested doc attributes

7 participants