Skip to content

Commit 0a9eb84

Browse files
author
Berkus Karchebnyi
committed
fix: Adjust matching code based on review feedback
1 parent d10dd19 commit 0a9eb84

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

clippy_lints/src/tests_outside_test_module.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
77
use rustc_span::def_id::LocalDefId;
88
use rustc_span::source_map::SourceMap;
9-
use rustc_span::{FileName, RealFileName, Span};
10-
use std::path::Component;
9+
use rustc_span::{FileName, Span};
10+
use std::path::PathBuf;
1111

1212
declare_clippy_lint! {
1313
/// ### What it does
@@ -80,10 +80,22 @@ impl LateLintPass<'_> for TestsOutsideTestModule {
8080
}
8181

8282
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-
}
83+
// This part is from https://github.com/rust-lang/rust/blob/a91f7d72f12efcc00ecf71591f066c534d45ddf7/compiler/rustc_expand/src/expand.rs#L402-L409 (fn expand_crate)
84+
// Extract crate base path from the filename path.
85+
let file_path = match sm.span_to_filename(sp) {
86+
FileName::Real(name) => name
87+
.into_local_path()
88+
.expect("attempting to resolve a file path in an external file"),
89+
other => PathBuf::from(other.prefer_local().to_string()),
90+
};
91+
92+
// Root path contains the topmost sources directory of the crate.
93+
// I.e., for `project` with sources in `src` and tests in `tests` folders
94+
// (no matter how many nested folders lie inside),
95+
// there will be two different root paths: `/project/src` and `/project/tests`.
96+
let root_path = file_path.parent().unwrap_or(&file_path).to_owned();
97+
98+
// The next part matches logic in https://github.com/rust-lang/rust/blob/a91f7d72f12efcc00ecf71591f066c534d45ddf7/compiler/rustc_builtin_macros/src/test.rs#L526 (fn test_type)
99+
// Integration tests are under /tests directory in the crate root_path we determined above.
100+
root_path.ends_with("tests")
89101
}

0 commit comments

Comments
 (0)