From 727baf0db196a6926e5ce755e73b5af5fd6ccf2a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 30 Nov 2021 14:25:55 -0500 Subject: [PATCH] When documenting private items in a binary, ignore warnings about links to private items Previously, rustdoc would warn about linking to items in a binary, even though cargo unconditionally documents private items in a binary. This changes cargo to silence the warning, since it's only relevant in cases where the private items might not be documented. --- src/cargo/ops/cargo_compile.rs | 6 ++++++ tests/testsuite/doc.rs | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 2a5d09f520b..4c5bee3b3fa 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -646,6 +646,12 @@ pub fn create_bcx<'a, 'cfg>( if rustdoc_document_private_items || unit.target.is_bin() { let mut args = extra_args.take().unwrap_or_default(); args.push("--document-private-items".into()); + if unit.target.is_bin() { + // This warning only makes sense if it's possible to document private items + // sometimes and ignore them at other times. But cargo consistently passes + // `--document-private-items`, so the warning isn't useful. + args.push("-Arustdoc::private-intra-doc-links".into()); + } extra_args = Some(args); } diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index dd6a3ec46ca..18882067bf9 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -2767,3 +2767,24 @@ fn doc_check_cfg_features() { ) .run(); } + +#[cargo_test] +fn link_to_private_item() { + let main = r#" + //! [bar] + #[allow(dead_code)] + fn bar() {} + "#; + let p = project().file("src/lib.rs", main).build(); + p.cargo("doc") + .with_stderr_contains("[..] documentation for `foo` links to private item `bar`") + .run(); + // Check that binaries don't emit a private_intra_doc_links warning. + fs::rename(p.root().join("src/lib.rs"), p.root().join("src/main.rs")).unwrap(); + p.cargo("doc") + .with_stderr( + "[DOCUMENTING] foo [..]\n\ + [FINISHED] [..]", + ) + .run(); +}