Skip to content

Commit

Permalink
ruff_python_parser: remove unchecked conversion to str
Browse files Browse the repository at this point in the history
While the usage looks correct, the use of `unsafe` here does not seem
justified to me. Namely, it's already doing integer parsing. And perhaps
most importantly, this is for parsing an octal literal which are likely
to be rare enough to not have a major impact on perf. (And it's not like
UTF-8 validation is slow.)

This was originally introduced in #8227 and it doesn't look like
unchecked string conversion was the main point there.
  • Loading branch information
BurntSushi committed Nov 10, 2023
1 parent 34d6bea commit f9ae93d
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions crates/ruff_python_parser/src/string.rs
Expand Up @@ -122,10 +122,8 @@ impl<'a> StringParser<'a> {
len += 1;
}

// SAFETY: radix_bytes is always going to be in the ASCII range.
#[allow(unsafe_code)]
let radix_str = unsafe { std::str::from_utf8_unchecked(&radix_bytes[..len]) };

// OK because radix_bytes is always going to be in the ASCII range.
let radix_str = std::str::from_utf8(&radix_bytes[..len]).expect("ASCII bytes");
let value = u32::from_str_radix(radix_str, 8).unwrap();
char::from_u32(value).unwrap()
}
Expand Down

0 comments on commit f9ae93d

Please sign in to comment.