From e446ed929ad4995dd0bd0b32b2e062efe3b7edb5 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 28 Jun 2025 10:13:21 +0200 Subject: [PATCH] clippy: replace vector indexing with safe .get() method --- src/uu/df/src/table.rs | 9 ++++++--- src/uu/expand/src/expand.rs | 6 +++--- src/uu/stat/src/stat.rs | 9 +++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/uu/df/src/table.rs b/src/uu/df/src/table.rs index 38fc69c8d..6e79c3758 100644 --- a/src/uu/df/src/table.rs +++ b/src/uu/df/src/table.rs @@ -447,8 +447,8 @@ impl fmt::Display for Table { while let Some((i, elem)) = col_iter.next() { let is_last_col = col_iter.peek().is_none(); - match self.alignments[i] { - Alignment::Left => { + match self.alignments.get(i) { + Some(Alignment::Left) => { if is_last_col { // no trailing spaces in last column write!(f, "{elem}")?; @@ -456,7 +456,10 @@ impl fmt::Display for Table { write!(f, "{: write!(f, "{:>width$}", elem, width = self.widths[i])?, + Some(Alignment::Right) => { + write!(f, "{:>width$}", elem, width = self.widths[i])?; + } + None => break, } if !is_last_col { diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index f8c5bf2cc..dd9d56c9a 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -381,10 +381,10 @@ fn expand_line( } } else { ( - match buf[byte] { + match buf.get(byte) { // always take exactly 1 byte in strict ASCII mode - 0x09 => Tab, - 0x08 => Backspace, + Some(0x09) => Tab, + Some(0x08) => Backspace, _ => Other, }, 1, diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 2d8871d40..87ceb166e 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -755,11 +755,11 @@ impl Stater { let chars = format_str.chars().collect::>(); let mut i = 0; while i < bound { - match chars[i] { - '%' => tokens.push(Self::handle_percent_case( + match chars.get(i) { + Some('%') => tokens.push(Self::handle_percent_case( &chars, &mut i, bound, format_str, )?), - '\\' => { + Some('\\') => { if use_printf { tokens.push(Self::handle_escape_sequences( &chars, &mut i, bound, format_str, @@ -768,7 +768,8 @@ impl Stater { tokens.push(Token::Char('\\')); } } - c => tokens.push(Token::Char(c)), + Some(c) => tokens.push(Token::Char(*c)), + None => break, } i += 1; }