1
- use std:: {
2
- ffi:: OsString ,
3
- path:: { Component , Path } ,
4
- } ;
5
-
6
1
use rustc_ast:: ast;
7
2
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
8
3
use rustc_lint:: { EarlyContext , EarlyLintPass , Level , LintContext } ;
9
4
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
10
5
use rustc_span:: { FileName , RealFileName , SourceFile , Span , SyntaxContext } ;
6
+ use std:: ffi:: OsStr ;
7
+ use std:: path:: { Component , Path } ;
11
8
12
9
declare_clippy_lint ! {
13
10
/// ### What it does
@@ -82,11 +79,7 @@ impl EarlyLintPass for ModStyle {
82
79
83
80
let files = cx. sess ( ) . source_map ( ) . files ( ) ;
84
81
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 } ;
90
83
91
84
// `folder_segments` is all unique folder path segments `path/to/foo.rs` gives
92
85
// `[path, to]` but not foo
@@ -97,26 +90,27 @@ impl EarlyLintPass for ModStyle {
97
90
// `{ foo => path/to/foo.rs, .. }
98
91
let mut file_map = FxHashMap :: default ( ) ;
99
92
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) ;
113
107
}
114
108
}
115
109
116
110
for folder in & folder_segments {
117
111
if !mod_folders. contains ( folder) {
118
112
if let Some ( ( file, path) ) = file_map. get ( folder) {
119
- let mut correct = path. clone ( ) ;
113
+ let mut correct = path. to_path_buf ( ) ;
120
114
correct. pop ( ) ;
121
115
correct. push ( folder) ;
122
116
correct. push ( "mod.rs" ) ;
@@ -138,25 +132,17 @@ impl EarlyLintPass for ModStyle {
138
132
139
133
/// For each `path` we add each folder component to `folder_segments` and if the file name
140
134
/// 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 > ,
145
139
) {
146
140
let mut comp = path. components ( ) . rev ( ) . peekable ( ) ;
147
141
let _ = comp. next ( ) ;
148
142
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 ( ) ) ;
150
144
}
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 } ) ;
160
146
folder_segments. extend ( folders) ;
161
147
}
162
148
0 commit comments