From 19c7200d34907dbf0ea23f47ace32bd7fc6c8d85 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Thu, 24 Jul 2025 11:10:00 +0200 Subject: [PATCH] rm: improve is_dir_empty from O(n) to O(1) --- src/uu/rm/src/rm.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index 7cb5e7844..8ba07787c 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -373,10 +373,7 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool { /// `path` must be a directory. If there is an error reading the /// contents of the directory, this returns `false`. fn is_dir_empty(path: &Path) -> bool { - match fs::read_dir(path) { - Err(_) => false, - Ok(iter) => iter.count() == 0, - } + fs::read_dir(path).is_ok_and(|mut iter| iter.next().is_none()) } #[cfg(unix)]