mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-30 14:52:56 +03:00
Replace some usages of the old unescape_ functions in AST, clippy and tests.
This commit is contained in:
@@ -6,8 +6,7 @@
|
||||
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use rustc_lexer::unescape::{unescape_byte, unescape_char};
|
||||
use rustc_lexer::unescape::{unescape_byte_str, unescape_str};
|
||||
use rustc_lexer::unescape::{unescape_raw_byte_str, unescape_raw_str};
|
||||
use rustc_lexer::unescape::{unescape_byte_literal, unescape_literal, Mode};
|
||||
use rustc_span::symbol::{kw, sym, Symbol};
|
||||
use rustc_span::Span;
|
||||
|
||||
@@ -59,45 +58,53 @@ fn from_lit_token(lit: token::Lit) -> Result<LitKind, LitError> {
|
||||
// new symbol because the string in the LitKind is different to the
|
||||
// string in the token.
|
||||
let s = symbol.as_str();
|
||||
let symbol = if s.contains(&['\\', '\r'][..]) {
|
||||
let mut buf = String::with_capacity(s.len());
|
||||
let mut error = Ok(());
|
||||
unescape_str(&s, &mut |_, unescaped_char| match unescaped_char {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
});
|
||||
error?;
|
||||
Symbol::intern(&buf)
|
||||
} else {
|
||||
symbol
|
||||
};
|
||||
let symbol =
|
||||
if s.contains(&['\\', '\r'][..]) {
|
||||
let mut buf = String::with_capacity(s.len());
|
||||
let mut error = Ok(());
|
||||
unescape_literal(&s, Mode::Str, &mut |_, unescaped_char| {
|
||||
match unescaped_char {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
}
|
||||
});
|
||||
error?;
|
||||
Symbol::intern(&buf)
|
||||
} else {
|
||||
symbol
|
||||
};
|
||||
LitKind::Str(symbol, ast::StrStyle::Cooked)
|
||||
}
|
||||
token::StrRaw(n) => {
|
||||
// Ditto.
|
||||
let s = symbol.as_str();
|
||||
let symbol = if s.contains('\r') {
|
||||
let mut buf = String::with_capacity(s.len());
|
||||
let mut error = Ok(());
|
||||
unescape_raw_str(&s, &mut |_, unescaped_char| match unescaped_char {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
});
|
||||
error?;
|
||||
buf.shrink_to_fit();
|
||||
Symbol::intern(&buf)
|
||||
} else {
|
||||
symbol
|
||||
};
|
||||
let symbol =
|
||||
if s.contains('\r') {
|
||||
let mut buf = String::with_capacity(s.len());
|
||||
let mut error = Ok(());
|
||||
unescape_literal(&s, Mode::RawStr, &mut |_, unescaped_char| {
|
||||
match unescaped_char {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
}
|
||||
});
|
||||
error?;
|
||||
buf.shrink_to_fit();
|
||||
Symbol::intern(&buf)
|
||||
} else {
|
||||
symbol
|
||||
};
|
||||
LitKind::Str(symbol, ast::StrStyle::Raw(n))
|
||||
}
|
||||
token::ByteStr => {
|
||||
let s = symbol.as_str();
|
||||
let mut buf = Vec::with_capacity(s.len());
|
||||
let mut error = Ok(());
|
||||
unescape_byte_str(&s, &mut |_, unescaped_byte| match unescaped_byte {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
unescape_byte_literal(&s, Mode::ByteStr, &mut |_, unescaped_byte| {
|
||||
match unescaped_byte {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
}
|
||||
});
|
||||
error?;
|
||||
buf.shrink_to_fit();
|
||||
@@ -108,9 +115,11 @@ fn from_lit_token(lit: token::Lit) -> Result<LitKind, LitError> {
|
||||
let bytes = if s.contains('\r') {
|
||||
let mut buf = Vec::with_capacity(s.len());
|
||||
let mut error = Ok(());
|
||||
unescape_raw_byte_str(&s, &mut |_, unescaped_byte| match unescaped_byte {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
unescape_byte_literal(&s, Mode::RawByteStr, &mut |_, unescaped_byte| {
|
||||
match unescaped_byte {
|
||||
Ok(c) => buf.push(c),
|
||||
Err(_) => error = Err(LitError::LexerError),
|
||||
}
|
||||
});
|
||||
error?;
|
||||
buf.shrink_to_fit();
|
||||
|
||||
@@ -111,54 +111,6 @@ pub fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
|
||||
.map_err(|err| (literal_text.len() - chars.as_str().len(), err))
|
||||
}
|
||||
|
||||
/// Takes a contents of a string literal (without quotes) and produces a
|
||||
/// sequence of escaped characters or errors.
|
||||
/// Values are returned through invoking of the provided callback.
|
||||
pub fn unescape_str<F>(literal_text: &str, callback: &mut F)
|
||||
where
|
||||
F: FnMut(Range<usize>, Result<char, EscapeError>),
|
||||
{
|
||||
unescape_str_or_byte_str(literal_text, Mode::Str, callback)
|
||||
}
|
||||
|
||||
/// Takes a contents of a byte string literal (without quotes) and produces a
|
||||
/// sequence of bytes or errors.
|
||||
/// Values are returned through invoking of the provided callback.
|
||||
pub fn unescape_byte_str<F>(literal_text: &str, callback: &mut F)
|
||||
where
|
||||
F: FnMut(Range<usize>, Result<u8, EscapeError>),
|
||||
{
|
||||
unescape_str_or_byte_str(literal_text, Mode::ByteStr, &mut |range, char| {
|
||||
callback(range, char.map(byte_from_char))
|
||||
})
|
||||
}
|
||||
|
||||
/// Takes a contents of a raw string literal (without quotes) and produces a
|
||||
/// sequence of characters or errors.
|
||||
/// Values are returned through invoking of the provided callback.
|
||||
/// NOTE: Raw strings do not perform any explicit character escaping, here we
|
||||
/// only translate CRLF to LF and produce errors on bare CR.
|
||||
pub fn unescape_raw_str<F>(literal_text: &str, callback: &mut F)
|
||||
where
|
||||
F: FnMut(Range<usize>, Result<char, EscapeError>),
|
||||
{
|
||||
unescape_raw_str_or_byte_str(literal_text, Mode::Str, callback)
|
||||
}
|
||||
|
||||
/// Takes a contents of a raw byte string literal (without quotes) and produces a
|
||||
/// sequence of bytes or errors.
|
||||
/// Values are returned through invoking of the provided callback.
|
||||
/// NOTE: Raw strings do not perform any explicit character escaping, here we
|
||||
/// only translate CRLF to LF and produce errors on bare CR.
|
||||
pub fn unescape_raw_byte_str<F>(literal_text: &str, callback: &mut F)
|
||||
where
|
||||
F: FnMut(Range<usize>, Result<u8, EscapeError>),
|
||||
{
|
||||
unescape_raw_str_or_byte_str(literal_text, Mode::ByteStr, &mut |range, char| {
|
||||
callback(range, char.map(byte_from_char))
|
||||
})
|
||||
}
|
||||
|
||||
/// What kind of literal do we parse.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Mode {
|
||||
|
||||
@@ -102,7 +102,7 @@ fn check(literal_text: &str, expected_char: char) {
|
||||
fn test_unescape_str_good() {
|
||||
fn check(literal_text: &str, expected: &str) {
|
||||
let mut buf = Ok(String::with_capacity(literal_text.len()));
|
||||
unescape_str(literal_text, &mut |range, c| {
|
||||
unescape_literal(literal_text, Mode::Str, &mut |range, c| {
|
||||
if let Ok(b) = &mut buf {
|
||||
match c {
|
||||
Ok(c) => b.push(c),
|
||||
@@ -222,7 +222,7 @@ fn check(literal_text: &str, expected_byte: u8) {
|
||||
fn test_unescape_byte_str_good() {
|
||||
fn check(literal_text: &str, expected: &[u8]) {
|
||||
let mut buf = Ok(Vec::with_capacity(literal_text.len()));
|
||||
unescape_byte_str(literal_text, &mut |range, c| {
|
||||
unescape_byte_literal(literal_text, Mode::ByteStr, &mut |range, c| {
|
||||
if let Ok(b) = &mut buf {
|
||||
match c {
|
||||
Ok(c) => b.push(c),
|
||||
@@ -246,7 +246,7 @@ fn check(literal_text: &str, expected: &[u8]) {
|
||||
fn test_unescape_raw_str() {
|
||||
fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
|
||||
let mut unescaped = Vec::with_capacity(literal.len());
|
||||
unescape_raw_str(literal, &mut |range, res| unescaped.push((range, res)));
|
||||
unescape_literal(literal, Mode::RawStr, &mut |range, res| unescaped.push((range, res)));
|
||||
assert_eq!(unescaped, expected);
|
||||
}
|
||||
|
||||
@@ -258,7 +258,9 @@ fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)])
|
||||
fn test_unescape_raw_byte_str() {
|
||||
fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
|
||||
let mut unescaped = Vec::with_capacity(literal.len());
|
||||
unescape_raw_byte_str(literal, &mut |range, res| unescaped.push((range, res)));
|
||||
unescape_byte_literal(literal, Mode::RawByteStr, &mut |range, res| {
|
||||
unescaped.push((range, res))
|
||||
});
|
||||
assert_eq!(unescaped, expected);
|
||||
}
|
||||
|
||||
|
||||
@@ -483,8 +483,8 @@ fn check_newlines(fmtstr: &StrLit) -> bool {
|
||||
};
|
||||
|
||||
match fmtstr.style {
|
||||
StrStyle::Cooked => unescape::unescape_str(contents, &mut cb),
|
||||
StrStyle::Raw(_) => unescape::unescape_raw_str(contents, &mut cb),
|
||||
StrStyle::Cooked => unescape::unescape_literal(contents, unescape::Mode::Str, &mut cb),
|
||||
StrStyle::Raw(_) => unescape::unescape_literal(contents, unescape::Mode::RawStr, &mut cb),
|
||||
}
|
||||
|
||||
should_lint
|
||||
|
||||
Reference in New Issue
Block a user