Skip to content

Commit d10dd19

Browse files
berkusBerkus Karchebnyi
authored andcommitted
fix: Do not warn tests outside a module for integration tests
Fixes #11024
1 parent a411267 commit d10dd19

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

clippy_lints/src/tests_outside_test_module.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use rustc_hir::{Body, FnDecl};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
77
use rustc_span::def_id::LocalDefId;
8-
use rustc_span::Span;
8+
use rustc_span::source_map::SourceMap;
9+
use rustc_span::{FileName, RealFileName, Span};
10+
use std::path::Component;
911

1012
declare_clippy_lint! {
1113
/// ### What it does
@@ -15,6 +17,7 @@ declare_clippy_lint! {
1517
/// ### Why restrict this?
1618
/// The idiomatic (and more performant) way of writing tests is inside a testing module (flagged with `#[cfg(test)]`),
1719
/// having test functions outside of this module is confusing and may lead to them being "hidden".
20+
/// This does not apply to integration tests though, and this lint will ignore those.
1821
///
1922
/// ### Example
2023
/// ```no_run
@@ -59,6 +62,7 @@ impl LateLintPass<'_> for TestsOutsideTestModule {
5962
) {
6063
if !matches!(kind, FnKind::Closure)
6164
&& is_in_test_function(cx.tcx, body.id().hir_id)
65+
&& !is_integration_test(cx.tcx.sess.source_map(), sp)
6266
&& !is_in_cfg_test(cx.tcx, body.id().hir_id)
6367
{
6468
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
@@ -74,3 +78,12 @@ impl LateLintPass<'_> for TestsOutsideTestModule {
7478
}
7579
}
7680
}
81+
82+
fn is_integration_test(sm: &SourceMap, sp: Span) -> bool {
83+
match sm.span_to_filename(sp) {
84+
FileName::Real(RealFileName::LocalPath(name)) => {
85+
name.components().next() == Some(Component::Normal("tests".as_ref()))
86+
},
87+
_ => false,
88+
}
89+
}

tests/ui/tests_outside_test_module.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ fn main() {
55
// test code goes here
66
}
77

8-
// Should lint
8+
// Should not lint
9+
// Because we're inside an integration test
910
#[test]
1011
fn my_test() {}
11-
//~^ ERROR: this function marked with #[test] is outside a #[cfg(test)] module
12-
//~| NOTE: move it to a testing module marked with #[cfg(test)]
1312

1413
#[cfg(test)]
1514
mod tests {

tests/ui/tests_outside_test_module.stderr

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)