Skip to content

Commit 57b4db1

Browse files
committed
Circumvent clippy::octal_escapes lint in generated literals
1 parent d827973 commit 57b4db1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/fallback.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,20 @@ impl Literal {
958958
pub fn string(t: &str) -> Literal {
959959
let mut repr = String::with_capacity(t.len() + 2);
960960
repr.push('"');
961-
for c in t.chars() {
962-
if c == '\'' {
961+
let mut chars = t.chars();
962+
while let Some(ch) = chars.next() {
963+
if ch == '\0'
964+
&& chars
965+
.as_str()
966+
.starts_with(|next| '0' <= next && next <= '7')
967+
{
968+
// circumvent clippy::octal_escapes lint
969+
repr.push_str("\\x00");
970+
} else if ch == '\'' {
963971
// escape_debug turns this into "\'" which is unnecessary.
964-
repr.push(c);
972+
repr.push(ch);
965973
} else {
966-
repr.extend(c.escape_debug());
974+
repr.extend(ch.escape_debug());
967975
}
968976
}
969977
repr.push('"');
@@ -985,16 +993,21 @@ impl Literal {
985993

986994
pub fn byte_string(bytes: &[u8]) -> Literal {
987995
let mut escaped = "b\"".to_string();
988-
for b in bytes {
996+
let mut bytes = bytes.iter();
997+
while let Some(&b) = bytes.next() {
989998
#[allow(clippy::match_overlapping_arm)]
990-
match *b {
991-
b'\0' => escaped.push_str(r"\0"),
999+
match b {
1000+
b'\0' => escaped.push_str(match bytes.as_slice().first() {
1001+
// circumvent clippy::octal_escapes lint
1002+
Some(b'0'..=b'7') => r"\x00",
1003+
_ => r"\0",
1004+
}),
9921005
b'\t' => escaped.push_str(r"\t"),
9931006
b'\n' => escaped.push_str(r"\n"),
9941007
b'\r' => escaped.push_str(r"\r"),
9951008
b'"' => escaped.push_str("\\\""),
9961009
b'\\' => escaped.push_str("\\\\"),
997-
b'\x20'..=b'\x7E' => escaped.push(*b as char),
1010+
b'\x20'..=b'\x7E' => escaped.push(b as char),
9981011
_ => {
9991012
let _ = write!(escaped, "\\x{:02X}", b);
10001013
}

tests/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn literal_string() {
116116
assert_eq!(Literal::string("didn't").to_string(), "\"didn't\"");
117117
assert_eq!(
118118
Literal::string("a\00b\07c\08d\0e\0").to_string(),
119-
"\"a\\00b\\07c\\08d\\0e\\0\"",
119+
"\"a\\x000b\\x007c\\08d\\0e\\0\"",
120120
);
121121
}
122122

@@ -153,7 +153,7 @@ fn literal_byte_string() {
153153
);
154154
assert_eq!(
155155
Literal::byte_string(b"a\00b\07c\08d\0e\0").to_string(),
156-
"b\"a\\00b\\07c\\08d\\0e\\0\"",
156+
"b\"a\\x000b\\x007c\\08d\\0e\\0\"",
157157
);
158158
}
159159

0 commit comments

Comments
 (0)