From ef1e2228cfd9df4059aa44740b0659fea7c5a52f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 23 Jan 2024 10:37:27 +1100 Subject: [PATCH] Use `from` instead of `into` in unescaping code. The `T` type in these functions took me some time to understand, and I find the explicit `T` in the use of `from` makes the code easier to read, as does the `u8` annotation in `scan_escape`. --- compiler/rustc_lexer/src/unescape.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_lexer/src/unescape.rs b/compiler/rustc_lexer/src/unescape.rs index 0a632c4d12a..a5ab3fcdd34 100644 --- a/compiler/rustc_lexer/src/unescape.rs +++ b/compiler/rustc_lexer/src/unescape.rs @@ -222,7 +222,7 @@ fn scan_escape + From>( mode: Mode, ) -> Result { // Previous character was '\\', unescape what follows. - let res = match chars.next().ok_or(EscapeError::LoneSlash)? { + let res: u8 = match chars.next().ok_or(EscapeError::LoneSlash)? { '"' => b'"', 'n' => b'\n', 'r' => b'\r', @@ -249,10 +249,10 @@ fn scan_escape + From>( value as u8 } - 'u' => return scan_unicode(chars, mode.is_unicode_escape_disallowed()).map(Into::into), + 'u' => return scan_unicode(chars, mode.is_unicode_escape_disallowed()).map(T::from), _ => return Err(EscapeError::InvalidEscape), }; - Ok(res.into()) + Ok(T::from(res)) } fn scan_unicode( @@ -366,7 +366,7 @@ fn unescape_non_raw_common + From>(src: &str, mode: Mode, c } '"' => Err(EscapeError::EscapeOnlyChar), '\r' => Err(EscapeError::BareCarriageReturn), - _ => ascii_check(c, chars_should_be_ascii).map(Into::into), + _ => ascii_check(c, chars_should_be_ascii).map(T::from), }; let end = src.len() - chars.as_str().len(); callback(start..end, res);