about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-11-08 22:27:13 +0000
committerbors <bors@rust-lang.org>2023-11-08 22:27:13 +0000
commit34b7d1559f03f4a35316ed2a5261a87b591834bb (patch)
tree5caafd9a41e943eab4626a40b16eda817bdd0e58
parente16d42f5404ea8bbea0a5339bf3d68e58aa0a534 (diff)
parent7e716ff9551965c8b458e6a2a935a4a36463fb77 (diff)
downloadrust-34b7d1559f03f4a35316ed2a5261a87b591834bb.tar.gz
rust-34b7d1559f03f4a35316ed2a5261a87b591834bb.zip
Auto merge of #11779 - partiallytyped:11775, r=blyxyas
[`mod_module_files`] Don't emit lint for mod.rs in tests

fixes: #11775

current state: indiscriminately emits the lint for mod files in tests.

The following

```
tests/
  common/
    mod.rs
  test.rs
```

is a common pattern for code shared across the tests and is suggested in the rust book. The change adds an additional check to verify that the mod file is not in tests.

changelog: Fix [`mod_module_files`]: false positive for mod files in tests folder
-rw-r--r--clippy_lints/src/module_style.rs5
1 files changed, 4 insertions, 1 deletions
diff --git a/clippy_lints/src/module_style.rs b/clippy_lints/src/module_style.rs
index efdc7560ee4..b49a5614329 100644
--- a/clippy_lints/src/module_style.rs
+++ b/clippy_lints/src/module_style.rs
@@ -153,8 +153,11 @@ fn process_paths_for_mod_files<'a>(
 }
 
 /// Checks every path for the presence of `mod.rs` files and emits the lint if found.
+/// We should not emit a lint for test modules in the presence of `mod.rs`.
+/// Using `mod.rs` in integration tests is a [common pattern](https://doc.rust-lang.org/book/ch11-03-test-organization.html#submodules-in-integration-test)
+/// for code-sharing between tests.
 fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &SourceFile) {
-    if path.ends_with("mod.rs") {
+    if path.ends_with("mod.rs") && !path.starts_with("tests") {
         let mut mod_file = path.to_path_buf();
         mod_file.pop();
         mod_file.set_extension("rs");