]> git.lizzy.rs Git - rust.git/commitdiff
Avoid an unnecessary intermediate value in char_lit().
authorNicholas Nethercote <nnethercote@mozilla.com>
Mon, 12 Sep 2016 03:03:21 +0000 (13:03 +1000)
committerNicholas Nethercote <nnethercote@mozilla.com>
Mon, 12 Sep 2016 06:15:52 +0000 (16:15 +1000)
This makes the function more concise and easier to understand.

src/libsyntax/parse/mod.rs

index af95e44a567bab9245e182d726026e9f55ecd280..31fa2cda5db5ea1c2f1c27464559adfdbb11de54 100644 (file)
@@ -287,26 +287,21 @@ pub fn char_lit(lit: &str) -> (char, isize) {
     use std::char;
 
     let mut chars = lit.chars();
-    let c = match (chars.next(), chars.next()) {
+    match (chars.next(), chars.next()) {
         (Some(c), None) if c != '\\' => return (c, 1),
         (Some('\\'), Some(c)) => match c {
-            '"' => Some('"'),
-            'n' => Some('\n'),
-            'r' => Some('\r'),
-            't' => Some('\t'),
-            '\\' => Some('\\'),
-            '\'' => Some('\''),
-            '0' => Some('\0'),
-            _ => { None }
+            '"' => return ('"', 2),
+            'n' => return ('\n', 2),
+            'r' => return ('\r', 2),
+            't' => return ('\t', 2),
+            '\\' => return ('\\', 2),
+            '\'' => return ('\'', 2),
+            '0' => return ('\0', 2),
+            _ => {}
         },
         _ => panic!("lexer accepted invalid char escape `{}`", lit)
     };
 
-    match c {
-        Some(x) => return (x, 2),
-        None => { }
-    }
-
     let msg = format!("lexer should have rejected a bad character escape {}", lit);
     let msg2 = &msg[..];