PrimeIntellect/SWE-rebench-V2 huggingface dataset:
Repository: uutils/procps (rust)

Issue:
pgrep: -o/-n fail if pattern doesn't match
`pgrep` currently fails when specifying a non-matching pattern with `-o` or `-n`:
```
$ cargo run pgrep -o non_matching
thread 'main' panicked at src/uu/pgrep/src/pgrep.rs:220:34:
called `Option::unwrap()` on a `None` value

$ cargo run pgrep -n non_matching
thread 'main' panicked at src/uu/pgrep/src/pgrep.rs:218:35:
called `Option::unwrap()` on a `None` value
```
The expected behavior is no output and an exit code of `1`.

Pull request:
pgrep: make -o/-n work if pattern doesn't match
Fixes #120

Patch:
diff --git a/src/uu/pgrep/src/pgrep.rs b/src/uu/pgrep/src/pgrep.rs
index dd12de8..7a9ac31 100644
--- a/src/uu/pgrep/src/pgrep.rs
+++ b/src/uu/pgrep/src/pgrep.rs
@@ -69,13 +69,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
     // Collect pids
     let pids = {
         let mut pids = collect_matched_pids(&matches);
-        process_flag_o_n(&matches, &mut pids)
+        if pids.is_empty() {
+            uucore::error::set_exit_code(1);
+            pids
+        } else {
+            process_flag_o_n(&matches, &mut pids)
+        }
     };
 
-    if pids.is_empty() {
-        uucore::error::set_exit_code(1);
-    }
-
     // Processing output
     let output = if matches.get_flag("count") {
         format!("{}", pids.len())


Test patch:
diff --git a/tests/by-util/test_pgrep.rs b/tests/by-util/test_pgrep.rs
index e3dd052..1ccfdd5 100644
--- a/tests/by-util/test_pgrep.rs
+++ b/tests/by-util/test_pgrep.rs
@@ -60,6 +60,17 @@ fn test_oldest() {
     }
 }
 
+#[test]
+#[cfg(target_os = "linux")]
+fn test_oldest_non_matching_pattern() {
+    new_ucmd!()
+        .arg("--oldest")
+        .arg("non_matching")
+        .fails()
+        .code_is(1)
+        .no_output();
+}
+
 #[test]
 #[cfg(target_os = "linux")]
 fn test_newest() {
@@ -71,6 +82,17 @@ fn test_newest() {
     }
 }
 
+#[test]
+#[cfg(target_os = "linux")]
+fn test_newest_non_matching_pattern() {
+    new_ucmd!()
+        .arg("--newest")
+        .arg("non_matching")
+        .fails()
+        .code_is(1)
+        .no_output();
+}
+
 #[test]
 #[cfg(target_os = "linux")]
 fn test_full() {