-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add tests_outside_test_module
lint
#10543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_note, is_in_cfg_test, is_in_test_function}; | ||
use rustc_hir::{intravisit::FnKind, Body, FnDecl}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::{def_id::LocalDefId, Span}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// Triggers when a testing function (marked with the `#[test]` attribute) isn't inside a testing module (marked with `#[cfg(test)]`). | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// The idiomatic (and more performant) way of writing tests is inside a testing module (flagged with `#[cfg(test)]`), having test functions outside of this module is confusing and may lead to them being "hidden". | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// #[test] | ||
/// fn my_cool_test() { | ||
/// // [...] | ||
/// } | ||
/// | ||
/// #[cfg(test)] | ||
/// mod tests { | ||
/// // [...] | ||
/// } | ||
/// | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// #[cfg(test)] | ||
/// mod tests { | ||
/// #[test] | ||
/// fn my_cool_test() { | ||
/// // [...] | ||
/// } | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.70.0"] | ||
pub TESTS_OUTSIDE_TEST_MODULE, | ||
restriction, | ||
"The test function `my_cool_test` is outside the testing module `tests`." | ||
blyxyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
declare_lint_pass!(TestsOutsideTestModule => [TESTS_OUTSIDE_TEST_MODULE]); | ||
|
||
impl LateLintPass<'_> for TestsOutsideTestModule { | ||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'_>, | ||
kind: FnKind<'_>, | ||
_: &FnDecl<'_>, | ||
body: &Body<'_>, | ||
sp: Span, | ||
_: LocalDefId, | ||
) { | ||
if_chain! { | ||
if !matches!(kind, FnKind::Closure); | ||
if is_in_test_function(cx.tcx, body.id().hir_id); | ||
if !is_in_cfg_test(cx.tcx, body.id().hir_id); | ||
then { | ||
span_lint_and_note( | ||
cx, | ||
TESTS_OUTSIDE_TEST_MODULE, | ||
sp, | ||
"this function marked with #[test] is outside a #[cfg(test)] module", | ||
None, | ||
"move it to a testing module marked with #[cfg(test)]", | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,18 @@ | ||
// compile-flags: --test | ||
#![allow(unused)] | ||
#![warn(clippy::tests_outside_test_module)] | ||
|
||
fn main() { | ||
// test code goes here | ||
} | ||
|
||
// Should lint | ||
#[test] | ||
fn my_test() {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
// Should not lint | ||
#[test] | ||
fn my_test() {} | ||
} |
This file contains hidden or 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,11 @@ | ||
error: this function marked with #[test] is outside a #[cfg(test)] module | ||
--> $DIR/tests_outside_test_module.rs:11:1 | ||
| | ||
LL | fn my_test() {} | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= note: move it to a testing module marked with #[cfg(test)] | ||
= note: `-D clippy::tests-outside-test-module` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.