From 6eddbb704ee4d80f18e6359c027e9f249ad2a7ae Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 11 Sep 2024 10:35:02 +0200 Subject: [PATCH 1/3] Fix false positive with `missing_docs` and `#[test]` Since #130025, the compiler don't ignore missing_docs when compiling the tests. But there is now a false positive warning for every `#[test]` For example, this code ```rust //! Crate docs fn just_a_test() {} ``` Would emit this warning when running `cargo test` ``` warning: missing documentation for a constant --> src/lib.rs:5:1 | 4 | #[test] | ------- in this procedural macro expansion 5 | fn just_a_test() {} | ^^^^^^^^^^^^^^^^^^^ ``` --- compiler/rustc_builtin_macros/src/test.rs | 2 ++ tests/pretty/tests-are-sorted.pp | 3 +++ tests/ui/lint/lint-missing-doc-test.rs | 3 +++ 3 files changed, 8 insertions(+) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 1b76a5f3234a2..ab3517d362721 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -277,6 +277,8 @@ pub(crate) fn expand_test_or_bench( cx.attr_nested_word(sym::cfg, sym::test, attr_sp), // #[rustc_test_marker = "test_case_sort_key"] cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), + // #[allow(missing_docs)] + cx.attr_nested_word(sym::allow, sym::missing_docs, attr_sp), ], // const $ident: test::TestDescAndFn = ast::ItemKind::Const( diff --git a/tests/pretty/tests-are-sorted.pp b/tests/pretty/tests-are-sorted.pp index a4b15dde4530e..0a31c3fc1b58f 100644 --- a/tests/pretty/tests-are-sorted.pp +++ b/tests/pretty/tests-are-sorted.pp @@ -12,6 +12,7 @@ extern crate test; #[cfg(test)] #[rustc_test_marker = "m_test"] +#[allow(missing_docs)] pub const m_test: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { @@ -36,6 +37,7 @@ extern crate test; #[cfg(test)] #[rustc_test_marker = "z_test"] +#[allow(missing_docs)] pub const z_test: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { @@ -61,6 +63,7 @@ extern crate test; #[cfg(test)] #[rustc_test_marker = "a_test"] +#[allow(missing_docs)] pub const a_test: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { diff --git a/tests/ui/lint/lint-missing-doc-test.rs b/tests/ui/lint/lint-missing-doc-test.rs index 93d4e4a44e928..466b79c0fb949 100644 --- a/tests/ui/lint/lint-missing-doc-test.rs +++ b/tests/ui/lint/lint-missing-doc-test.rs @@ -3,3 +3,6 @@ //@ check-pass //@ compile-flags: --test -Dmissing_docs + +#[test] +fn test() {} From 5d456dfaa1a712f1b1619c3b5a6f725776d5f101 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 11 Sep 2024 11:49:27 +0200 Subject: [PATCH 2/3] Use `#[doc(hidden)]` instead of `#[allow(missing_docs)]` on the const generated for `#[test]` --- compiler/rustc_builtin_macros/src/test.rs | 4 ++-- tests/pretty/tests-are-sorted.pp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index ab3517d362721..40dc75339efd7 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -277,8 +277,8 @@ pub(crate) fn expand_test_or_bench( cx.attr_nested_word(sym::cfg, sym::test, attr_sp), // #[rustc_test_marker = "test_case_sort_key"] cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), - // #[allow(missing_docs)] - cx.attr_nested_word(sym::allow, sym::missing_docs, attr_sp), + // #[doc(hidden)] + cx.attr_nested_word(sym::doc, sym::hidden, attr_sp), ], // const $ident: test::TestDescAndFn = ast::ItemKind::Const( diff --git a/tests/pretty/tests-are-sorted.pp b/tests/pretty/tests-are-sorted.pp index 0a31c3fc1b58f..d10368a89cbc7 100644 --- a/tests/pretty/tests-are-sorted.pp +++ b/tests/pretty/tests-are-sorted.pp @@ -12,7 +12,7 @@ extern crate test; #[cfg(test)] #[rustc_test_marker = "m_test"] -#[allow(missing_docs)] +#[doc(hidden)] pub const m_test: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { @@ -37,7 +37,7 @@ extern crate test; #[cfg(test)] #[rustc_test_marker = "z_test"] -#[allow(missing_docs)] +#[doc(hidden)] pub const z_test: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { @@ -63,7 +63,7 @@ extern crate test; #[cfg(test)] #[rustc_test_marker = "a_test"] -#[allow(missing_docs)] +#[doc(hidden)] pub const a_test: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { From cc34d64c511feaf576854797fc291e03f5f275a7 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 11 Sep 2024 12:08:14 +0200 Subject: [PATCH 3/3] Use `doc(hidden)` instead of `allow(missing_docs)` in the test harness So that it doesn't fail with `forbid(missing_docs)` Fixes #130218 --- compiler/rustc_builtin_macros/src/test_harness.rs | 6 +++--- compiler/rustc_span/src/symbol.rs | 1 - tests/pretty/tests-are-sorted.pp | 2 +- tests/ui/lint/lint-missing-doc-test.rs | 4 +++- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index a694d3b8c2857..400557ca260b2 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -326,8 +326,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P { let main_attr = ecx.attr_word(sym::rustc_main, sp); // #[coverage(off)] let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp); - // #[allow(missing_docs)] - let missing_docs_attr = ecx.attr_nested_word(sym::allow, sym::missing_docs, sp); + // #[doc(hidden)] + let doc_hidden_attr = ecx.attr_nested_word(sym::doc, sym::hidden, sp); // pub fn main() { ... } let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(ThinVec::new())); @@ -357,7 +357,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P { let main = P(ast::Item { ident: main_id, - attrs: thin_vec![main_attr, coverage_attr, missing_docs_attr], + attrs: thin_vec![main_attr, coverage_attr, doc_hidden_attr], id: ast::DUMMY_NODE_ID, kind: main, vis: ast::Visibility { span: sp, kind: ast::VisibilityKind::Public, tokens: None }, diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index d5240a05310b6..28d18f2dfcc15 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1236,7 +1236,6 @@ symbols! { mir_unwind_unreachable, mir_variant, miri, - missing_docs, mmx_reg, modifiers, module, diff --git a/tests/pretty/tests-are-sorted.pp b/tests/pretty/tests-are-sorted.pp index d10368a89cbc7..31449b51dc3e1 100644 --- a/tests/pretty/tests-are-sorted.pp +++ b/tests/pretty/tests-are-sorted.pp @@ -86,7 +86,7 @@ fn a_test() {} #[rustc_main] #[coverage(off)] -#[allow(missing_docs)] +#[doc(hidden)] pub fn main() -> () { extern crate test; test::test_main_static(&[&a_test, &m_test, &z_test]) diff --git a/tests/ui/lint/lint-missing-doc-test.rs b/tests/ui/lint/lint-missing-doc-test.rs index 466b79c0fb949..d86ec3525df36 100644 --- a/tests/ui/lint/lint-missing-doc-test.rs +++ b/tests/ui/lint/lint-missing-doc-test.rs @@ -2,7 +2,9 @@ //! on the generated test harness. //@ check-pass -//@ compile-flags: --test -Dmissing_docs +//@ compile-flags: --test + +#![forbid(missing_docs)] #[test] fn test() {}