From 7efc1c4db2ea533e4263aa39bf7264ec7b7d4b8f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 2 Apr 2023 23:20:47 -0700 Subject: [PATCH] Implement consistent behavior for Literal::string on all versions of Rust --- src/fallback.rs | 19 ++++++++++++------- tests/test.rs | 9 ++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index bd5d9dad..bbea473d 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -960,13 +960,18 @@ impl Literal { repr.push('"'); let mut chars = t.chars(); while let Some(ch) = chars.next() { - if ch == '\0' - && chars - .as_str() - .starts_with(|next| '0' <= next && next <= '7') - { - // circumvent clippy::octal_escapes lint - repr.push_str("\\x00"); + if ch == '\0' { + repr.push_str( + if chars + .as_str() + .starts_with(|next| '0' <= next && next <= '7') + { + // circumvent clippy::octal_escapes lint + "\\x00" + } else { + "\\0" + }, + ); } else if ch == '\'' { // escape_debug turns this into "\'" which is unnecessary. repr.push(ch); diff --git a/tests/test.rs b/tests/test.rs index 2bd93e0b..75f69e2a 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -115,11 +115,10 @@ fn literal_string() { assert_eq!(Literal::string("foo").to_string(), "\"foo\""); assert_eq!(Literal::string("\"").to_string(), "\"\\\"\""); assert_eq!(Literal::string("didn't").to_string(), "\"didn't\""); - - let repr = Literal::string("a\00b\07c\08d\0e\0").to_string(); - if repr != "\"a\\x000b\\x007c\\u{0}8d\\u{0}e\\u{0}\"" { - assert_eq!(repr, "\"a\\x000b\\x007c\\08d\\0e\\0\""); - } + assert_eq!( + Literal::string("a\00b\07c\08d\0e\0").to_string(), + "\"a\\x000b\\x007c\\08d\\0e\\0\"", + ); } #[test]