@@ -8,8 +8,10 @@ use crate::util::interning::InternedString;
8
8
use crate :: util:: { human_readable_bytes, GlobalContext , Progress , ProgressStyle } ;
9
9
use anyhow:: bail;
10
10
use cargo_util:: paths;
11
+ use std:: collections:: HashMap ;
11
12
use std:: fs;
12
13
use std:: path:: { Path , PathBuf } ;
14
+ use std:: rc:: Rc ;
13
15
14
16
pub struct CleanOptions < ' gctx > {
15
17
pub gctx : & ' gctx GlobalContext ,
@@ -168,7 +170,7 @@ fn clean_specs(
168
170
let packages = pkg_set. get_many ( pkg_ids) ?;
169
171
170
172
clean_ctx. progress = Box :: new ( CleaningPackagesBar :: new ( clean_ctx. gctx , packages. len ( ) ) ) ;
171
-
173
+ let mut dirs_to_clean : HashMap < _ , HashSet < _ > > = HashMap :: new ( ) ;
172
174
for pkg in packages {
173
175
let pkg_dir = format ! ( "{}-*" , pkg. name( ) ) ;
174
176
clean_ctx. progress . on_cleaning_package ( & pkg. name ( ) ) ?;
@@ -193,8 +195,9 @@ fn clean_specs(
193
195
continue ;
194
196
}
195
197
let crate_name = target. crate_name ( ) ;
196
- let path_dot = format ! ( "{crate_name}." ) ;
197
- let path_dash = format ! ( "{crate_name}-" ) ;
198
+ let path_dot: Rc < str > = format ! ( "{crate_name}." ) . into ( ) ;
199
+ let path_dash: Rc < str > = format ! ( "{crate_name}-" ) . into ( ) ;
200
+
198
201
for & mode in & [
199
202
CompileMode :: Build ,
200
203
CompileMode :: Test ,
@@ -240,16 +243,16 @@ fn clean_specs(
240
243
// Remove split-debuginfo files generated by rustc.
241
244
242
245
let paths = [
243
- ( path_dash. as_str ( ) , ".d" ) ,
244
- ( path_dot. as_str ( ) , ".o" ) ,
245
- ( path_dot. as_str ( ) , ".dwo" ) ,
246
- ( path_dot. as_str ( ) , ".dwp" ) ,
246
+ ( path_dash. clone ( ) , ".d" ) ,
247
+ ( path_dot. clone ( ) , ".o" ) ,
248
+ ( path_dot. clone ( ) , ".dwo" ) ,
249
+ ( path_dot. clone ( ) , ".dwp" ) ,
247
250
] ;
248
251
if !dir_glob_str. ends_with ( std:: path:: MAIN_SEPARATOR ) {
249
252
dir_glob_str. push ( std:: path:: MAIN_SEPARATOR ) ;
250
253
}
251
254
dir_glob_str. push ( '*' ) ;
252
- clean_ctx . rm_rf_prefix_list ( & dir_glob_str. as_str ( ) , & paths) ? ;
255
+ dirs_to_clean . entry ( dir_glob_str) . or_default ( ) . extend ( paths) ;
253
256
254
257
// TODO: what to do about build_script_build?
255
258
let dir = escape_glob_path ( layout. incremental ( ) ) ?;
@@ -260,6 +263,9 @@ fn clean_specs(
260
263
}
261
264
}
262
265
266
+ for ( dir, paths) in dirs_to_clean {
267
+ clean_ctx. rm_rf_prefix_list ( & dir, & paths) ?;
268
+ }
263
269
Ok ( ( ) )
264
270
}
265
271
@@ -332,17 +338,16 @@ impl<'gctx> CleanContext<'gctx> {
332
338
fn rm_rf_prefix_list (
333
339
& mut self ,
334
340
pattern : & str ,
335
- path_matchers : & [ ( & str , & str ) ] ,
341
+ path_matchers : & HashSet < ( Rc < str > , & str ) > ,
336
342
) -> CargoResult < ( ) > {
337
343
// TODO: Display utf8 warning to user? Or switch to globset?
338
344
339
345
for path in glob:: glob ( pattern) ? {
340
346
let path = path?;
341
347
let filename = path. file_name ( ) . and_then ( |name| name. to_str ( ) ) . unwrap ( ) ;
342
- if path_matchers
343
- . iter ( )
344
- . any ( |( prefix, suffix) | filename. starts_with ( prefix) && filename. ends_with ( suffix) )
345
- {
348
+ if path_matchers. iter ( ) . any ( |( prefix, suffix) | {
349
+ filename. starts_with ( & * * prefix) && filename. ends_with ( suffix)
350
+ } ) {
346
351
self . rm_rf ( & path) ?;
347
352
}
348
353
}
0 commit comments