2 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3 use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
4 use rustc_session::{declare_tool_lint, impl_lint_pass};
5 use rustc_span::{FileName, RealFileName, SourceFile, Span, SyntaxContext};
7 use std::path::{Component, Path};
11 /// Checks that module layout uses only self named module files, bans mod.rs files.
13 /// ### Why is this bad?
14 /// Having multiple module layout styles in a project can be confusing.
32 #[clippy::version = "1.57.0"]
35 "checks that module layout is consistent"
38 declare_clippy_lint! {
40 /// Checks that module layout uses only mod.rs files.
42 /// ### Why is this bad?
43 /// Having multiple module layout styles in a project can be confusing.
62 #[clippy::version = "1.57.0"]
63 pub SELF_NAMED_MODULE_FILES,
65 "checks that module layout is consistent"
70 impl_lint_pass!(ModStyle => [MOD_MODULE_FILES, SELF_NAMED_MODULE_FILES]);
72 impl EarlyLintPass for ModStyle {
73 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
74 if cx.builder.lint_level(MOD_MODULE_FILES).0 == Level::Allow
75 && cx.builder.lint_level(SELF_NAMED_MODULE_FILES).0 == Level::Allow
80 let files = cx.sess().source_map().files();
82 let RealFileName::LocalPath(trim_to_src) = &cx.sess().opts.working_dir else { return };
84 // `folder_segments` is all unique folder path segments `path/to/foo.rs` gives
85 // `[path, to]` but not foo
86 let mut folder_segments = FxHashSet::default();
87 // `mod_folders` is all the unique folder names that contain a mod.rs file
88 let mut mod_folders = FxHashSet::default();
89 // `file_map` maps file names to the full path including the file name
90 // `{ foo => path/to/foo.rs, .. }
91 let mut file_map = FxHashMap::default();
92 for file in files.iter() {
93 if let FileName::Real(RealFileName::LocalPath(lp)) = &file.name {
94 let path = if lp.is_relative() {
96 } else if let Ok(relative) = lp.strip_prefix(trim_to_src) {
102 if let Some(stem) = path.file_stem() {
103 file_map.insert(stem, (file, path));
105 process_paths_for_mod_files(path, &mut folder_segments, &mut mod_folders);
106 check_self_named_mod_exists(cx, path, file);
110 for folder in &folder_segments {
111 if !mod_folders.contains(folder) {
112 if let Some((file, path)) = file_map.get(folder) {
113 let mut correct = path.to_path_buf();
115 correct.push(folder);
116 correct.push("mod.rs");
118 SELF_NAMED_MODULE_FILES,
119 Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
122 build.build(&format!("`mod.rs` files are required, found `{}`", path.display()));
123 lint.help(&format!("move `{}` to `{}`", path.display(), correct.display(),));
133 /// For each `path` we add each folder component to `folder_segments` and if the file name
134 /// is `mod.rs` we add it's parent folder to `mod_folders`.
135 fn process_paths_for_mod_files<'a>(
137 folder_segments: &mut FxHashSet<&'a OsStr>,
138 mod_folders: &mut FxHashSet<&'a OsStr>,
140 let mut comp = path.components().rev().peekable();
142 if path.ends_with("mod.rs") {
143 mod_folders.insert(comp.peek().map(|c| c.as_os_str()).unwrap_or_default());
145 let folders = comp.filter_map(|c| if let Component::Normal(s) = c { Some(s) } else { None });
146 folder_segments.extend(folders);
149 /// Checks every path for the presence of `mod.rs` files and emits the lint if found.
150 fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &SourceFile) {
151 if path.ends_with("mod.rs") {
152 let mut mod_file = path.to_path_buf();
154 mod_file.set_extension("rs");
158 Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
160 let mut lint = build.build(&format!("`mod.rs` files are not allowed, found `{}`", path.display()));
161 lint.help(&format!("move `{}` to `{}`", path.display(), mod_file.display(),));