-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #11872 - llogiq:test-attr-in-doctest, r=xFrednet
add lint against unit tests in doctests During RustLab, Alice Ryhl brought to my attention that the Andoid team stumbled over the fact that if one attempts to write a unit test within a doctest, it will be summarily ignored. So this lint should help people wondering why their tests won't run. --- changelog: New lint: [`test_attr_in_doctest`] [#11872](#11872)
- Loading branch information
Showing
6 changed files
with
172 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/// This is a test for `#[test]` in doctests | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #[test] | ||
/// fn should_be_linted() { | ||
/// assert_eq!(1, 1); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Make sure we catch multiple tests in one example, | ||
/// and show that we really parse the attr: | ||
/// | ||
/// ``` | ||
/// #[test] | ||
/// fn should_also_be_linted() { | ||
/// #[cfg(test)] | ||
/// assert!(true); | ||
/// } | ||
/// | ||
/// #[test] | ||
/// fn should_be_linted_too() { | ||
/// assert_eq!("#[test]", " | ||
/// #[test] | ||
/// "); | ||
/// } | ||
/// ``` | ||
/// | ||
/// We don't catch examples that aren't run: | ||
/// | ||
/// ```ignore | ||
/// #[test] | ||
/// fn ignored() { todo!() } | ||
/// ``` | ||
/// | ||
/// ```no_run | ||
/// #[test] | ||
/// fn ignored() { todo!() } | ||
/// ``` | ||
/// | ||
/// ```compile_fail | ||
/// #[test] | ||
/// fn ignored() { Err(()) } | ||
/// ``` | ||
/// | ||
/// ```txt | ||
/// #[test] | ||
/// fn not_even_rust() { panic!("Ouch") } | ||
/// ``` | ||
fn test_attr_in_doctests() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error: unit tests in doctest are not executed | ||
--> $DIR/test_attr_in_doctest.rs:6:5 | ||
| | ||
LL | /// #[test] | ||
| _____^ | ||
LL | | /// fn should_be_linted() { | ||
| |_______________________^ | ||
| | ||
= note: `-D clippy::test-attr-in-doctest` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::test_attr_in_doctest)]` | ||
|
||
error: unit tests in doctest are not executed | ||
--> $DIR/test_attr_in_doctest.rs:16:5 | ||
| | ||
LL | /// #[test] | ||
| _____^ | ||
LL | | /// fn should_also_be_linted() { | ||
| |____________________________^ | ||
|
||
error: unit tests in doctest are not executed | ||
--> $DIR/test_attr_in_doctest.rs:22:5 | ||
| | ||
LL | /// #[test] | ||
| _____^ | ||
LL | | /// fn should_be_linted_too() { | ||
| |___________________________^ | ||
|
||
error: aborting due to 3 previous errors | ||
|