about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-14 20:09:49 +0000
committerbors <bors@rust-lang.org>2022-09-14 20:09:49 +0000
commite585b71d9e3a9b95b5e8c3f01e0b3354b2be3ca6 (patch)
tree36aaf40fd616fd52f4336cd59e05597949dd692b
parentbae4699a9f6afc1c3c2e295e2ffa7d309f96f84d (diff)
parente9722feef35587a83a359dbc22bf515fae4ccf66 (diff)
downloadrust-e585b71d9e3a9b95b5e8c3f01e0b3354b2be3ca6.tar.gz
rust-e585b71d9e3a9b95b5e8c3f01e0b3354b2be3ca6.zip
Auto merge of #9475 - Nemo157:mod-files-remap, r=xFrednet
Make module-style lints resilient to --remap-path-prefix

changelog: [`self_named_module_files`], [`mod_module_files`]: Make module-style lints resilient to `--remap-path-prefix`

Without this if a user has configured `--remap-path-prefix` to be used for a prefix containing the current source directory the lints would silently fail to generate a warning.
-rw-r--r--clippy_lints/src/module_style.rs6
-rw-r--r--tests/ui-cargo/module_style/fail_mod_remap/Cargo.toml9
-rw-r--r--tests/ui-cargo/module_style/fail_mod_remap/src/bad.rs1
-rw-r--r--tests/ui-cargo/module_style/fail_mod_remap/src/bad/inner.rs1
-rw-r--r--tests/ui-cargo/module_style/fail_mod_remap/src/main.rs7
-rw-r--r--tests/ui-cargo/module_style/fail_mod_remap/src/main.stderr11
6 files changed, 32 insertions, 3 deletions
diff --git a/clippy_lints/src/module_style.rs b/clippy_lints/src/module_style.rs
index 0a393657267..22071ab3044 100644
--- a/clippy_lints/src/module_style.rs
+++ b/clippy_lints/src/module_style.rs
@@ -2,7 +2,7 @@ use rustc_ast::ast;
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
-use rustc_span::{FileName, RealFileName, SourceFile, Span, SyntaxContext};
+use rustc_span::{FileName, SourceFile, Span, SyntaxContext};
 use std::ffi::OsStr;
 use std::path::{Component, Path};
 
@@ -79,7 +79,7 @@ impl EarlyLintPass for ModStyle {
 
         let files = cx.sess().source_map().files();
 
-        let RealFileName::LocalPath(trim_to_src) = &cx.sess().opts.working_dir else { return };
+        let Some(trim_to_src) = cx.sess().opts.working_dir.local_path() else { return };
 
         // `folder_segments` is all unique folder path segments `path/to/foo.rs` gives
         // `[path, to]` but not foo
@@ -90,7 +90,7 @@ impl EarlyLintPass for ModStyle {
         // `{ foo => path/to/foo.rs, .. }
         let mut file_map = FxHashMap::default();
         for file in files.iter() {
-            if let FileName::Real(RealFileName::LocalPath(lp)) = &file.name {
+            if let FileName::Real(name) = &file.name && let Some(lp) = name.local_path() {
                 let path = if lp.is_relative() {
                     lp
                 } else if let Ok(relative) = lp.strip_prefix(trim_to_src) {
diff --git a/tests/ui-cargo/module_style/fail_mod_remap/Cargo.toml b/tests/ui-cargo/module_style/fail_mod_remap/Cargo.toml
new file mode 100644
index 00000000000..a822fad388f
--- /dev/null
+++ b/tests/ui-cargo/module_style/fail_mod_remap/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "fail-mod-remap"
+version = "0.1.0"
+edition = "2018"
+publish = false
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/tests/ui-cargo/module_style/fail_mod_remap/src/bad.rs b/tests/ui-cargo/module_style/fail_mod_remap/src/bad.rs
new file mode 100644
index 00000000000..509aad18622
--- /dev/null
+++ b/tests/ui-cargo/module_style/fail_mod_remap/src/bad.rs
@@ -0,0 +1 @@
+pub mod inner;
diff --git a/tests/ui-cargo/module_style/fail_mod_remap/src/bad/inner.rs b/tests/ui-cargo/module_style/fail_mod_remap/src/bad/inner.rs
new file mode 100644
index 00000000000..8b137891791
--- /dev/null
+++ b/tests/ui-cargo/module_style/fail_mod_remap/src/bad/inner.rs
@@ -0,0 +1 @@
+
diff --git a/tests/ui-cargo/module_style/fail_mod_remap/src/main.rs b/tests/ui-cargo/module_style/fail_mod_remap/src/main.rs
new file mode 100644
index 00000000000..ba4c8c873dd
--- /dev/null
+++ b/tests/ui-cargo/module_style/fail_mod_remap/src/main.rs
@@ -0,0 +1,7 @@
+// compile-flags: --remap-path-prefix {{src-base}}=/remapped
+
+#![warn(clippy::self_named_module_files)]
+
+mod bad;
+
+fn main() {}
diff --git a/tests/ui-cargo/module_style/fail_mod_remap/src/main.stderr b/tests/ui-cargo/module_style/fail_mod_remap/src/main.stderr
new file mode 100644
index 00000000000..46991ff662e
--- /dev/null
+++ b/tests/ui-cargo/module_style/fail_mod_remap/src/main.stderr
@@ -0,0 +1,11 @@
+error: `mod.rs` files are required, found `bad.rs`
+  --> /remapped/module_style/fail_mod_remap/src/bad.rs:1:1
+   |
+LL | pub mod inner;
+   | ^
+   |
+   = note: `-D clippy::self-named-module-files` implied by `-D warnings`
+   = help: move `bad.rs` to `bad/mod.rs`
+
+error: aborting due to previous error
+