diff --git a/src/find/mod.rs b/src/find/mod.rs index ff047c82..0cab6e82 100644 --- a/src/find/mod.rs +++ b/src/find/mod.rs @@ -276,6 +276,13 @@ fn process_dir( matcher: &dyn matchers::Matcher, quit: &mut bool, ) -> i32 { + // No depth can be both >= min_depth and <= max_depth when min_depth exceeds + // max_depth, so nothing matches — the same as GNU find. `WalkDir` would + // otherwise still yield the max-depth entries, so short-circuit here. + if config.min_depth > config.max_depth { + return 0; + } + let mut walkdir = WalkDir::new(dir) .contents_first(config.depth_first) .max_depth(config.max_depth) @@ -738,6 +745,29 @@ mod tests { ); } + #[test] + fn find_mindepth_greater_than_maxdepth() { + // -mindepth greater than -maxdepth can never be satisfied, so find + // outputs nothing, matching GNU find (issue #778). + let deps = FakeDependencies::new(); + + let rc = find_main( + &[ + "find", + &fix_up_slashes("./test_data/depth"), + "-sorted", + "-mindepth", + "2", + "-maxdepth", + "1", + ], + &deps, + ); + + assert_eq!(rc, 0); + assert_eq!(deps.get_output_as_string(), ""); + } + #[test] fn find_maxdepth_depth_first() { let deps = FakeDependencies::new(); diff --git a/tests/test_find.rs b/tests/test_find.rs index b9c73d5f..b1cfc2b3 100644 --- a/tests/test_find.rs +++ b/tests/test_find.rs @@ -169,6 +169,16 @@ fn depth_rejects_signed_value() { } } +#[test] +fn mindepth_exceeds_maxdepth_outputs_nothing() { + // When -mindepth is greater than -maxdepth no entry can match, so find + // prints nothing and exits successfully, matching GNU find. + ucmd() + .args(&["./test_data/simple", "-mindepth", "2", "-maxdepth", "1"]) + .succeeds() + .no_stdout(); +} + #[test] fn multiple_matcher_failure() { ucmd()