1- use std:: {
2- ffi:: OsString ,
3- path:: { Component , Path } ,
4- } ;
5-
61use rustc_ast:: ast;
72use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
83use rustc_lint:: { EarlyContext , EarlyLintPass , Level , LintContext } ;
94use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
105use rustc_span:: { FileName , RealFileName , SourceFile , Span , SyntaxContext } ;
6+ use std:: ffi:: OsStr ;
7+ use std:: path:: { Component , Path } ;
118
129declare_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
0 commit comments