diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-01-23 10:37:27 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-01-25 12:26:36 +1100 |
| commit | ef1e2228cfd9df4059aa44740b0659fea7c5a52f (patch) | |
| tree | 34e6de0bb8e4a89ae673a2ec14918e8723562b9d /compiler/rustc_lexer/src | |
| parent | 4b4bdb575b8498b22729a522885ffadd0e646c0f (diff) | |
| download | rust-ef1e2228cfd9df4059aa44740b0659fea7c5a52f.tar.gz rust-ef1e2228cfd9df4059aa44740b0659fea7c5a52f.zip | |
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`.
Diffstat (limited to 'compiler/rustc_lexer/src')
| -rw-r--r-- | compiler/rustc_lexer/src/unescape.rs | 8 |
1 files 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<T: From<u8> + From<char>>( mode: Mode, ) -> Result<T, EscapeError> { // 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<T: From<u8> + From<char>>( 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 @@ where } '"' => 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); |
