Skip to content

Commit f414b70

Browse files
authored
refactor: replace manual last().is_some_and() checks with pop_if() method (#11272)
Replace manual checks for last element existence and condition with the new pop_if() method for cleaner, more idiomatic code across multiple utilities (dd, nl, paste, shuf, uniq). This change improves code readability and reduces boilerplate while maintaining identical functionality.
1 parent 80ef0d7 commit f414b70

5 files changed

Lines changed: 6 additions & 19 deletions

File tree

src/uu/dd/src/blocks.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ fn block(buf: &[u8], cbs: usize, sync: bool, rstat: &mut ReadStat) -> Vec<Vec<u8
3939
// If `sync` is true and there has been at least one partial
4040
// record read from the input, then leave the all-spaces block at
4141
// the end. Otherwise, remove it.
42-
if let Some(last) = blocks.last() {
43-
if (!sync || rstat.reads_partial == 0) && last.iter().all(|&e| e == SPACE) {
44-
blocks.pop();
45-
}
46-
}
42+
let _ = blocks
43+
.pop_if(|last| (!sync || rstat.reads_partial == 0) && last.iter().all(|&e| e == SPACE));
4744

4845
blocks
4946
}

src/uu/nl/src/nl.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,7 @@ fn nl<T: Read>(reader: &mut BufReader<T>, stats: &mut Stats, settings: &Settings
395395
break;
396396
}
397397

398-
if line.last().copied() == Some(b'\n') {
399-
line.pop();
400-
}
398+
let _ = line.pop_if(|byte| *byte == b'\n');
401399

402400
if line.is_empty() {
403401
stats.consecutive_empty_lines += 1;

src/uu/paste/src/paste.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,7 @@ fn parse_delimiters(delimiters: &OsString) -> UResult<Box<[Box<[u8]>]>> {
273273
}
274274

275275
fn remove_trailing_line_ending_byte(line_ending_byte: u8, output: &mut Vec<u8>) {
276-
if let Some(&byte) = output.last() {
277-
if byte == line_ending_byte {
278-
assert_eq!(output.pop(), Some(line_ending_byte));
279-
}
280-
}
276+
let _ = output.pop_if(|byte| *byte == line_ending_byte);
281277
}
282278

283279
enum DelimiterState<'a> {

src/uu/shuf/src/shuf.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ fn split_seps(data: &[u8], sep: u8) -> Vec<&[u8]> {
276276
let predicted_capacity = data.len() / PREDICTED_LINE_LENGTH;
277277
let mut elements = Vec::with_capacity(predicted_capacity);
278278
elements.extend(data.split(|&b| b == sep));
279-
if elements.last().is_some_and(|e| e.is_empty()) {
280-
elements.pop();
281-
}
279+
let _ = elements.pop_if(|e| e.is_empty());
282280
elements
283281
}
284282

src/uu/uniq/src/uniq.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ impl Uniq {
247247
if bytes_read == 0 {
248248
return Ok(false);
249249
}
250-
if buffer.last().is_some_and(|last| *last == line_terminator) {
251-
buffer.pop();
252-
}
250+
let _ = buffer.pop_if(|last| *last == line_terminator);
253251
Ok(true)
254252
}
255253

0 commit comments

Comments
 (0)