From a87b88b5ca8694b1a2ce4df654321eb7eea7c628 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 10 Nov 2023 09:10:31 -0500 Subject: [PATCH] ruff_python_parser: remove unchecked conversion to str 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. --- crates/ruff_python_parser/src/string.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index 2d64e66bd2ec4d..2d4f2c5df926a3 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -120,10 +120,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() }