Skip to content

Commit 6ed0f08

Browse files
committed
sort: refactor ordering_incompatible to use early returns
Simplify the function by computing mode_count directly and using early returns instead of accumulating a counter with nested if statements.
1 parent db1d57c commit 6ed0f08

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/uu/sort/src/sort.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -511,23 +511,22 @@ fn ordering_incompatible(
511511
dictionary_order: bool,
512512
ignore_non_printing: bool,
513513
) -> bool {
514-
let mut count = 0;
515-
if flags.numeric {
516-
count += 1;
517-
}
518-
if flags.general_numeric {
519-
count += 1;
520-
}
521-
if flags.human_numeric {
522-
count += 1;
523-
}
524-
if flags.month {
525-
count += 1;
514+
let mode_count = u8::from(flags.numeric)
515+
+ u8::from(flags.general_numeric)
516+
+ u8::from(flags.human_numeric)
517+
+ u8::from(flags.month);
518+
519+
// Multiple numeric/month modes are incompatible
520+
if mode_count > 1 {
521+
return true;
526522
}
527-
if flags.version || flags.random || dictionary_order || ignore_non_printing {
528-
count += 1;
523+
524+
// A numeric/month mode combined with version/random/dictionary/ignore_non_printing is incompatible
525+
if mode_count == 1 {
526+
return flags.version || flags.random || dictionary_order || ignore_non_printing;
529527
}
530-
count > 1
528+
529+
false
531530
}
532531

533532
fn incompatible_options_error(opts: &str) -> Box<dyn UError> {

0 commit comments

Comments
 (0)