3 path::{Component, Path},
7 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8 use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
9 use rustc_session::{declare_tool_lint, impl_lint_pass};
10 use rustc_span::{FileName, RealFileName, SourceFile, Span, SyntaxContext};
12 declare_clippy_lint! {
14 /// Checks that module layout uses only self named module files, bans mod.rs files.
16 /// ### Why is this bad?
17 /// Having multiple module layout styles in a project can be confusing.
37 "checks that module layout is consistent"
40 declare_clippy_lint! {
42 /// Checks that module layout uses only mod.rs files.
44 /// ### Why is this bad?
45 /// Having multiple module layout styles in a project can be confusing.
64 pub SELF_NAMED_MODULE_FILES,
66 "checks that module layout is consistent"
71 impl_lint_pass!(ModStyle => [MOD_MODULE_FILES, SELF_NAMED_MODULE_FILES]);
73 impl EarlyLintPass for ModStyle {
74 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
75 if cx.builder.lint_level(MOD_MODULE_FILES).0 == Level::Allow
76 && cx.builder.lint_level(SELF_NAMED_MODULE_FILES).0 == Level::Allow
81 let files = cx.sess.source_map().files();
83 let trim_to_src = if let RealFileName::LocalPath(p) = &cx.sess.opts.working_dir {
89 // `folder_segments` is all unique folder path segments `path/to/foo.rs` gives
90 // `[path, to]` but not foo
91 let mut folder_segments = FxHashSet::default();
92 // `mod_folders` is all the unique folder names that contain a mod.rs file
93 let mut mod_folders = FxHashSet::default();
94 // `file_map` maps file names to the full path including the file name
95 // `{ foo => path/to/foo.rs, .. }
96 let mut file_map = FxHashMap::default();
97 for file in files.iter() {
99 FileName::Real(RealFileName::LocalPath(lp))
100 if lp.to_string_lossy().starts_with(trim_to_src.as_ref()) =>
102 let p = lp.to_string_lossy();
103 let path = Path::new(p.trim_start_matches(trim_to_src.as_ref()));
104 if let Some(stem) = path.file_stem() {
105 file_map.insert(stem.to_os_string(), (file, path.to_owned()));
107 process_paths_for_mod_files(path, &mut folder_segments, &mut mod_folders);
108 check_self_named_mod_exists(cx, path, file);
114 for folder in &folder_segments {
115 if !mod_folders.contains(folder) {
116 if let Some((file, path)) = file_map.get(folder) {
117 let mut correct = path.clone();
119 correct.push(folder);
120 correct.push("mod.rs");
122 SELF_NAMED_MODULE_FILES,
123 Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
126 build.build(&format!("`mod.rs` files are required, found `{}`", path.display()));
127 lint.help(&format!("move `{}` to `{}`", path.display(), correct.display(),));
137 /// For each `path` we add each folder component to `folder_segments` and if the file name
138 /// is `mod.rs` we add it's parent folder to `mod_folders`.
139 fn process_paths_for_mod_files(
141 folder_segments: &mut FxHashSet<OsString>,
142 mod_folders: &mut FxHashSet<OsString>,
144 let mut comp = path.components().rev().peekable();
146 if path.ends_with("mod.rs") {
147 mod_folders.insert(comp.peek().map(|c| c.as_os_str().to_owned()).unwrap_or_default());
151 if let Component::Normal(s) = c {
152 Some(s.to_os_string())
157 .collect::<Vec<_>>();
158 folder_segments.extend(folders);
161 /// Checks every path for the presence of `mod.rs` files and emits the lint if found.
162 fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &SourceFile) {
163 if path.ends_with("mod.rs") {
164 let mut mod_file = path.to_path_buf();
166 mod_file.set_extension("rs");
170 Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
172 let mut lint = build.build(&format!("`mod.rs` files are not allowed, found `{}`", path.display()));
173 lint.help(&format!("move `{}` to `{}`", path.display(), mod_file.display(),));