Skip to content

Commit 43ae785

Browse files
Replace some usages of the old unescape_ functions in AST, clippy and tests.
1 parent 18cc63d commit 43ae785

File tree

4 files changed

+50
-87
lines changed

4 files changed

+50
-87
lines changed

src/librustc_ast/util/literal.rs

+42-33
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::tokenstream::TokenTree;
66

77
use rustc_data_structures::sync::Lrc;
88
use rustc_lexer::unescape::{unescape_byte, unescape_char};
9-
use rustc_lexer::unescape::{unescape_byte_str, unescape_str};
10-
use rustc_lexer::unescape::{unescape_raw_byte_str, unescape_raw_str};
9+
use rustc_lexer::unescape::{unescape_byte_literal, unescape_literal, Mode};
1110
use rustc_span::symbol::{kw, sym, Symbol};
1211
use rustc_span::Span;
1312

@@ -59,45 +58,53 @@ impl LitKind {
5958
// new symbol because the string in the LitKind is different to the
6059
// string in the token.
6160
let s = symbol.as_str();
62-
let symbol = if s.contains(&['\\', '\r'][..]) {
63-
let mut buf = String::with_capacity(s.len());
64-
let mut error = Ok(());
65-
unescape_str(&s, &mut |_, unescaped_char| match unescaped_char {
66-
Ok(c) => buf.push(c),
67-
Err(_) => error = Err(LitError::LexerError),
68-
});
69-
error?;
70-
Symbol::intern(&buf)
71-
} else {
72-
symbol
73-
};
61+
let symbol =
62+
if s.contains(&['\\', '\r'][..]) {
63+
let mut buf = String::with_capacity(s.len());
64+
let mut error = Ok(());
65+
unescape_literal(&s, Mode::Str, &mut |_, unescaped_char| {
66+
match unescaped_char {
67+
Ok(c) => buf.push(c),
68+
Err(_) => error = Err(LitError::LexerError),
69+
}
70+
});
71+
error?;
72+
Symbol::intern(&buf)
73+
} else {
74+
symbol
75+
};
7476
LitKind::Str(symbol, ast::StrStyle::Cooked)
7577
}
7678
token::StrRaw(n) => {
7779
// Ditto.
7880
let s = symbol.as_str();
79-
let symbol = if s.contains('\r') {
80-
let mut buf = String::with_capacity(s.len());
81-
let mut error = Ok(());
82-
unescape_raw_str(&s, &mut |_, unescaped_char| match unescaped_char {
83-
Ok(c) => buf.push(c),
84-
Err(_) => error = Err(LitError::LexerError),
85-
});
86-
error?;
87-
buf.shrink_to_fit();
88-
Symbol::intern(&buf)
89-
} else {
90-
symbol
91-
};
81+
let symbol =
82+
if s.contains('\r') {
83+
let mut buf = String::with_capacity(s.len());
84+
let mut error = Ok(());
85+
unescape_literal(&s, Mode::RawStr, &mut |_, unescaped_char| {
86+
match unescaped_char {
87+
Ok(c) => buf.push(c),
88+
Err(_) => error = Err(LitError::LexerError),
89+
}
90+
});
91+
error?;
92+
buf.shrink_to_fit();
93+
Symbol::intern(&buf)
94+
} else {
95+
symbol
96+
};
9297
LitKind::Str(symbol, ast::StrStyle::Raw(n))
9398
}
9499
token::ByteStr => {
95100
let s = symbol.as_str();
96101
let mut buf = Vec::with_capacity(s.len());
97102
let mut error = Ok(());
98-
unescape_byte_str(&s, &mut |_, unescaped_byte| match unescaped_byte {
99-
Ok(c) => buf.push(c),
100-
Err(_) => error = Err(LitError::LexerError),
103+
unescape_byte_literal(&s, Mode::ByteStr, &mut |_, unescaped_byte| {
104+
match unescaped_byte {
105+
Ok(c) => buf.push(c),
106+
Err(_) => error = Err(LitError::LexerError),
107+
}
101108
});
102109
error?;
103110
buf.shrink_to_fit();
@@ -108,9 +115,11 @@ impl LitKind {
108115
let bytes = if s.contains('\r') {
109116
let mut buf = Vec::with_capacity(s.len());
110117
let mut error = Ok(());
111-
unescape_raw_byte_str(&s, &mut |_, unescaped_byte| match unescaped_byte {
112-
Ok(c) => buf.push(c),
113-
Err(_) => error = Err(LitError::LexerError),
118+
unescape_byte_literal(&s, Mode::RawByteStr, &mut |_, unescaped_byte| {
119+
match unescaped_byte {
120+
Ok(c) => buf.push(c),
121+
Err(_) => error = Err(LitError::LexerError),
122+
}
114123
});
115124
error?;
116125
buf.shrink_to_fit();

src/librustc_lexer/src/unescape.rs

-48
Original file line numberDiff line numberDiff line change
@@ -111,54 +111,6 @@ pub fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
111111
.map_err(|err| (literal_text.len() - chars.as_str().len(), err))
112112
}
113113

114-
/// Takes a contents of a string literal (without quotes) and produces a
115-
/// sequence of escaped characters or errors.
116-
/// Values are returned through invoking of the provided callback.
117-
pub fn unescape_str<F>(literal_text: &str, callback: &mut F)
118-
where
119-
F: FnMut(Range<usize>, Result<char, EscapeError>),
120-
{
121-
unescape_str_or_byte_str(literal_text, Mode::Str, callback)
122-
}
123-
124-
/// Takes a contents of a byte string literal (without quotes) and produces a
125-
/// sequence of bytes or errors.
126-
/// Values are returned through invoking of the provided callback.
127-
pub fn unescape_byte_str<F>(literal_text: &str, callback: &mut F)
128-
where
129-
F: FnMut(Range<usize>, Result<u8, EscapeError>),
130-
{
131-
unescape_str_or_byte_str(literal_text, Mode::ByteStr, &mut |range, char| {
132-
callback(range, char.map(byte_from_char))
133-
})
134-
}
135-
136-
/// Takes a contents of a raw string literal (without quotes) and produces a
137-
/// sequence of characters or errors.
138-
/// Values are returned through invoking of the provided callback.
139-
/// NOTE: Raw strings do not perform any explicit character escaping, here we
140-
/// only translate CRLF to LF and produce errors on bare CR.
141-
pub fn unescape_raw_str<F>(literal_text: &str, callback: &mut F)
142-
where
143-
F: FnMut(Range<usize>, Result<char, EscapeError>),
144-
{
145-
unescape_raw_str_or_byte_str(literal_text, Mode::Str, callback)
146-
}
147-
148-
/// Takes a contents of a raw byte string literal (without quotes) and produces a
149-
/// sequence of bytes or errors.
150-
/// Values are returned through invoking of the provided callback.
151-
/// NOTE: Raw strings do not perform any explicit character escaping, here we
152-
/// only translate CRLF to LF and produce errors on bare CR.
153-
pub fn unescape_raw_byte_str<F>(literal_text: &str, callback: &mut F)
154-
where
155-
F: FnMut(Range<usize>, Result<u8, EscapeError>),
156-
{
157-
unescape_raw_str_or_byte_str(literal_text, Mode::ByteStr, &mut |range, char| {
158-
callback(range, char.map(byte_from_char))
159-
})
160-
}
161-
162114
/// What kind of literal do we parse.
163115
#[derive(Debug, Clone, Copy)]
164116
pub enum Mode {

src/librustc_lexer/src/unescape/tests.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn test_unescape_char_good() {
102102
fn test_unescape_str_good() {
103103
fn check(literal_text: &str, expected: &str) {
104104
let mut buf = Ok(String::with_capacity(literal_text.len()));
105-
unescape_str(literal_text, &mut |range, c| {
105+
unescape_literal(literal_text, Mode::Str, &mut |range, c| {
106106
if let Ok(b) = &mut buf {
107107
match c {
108108
Ok(c) => b.push(c),
@@ -222,7 +222,7 @@ fn test_unescape_byte_good() {
222222
fn test_unescape_byte_str_good() {
223223
fn check(literal_text: &str, expected: &[u8]) {
224224
let mut buf = Ok(Vec::with_capacity(literal_text.len()));
225-
unescape_byte_str(literal_text, &mut |range, c| {
225+
unescape_byte_literal(literal_text, Mode::ByteStr, &mut |range, c| {
226226
if let Ok(b) = &mut buf {
227227
match c {
228228
Ok(c) => b.push(c),
@@ -246,7 +246,7 @@ fn test_unescape_byte_str_good() {
246246
fn test_unescape_raw_str() {
247247
fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
248248
let mut unescaped = Vec::with_capacity(literal.len());
249-
unescape_raw_str(literal, &mut |range, res| unescaped.push((range, res)));
249+
unescape_literal(literal, Mode::RawStr, &mut |range, res| unescaped.push((range, res)));
250250
assert_eq!(unescaped, expected);
251251
}
252252

@@ -258,7 +258,9 @@ fn test_unescape_raw_str() {
258258
fn test_unescape_raw_byte_str() {
259259
fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
260260
let mut unescaped = Vec::with_capacity(literal.len());
261-
unescape_raw_byte_str(literal, &mut |range, res| unescaped.push((range, res)));
261+
unescape_byte_literal(literal, Mode::RawByteStr, &mut |range, res| {
262+
unescaped.push((range, res))
263+
});
262264
assert_eq!(unescaped, expected);
263265
}
264266

src/tools/clippy/clippy_lints/src/write.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ fn check_newlines(fmtstr: &StrLit) -> bool {
483483
};
484484

485485
match fmtstr.style {
486-
StrStyle::Cooked => unescape::unescape_str(contents, &mut cb),
487-
StrStyle::Raw(_) => unescape::unescape_raw_str(contents, &mut cb),
486+
StrStyle::Cooked => unescape::unescape_literal(contents, unescape::Mode::Str, &mut cb),
487+
StrStyle::Raw(_) => unescape::unescape_literal(contents, unescape::Mode::RawStr, &mut cb),
488488
}
489489

490490
should_lint

0 commit comments

Comments
 (0)