@@ -500,50 +500,48 @@ fn prompt_file(path: &Path, options: &Options) -> bool {
500
500
}
501
501
}
502
502
}
503
+
503
504
// File::open(path) doesn't open the file in write mode so we need to use file options to open it in also write mode to check if it can written too
504
505
match File :: options ( ) . read ( true ) . write ( true ) . open ( path) {
505
506
Ok ( file) => {
506
- if file. metadata ( ) . is_err ( ) {
507
+ let Ok ( metadata ) = file. metadata ( ) else {
507
508
return true ;
508
- }
509
+ } ;
509
510
510
- let metadata = file. metadata ( ) . unwrap ( ) ;
511
- if metadata. permissions ( ) . readonly ( ) {
512
- if metadata. len ( ) == 0 {
513
- prompt_yes ! (
514
- "remove write-protected regular empty file {}?" ,
515
- path. quote( )
516
- )
517
- } else {
518
- prompt_yes ! ( "remove write-protected regular file {}?" , path. quote( ) )
519
- }
520
- } else if options. interactive == InteractiveMode :: Always {
521
- if metadata. len ( ) == 0 {
511
+ if options. interactive == InteractiveMode :: Always && !metadata. permissions ( ) . readonly ( )
512
+ {
513
+ return if metadata. len ( ) == 0 {
522
514
prompt_yes ! ( "remove regular empty file {}?" , path. quote( ) )
523
515
} else {
524
516
prompt_yes ! ( "remove file {}?" , path. quote( ) )
525
- }
526
- } else {
527
- true
517
+ } ;
528
518
}
519
+ prompt_file_permission_readonly ( path, Ok ( metadata) )
529
520
}
530
521
Err ( err) => {
531
522
if err. kind ( ) != ErrorKind :: PermissionDenied {
532
523
return true ;
533
524
}
534
- match fs:: metadata ( path) {
535
- Ok ( metadata) if metadata. len ( ) == 0 => {
536
- prompt_yes ! (
537
- "remove write-protected regular empty file {}?" ,
538
- path. quote( )
539
- )
540
- }
541
- _ => prompt_yes ! ( "remove write-protected regular file {}?" , path. quote( ) ) ,
542
- }
525
+ prompt_file_permission_readonly ( path, fs:: metadata ( path) )
543
526
}
544
527
}
545
528
}
546
529
530
+ #[ allow( clippy:: cognitive_complexity) ]
531
+ fn prompt_file_permission_readonly (
532
+ path : & Path ,
533
+ metadata_or_err : Result < Metadata , std:: io:: Error > ,
534
+ ) -> bool {
535
+ match metadata_or_err {
536
+ Ok ( metadata) if !metadata. permissions ( ) . readonly ( ) => true ,
537
+ Ok ( metadata) if metadata. len ( ) == 0 => prompt_yes ! (
538
+ "remove write-protected regular empty file {}?" ,
539
+ path. quote( )
540
+ ) ,
541
+ _ => prompt_yes ! ( "remove write-protected regular file {}?" , path. quote( ) ) ,
542
+ }
543
+ }
544
+
547
545
// For directories finding if they are writable or not is a hassle. In Unix we can use the built-in rust crate to to check mode bits. But other os don't have something similar afaik
548
546
// Most cases are covered by keep eye out for edge cases
549
547
#[ cfg( unix) ]
0 commit comments