Skip to content

test_attr_in_doctest emits when the test_harness attribute is present #16447

@epage

Description

@epage

Summary

The motivation for this lint is

Code in examples marked as #[test] will somewhat surprisingly not be run by cargo test. If you really want to show how to test stuff in an example, mark it no_run to make the intent clear.

But with the test_harness attribute, the tests will be run.

Lint Name

clippy::test_attr_in_doctest

Reproducer

I tried this code:

/// ```test_harness
/// #[test]
/// fn foo() {
///     panic!();
/// }
/// ```
pub fn foo() {}

I saw this happen:

warning: unit tests in doctest are not executed
 --> src/lib.rs:2:5
  |
2 | /// #[test]
  |     ^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#test_attr_in_do
ctest
  = note: `#[warn(clippy::test_attr_in_doctest)]` on by default

I expected to see this happen:

Version

rustc 1.93.0 (254b59607 2026-01-19)
binary: rustc
commit-hash: 254b59607d4417e9dffbc307138ae5c86280fe4c
commit-date: 2026-01-19
host: x86_64-unknown-linux-gnu
release: 1.93.0
LLVM version: 21.1.8

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions