Skip to content

Commit 6ef58ef

Browse files
committed
treefile: Fix clippy char_indices_as_byte_indices lint
The split_whitespace_unless_quoted function was using chars().enumerate() which returns character indices, but then using those indices to slice the string which requires byte indices. This triggers the char_indices_as_byte_indices clippy lint. Fixed by using char_indices() instead, which provides byte indices directly. Also refactored the end-of-string handling to occur after the loop completes, which is clearer and handles the remaining text properly regardless of multi-byte characters. Assisted-by: Claude Code (Sonnet 4.5) Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 4b099f9 commit 6ef58ef

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

rust/src/treefile.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ fn split_whitespace_unless_quoted(element: &str) -> Result<impl Iterator<Item =
20862086
let mut ret = vec![];
20872087
let mut start_index = 0;
20882088
let mut looping_over_quoted_pkg = false;
2089-
for (i, c) in element.chars().enumerate() {
2089+
for (i, c) in element.char_indices() {
20902090
if c == '\'' {
20912091
if looping_over_quoted_pkg {
20922092
ret.push(&element[start_index..i]);
@@ -2095,15 +2095,16 @@ fn split_whitespace_unless_quoted(element: &str) -> Result<impl Iterator<Item =
20952095
ret.extend(element[start_index..i].split_whitespace());
20962096
looping_over_quoted_pkg = true;
20972097
}
2098-
start_index = i + 1;
2099-
}
2100-
if i == element.len() - 1 {
2101-
if looping_over_quoted_pkg {
2102-
bail!("Missing terminating quote: {}", element);
2103-
}
2104-
ret.extend(element[start_index..].split_whitespace());
2098+
start_index = i + c.len_utf8();
21052099
}
21062100
}
2101+
// Handle any remaining text after the loop
2102+
if looping_over_quoted_pkg {
2103+
bail!("Missing terminating quote: {}", element);
2104+
}
2105+
if start_index < element.len() {
2106+
ret.extend(element[start_index..].split_whitespace());
2107+
}
21072108

21082109
Ok(ret.into_iter())
21092110
}

0 commit comments

Comments
 (0)