Skip to content

Commit baaddf2

Browse files
committed
Auto merge of #8611 - Alexendoo:module-files-relative-paths, r=llogiq
Handle relative paths in module_files lints The problem being that when clippy is run in the project's directory `lp` would be a relative path, this wasn't caught by the tests as there `lp` is an absolute path. Being a relative path it did not start with `trim_src_path` and so was ignored Also allowed the removal of some `.to_os_string`/`.to_owned`s changelog: Fixes [`self_named_module_files`] and [`mod_module_files`] not linting Fixes #8123, cc `@DevinR528`
2 parents f6b2992 + 10a6d87 commit baaddf2

File tree

3 files changed

+30
-44
lines changed

3 files changed

+30
-44
lines changed

clippy_lints/src/module_style.rs

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
use std::{
2-
ffi::OsString,
3-
path::{Component, Path},
4-
};
5-
61
use rustc_ast::ast;
72
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
83
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
94
use rustc_session::{declare_tool_lint, impl_lint_pass};
105
use rustc_span::{FileName, RealFileName, SourceFile, Span, SyntaxContext};
6+
use std::ffi::OsStr;
7+
use std::path::{Component, Path};
118

129
declare_clippy_lint! {
1310
/// ### What it does
@@ -82,11 +79,7 @@ impl EarlyLintPass for ModStyle {
8279

8380
let files = cx.sess().source_map().files();
8481

85-
let trim_to_src = if let RealFileName::LocalPath(p) = &cx.sess().opts.working_dir {
86-
p.to_string_lossy()
87-
} else {
88-
return;
89-
};
82+
let RealFileName::LocalPath(trim_to_src) = &cx.sess().opts.working_dir else { return };
9083

9184
// `folder_segments` is all unique folder path segments `path/to/foo.rs` gives
9285
// `[path, to]` but not foo
@@ -97,26 +90,27 @@ impl EarlyLintPass for ModStyle {
9790
// `{ foo => path/to/foo.rs, .. }
9891
let mut file_map = FxHashMap::default();
9992
for file in files.iter() {
100-
match &file.name {
101-
FileName::Real(RealFileName::LocalPath(lp))
102-
if lp.to_string_lossy().starts_with(trim_to_src.as_ref()) =>
103-
{
104-
let p = lp.to_string_lossy();
105-
let path = Path::new(p.trim_start_matches(trim_to_src.as_ref()));
106-
if let Some(stem) = path.file_stem() {
107-
file_map.insert(stem.to_os_string(), (file, path.to_owned()));
108-
}
109-
process_paths_for_mod_files(path, &mut folder_segments, &mut mod_folders);
110-
check_self_named_mod_exists(cx, path, file);
111-
},
112-
_ => {},
93+
if let FileName::Real(RealFileName::LocalPath(lp)) = &file.name {
94+
let path = if lp.is_relative() {
95+
lp
96+
} else if let Ok(relative) = lp.strip_prefix(trim_to_src) {
97+
relative
98+
} else {
99+
continue;
100+
};
101+
102+
if let Some(stem) = path.file_stem() {
103+
file_map.insert(stem, (file, path));
104+
}
105+
process_paths_for_mod_files(path, &mut folder_segments, &mut mod_folders);
106+
check_self_named_mod_exists(cx, path, file);
113107
}
114108
}
115109

116110
for folder in &folder_segments {
117111
if !mod_folders.contains(folder) {
118112
if let Some((file, path)) = file_map.get(folder) {
119-
let mut correct = path.clone();
113+
let mut correct = path.to_path_buf();
120114
correct.pop();
121115
correct.push(folder);
122116
correct.push("mod.rs");
@@ -138,25 +132,17 @@ impl EarlyLintPass for ModStyle {
138132

139133
/// For each `path` we add each folder component to `folder_segments` and if the file name
140134
/// is `mod.rs` we add it's parent folder to `mod_folders`.
141-
fn process_paths_for_mod_files(
142-
path: &Path,
143-
folder_segments: &mut FxHashSet<OsString>,
144-
mod_folders: &mut FxHashSet<OsString>,
135+
fn process_paths_for_mod_files<'a>(
136+
path: &'a Path,
137+
folder_segments: &mut FxHashSet<&'a OsStr>,
138+
mod_folders: &mut FxHashSet<&'a OsStr>,
145139
) {
146140
let mut comp = path.components().rev().peekable();
147141
let _ = comp.next();
148142
if path.ends_with("mod.rs") {
149-
mod_folders.insert(comp.peek().map(|c| c.as_os_str().to_owned()).unwrap_or_default());
143+
mod_folders.insert(comp.peek().map(|c| c.as_os_str()).unwrap_or_default());
150144
}
151-
let folders = comp
152-
.filter_map(|c| {
153-
if let Component::Normal(s) = c {
154-
Some(s.to_os_string())
155-
} else {
156-
None
157-
}
158-
})
159-
.collect::<Vec<_>>();
145+
let folders = comp.filter_map(|c| if let Component::Normal(s) = c { Some(s) } else { None });
160146
folder_segments.extend(folders);
161147
}
162148

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
error: `mod.rs` files are required, found `/bad/inner.rs`
1+
error: `mod.rs` files are required, found `bad/inner.rs`
22
--> $DIR/bad/inner.rs:1:1
33
|
44
LL | pub mod stuff;
55
| ^
66
|
77
= note: `-D clippy::self-named-module-files` implied by `-D warnings`
8-
= help: move `/bad/inner.rs` to `/bad/inner/mod.rs`
8+
= help: move `bad/inner.rs` to `bad/inner/mod.rs`
99

10-
error: `mod.rs` files are required, found `/bad/inner/stuff.rs`
10+
error: `mod.rs` files are required, found `bad/inner/stuff.rs`
1111
--> $DIR/bad/inner/stuff.rs:1:1
1212
|
1313
LL | pub mod most;
1414
| ^
1515
|
16-
= help: move `/bad/inner/stuff.rs` to `/bad/inner/stuff/mod.rs`
16+
= help: move `bad/inner/stuff.rs` to `bad/inner/stuff/mod.rs`
1717

1818
error: aborting due to 2 previous errors
1919

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: `mod.rs` files are not allowed, found `/bad/mod.rs`
1+
error: `mod.rs` files are not allowed, found `bad/mod.rs`
22
--> $DIR/bad/mod.rs:1:1
33
|
44
LL | pub struct Thing;
55
| ^
66
|
77
= note: `-D clippy::mod-module-files` implied by `-D warnings`
8-
= help: move `/bad/mod.rs` to `/bad.rs`
8+
= help: move `bad/mod.rs` to `bad.rs`
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)