Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
43ae7854
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
43ae7854
编写于
5月 13, 2020
作者:
J
Julian Wollersberger
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace some usages of the old `unescape_` functions in AST, clippy and tests.
上级
18cc63d6
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
50 addition
and
87 deletion
+50
-87
src/librustc_ast/util/literal.rs
src/librustc_ast/util/literal.rs
+42
-33
src/librustc_lexer/src/unescape.rs
src/librustc_lexer/src/unescape.rs
+0
-48
src/librustc_lexer/src/unescape/tests.rs
src/librustc_lexer/src/unescape/tests.rs
+6
-4
src/tools/clippy/clippy_lints/src/write.rs
src/tools/clippy/clippy_lints/src/write.rs
+2
-2
未找到文件。
src/librustc_ast/util/literal.rs
浏览文件 @
43ae7854
...
...
@@ -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
();
...
...
src/librustc_lexer/src/unescape.rs
浏览文件 @
43ae7854
...
...
@@ -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
{
...
...
src/librustc_lexer/src/unescape/tests.rs
浏览文件 @
43ae7854
...
...
@@ -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
);
}
...
...
src/tools/clippy/clippy_lints/src/write.rs
浏览文件 @
43ae7854
...
...
@@ -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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录