From b0a1a4469ae3bcab01c999228e0c1e3e9506b43c Mon Sep 17 00:00:00 2001 From: avitex Date: Tue, 28 Dec 2021 21:37:08 +1100 Subject: [PATCH 1/2] Fix rustdoc::private_doc_tests lint for public re-exported items This involves changing the lint to check the access level is exported, rather than public. The exported access level accounts for public items and items accessible to other crates with the help of `pub use` re-exports. The pattern of re-exporting public items from a private module is usage seen in a number of popular crates. --- src/librustdoc/passes/check_doc_test_visibility.rs | 2 +- .../rustdoc-ui/public-reexported-item-doc-test.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc-ui/public-reexported-item-doc-test.rs diff --git a/src/librustdoc/passes/check_doc_test_visibility.rs b/src/librustdoc/passes/check_doc_test_visibility.rs index b86ec8abefa..e8f8ff988c1 100644 --- a/src/librustdoc/passes/check_doc_test_visibility.rs +++ b/src/librustdoc/passes/check_doc_test_visibility.rs @@ -131,7 +131,7 @@ fn add_test(&mut self, _: String, config: LangString, _: usize) { ); } } else if tests.found_tests > 0 - && !cx.cache.access_levels.is_public(item.def_id.expect_def_id()) + && !cx.cache.access_levels.is_exported(item.def_id.expect_def_id()) { cx.tcx.struct_span_lint_hir( crate::lint::PRIVATE_DOC_TESTS, diff --git a/src/test/rustdoc-ui/public-reexported-item-doc-test.rs b/src/test/rustdoc-ui/public-reexported-item-doc-test.rs new file mode 100644 index 00000000000..0398975f137 --- /dev/null +++ b/src/test/rustdoc-ui/public-reexported-item-doc-test.rs @@ -0,0 +1,14 @@ +// check-pass + +#![deny(rustdoc::private_doc_tests)] + +mod foo { + /// re-exported doc test + /// + /// ``` + /// assert!(true); + /// ``` + pub fn bar() {} +} + +pub use foo::bar; From 992646b9ebfc6eb6240ffaa2c2eec38f8502f168 Mon Sep 17 00:00:00 2001 From: avitex Date: Wed, 29 Dec 2021 00:53:12 +1100 Subject: [PATCH 2/2] Improve rustdoc::private_doc_tests tests - Ensure standard public items are accepted - Ensure public items not re-exported from private modules are denied --- .../rustdoc-ui/private-public-item-doc-test.rs | 11 +++++++++++ .../private-public-item-doc-test.stderr | 18 ++++++++++++++++++ .../public-reexported-item-doc-test.rs | 6 ++++-- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/test/rustdoc-ui/private-public-item-doc-test.rs create mode 100644 src/test/rustdoc-ui/private-public-item-doc-test.stderr diff --git a/src/test/rustdoc-ui/private-public-item-doc-test.rs b/src/test/rustdoc-ui/private-public-item-doc-test.rs new file mode 100644 index 00000000000..7cc62b38cc2 --- /dev/null +++ b/src/test/rustdoc-ui/private-public-item-doc-test.rs @@ -0,0 +1,11 @@ +#![deny(rustdoc::private_doc_tests)] + +mod foo { + /// private doc test + /// + /// ``` + /// assert!(false); + /// ``` + //~^^^^^ ERROR documentation test in private item + pub fn bar() {} +} diff --git a/src/test/rustdoc-ui/private-public-item-doc-test.stderr b/src/test/rustdoc-ui/private-public-item-doc-test.stderr new file mode 100644 index 00000000000..f50dbd1844e --- /dev/null +++ b/src/test/rustdoc-ui/private-public-item-doc-test.stderr @@ -0,0 +1,18 @@ +error: documentation test in private item + --> $DIR/private-public-item-doc-test.rs:4:5 + | +LL | / /// private doc test +LL | | /// +LL | | /// ``` +LL | | /// assert!(false); +LL | | /// ``` + | |___________^ + | +note: the lint level is defined here + --> $DIR/private-public-item-doc-test.rs:1:9 + | +LL | #![deny(rustdoc::private_doc_tests)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/rustdoc-ui/public-reexported-item-doc-test.rs b/src/test/rustdoc-ui/public-reexported-item-doc-test.rs index 0398975f137..b86a53305a1 100644 --- a/src/test/rustdoc-ui/public-reexported-item-doc-test.rs +++ b/src/test/rustdoc-ui/public-reexported-item-doc-test.rs @@ -2,7 +2,9 @@ #![deny(rustdoc::private_doc_tests)] -mod foo { +pub fn foo() {} + +mod private { /// re-exported doc test /// /// ``` @@ -11,4 +13,4 @@ mod foo { pub fn bar() {} } -pub use foo::bar; +pub use private::bar;