rust-lang / rust-lang/rust

rustdoc: Emit a lint warning if doctest contains `#[test]` functions but isn't marked `test_harness`

Open
#157,458 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-doctests A-lints C-feature-request T-rustdoc
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=001bcadcd1efc090468ba6ce7231359b

/// ```
/// #[test]
/// fn my_test() {
///     panic!("this test should fail");
/// }
/// ```
const _: () = ();

I expected to see this happen: running cargo test would try to run my_test and report it failed.

Instead, this happened: it reports the doctest ran successfully.

I want to be able to use #[test] in a doctest to document things that are intended to only be used inside a #[test] (since they print errors to stdout/stderr and/or panic for better testing ergonomics, instead of returning a Result).

Meta

I tried it on 1.96.0 stable as well as 1.98.0-nightly (2026-06-03 b354133fb126352871be)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the linked doctest with cargo test and begin at rustdoc's doctest handling and diagnostic entry points. Trace how #[test] functions are treated when test_harness is absent; done means this case emits a lint warning and coverage verifies the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.