Skip to content

Commit

Permalink
Auto merge of #91241 - dtolnay:firstchunk, r=oli-obk
Browse files Browse the repository at this point in the history
Eliminate an unreachable codepath from String::from_utf8_lossy

`Utf8Lossy`'s `Iterator` implementation ensures that only the **final** chunk has an empty slice for `broken`:

https://github.com/rust-lang/rust/blob/dd549dcab404ec4c7d07b5a83aca5bdd7171138f/library/core/src/str/lossy.rs#L46-L47

Thus the only way the **first** chunk could have an empty `broken` is if it is the **final** chunk, i.e. there is only one chunk total. And the only way that there could be one chunk total with an empty `broken` is if the whole input is valid utf8 and non-empty.

That condition has already been handled by an early return, so at the point that the first `REPLACEMENT` is being pushed, it's impossible for `first_broken` to be empty.
  • Loading branch information
bors committed Nov 27, 2021
2 parents 686e313 + 9125dd7 commit 4919988
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,13 @@ impl String {
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
let mut iter = lossy::Utf8Lossy::from_bytes(v).chunks();

let (first_valid, first_broken) = if let Some(chunk) = iter.next() {
let first_valid = if let Some(chunk) = iter.next() {
let lossy::Utf8LossyChunk { valid, broken } = chunk;
if valid.len() == v.len() {
debug_assert!(broken.is_empty());
if broken.is_empty() {
debug_assert_eq!(valid.len(), v.len());
return Cow::Borrowed(valid);
}
(valid, broken)
valid
} else {
return Cow::Borrowed("");
};
Expand All @@ -573,9 +573,7 @@ impl String {

let mut res = String::with_capacity(v.len());
res.push_str(first_valid);
if !first_broken.is_empty() {
res.push_str(REPLACEMENT);
}
res.push_str(REPLACEMENT);

for lossy::Utf8LossyChunk { valid, broken } in iter {
res.push_str(valid);
Expand Down

0 comments on commit 4919988

Please sign in to comment.