]> git.lizzy.rs Git - rust.git/commitdiff
Stop Option-wrapping comments
authorMark Rousskov <mark.simulacrum@gmail.com>
Fri, 5 Jul 2019 20:32:15 +0000 (16:32 -0400)
committerMark Rousskov <mark.simulacrum@gmail.com>
Wed, 10 Jul 2019 11:12:28 +0000 (07:12 -0400)
We always check against the length before indexing anyway.

src/librustc/hir/print.rs
src/libsyntax/print/pprust.rs

index 137f20d6c32b32dae1f3781761c9f9453d8ab3be..9acf6d895e3e622c3c8cd3d0600411ce98444f55 100644 (file)
@@ -72,7 +72,7 @@ fn nested(&self, state: &mut State<'_>, nested: Nested) {
 pub struct State<'a> {
     pub s: pp::Printer<'a>,
     cm: Option<&'a SourceMap>,
-    comments: Option<Vec<comments::Comment>>,
+    comments: Vec<comments::Comment>,
     cur_cmnt: usize,
     ann: &'a (dyn PpAnn + 'a),
 }
@@ -82,7 +82,7 @@ fn writer(&mut self) -> &mut pp::Printer<'a> {
         &mut self.s
     }
 
-    fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
+    fn comments(&mut self) -> &mut Vec<comments::Comment> {
         &mut self.comments
     }
 
@@ -134,7 +134,7 @@ pub fn new(cm: &'a SourceMap,
         State {
             s: pp::mk_printer(out),
             cm: Some(cm),
-            comments,
+            comments: comments.unwrap_or_default(),
             cur_cmnt: 0,
             ann,
         }
@@ -149,7 +149,7 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
         let mut printer = State {
             s: pp::mk_printer(&mut wr),
             cm: None,
-            comments: None,
+            comments: Vec::new(),
             cur_cmnt: 0,
             ann,
         };
index 4c86300b80562f42988203aa4c771c1868525756..460c443471282f22ed51ef47ab26ba81b85c52d5 100644 (file)
@@ -46,7 +46,7 @@ impl PpAnn for NoAnn {}
 pub struct State<'a> {
     pub s: pp::Printer<'a>,
     cm: Option<&'a SourceMap>,
-    comments: Option<Vec<comments::Comment>>,
+    comments: Vec<comments::Comment>,
     cur_cmnt: usize,
     ann: &'a (dyn PpAnn+'a),
     is_expanded: bool
@@ -110,7 +110,7 @@ pub fn new(cm: &'a SourceMap,
         State {
             s: pp::mk_printer(out),
             cm: Some(cm),
-            comments,
+            comments: comments.unwrap_or_default(),
             cur_cmnt: 0,
             ann,
             is_expanded,
@@ -126,7 +126,7 @@ pub fn to_string<F>(f: F) -> String where
         let mut printer = State {
             s: pp::mk_printer(&mut wr),
             cm: None,
-            comments: None,
+            comments: Vec::new(),
             cur_cmnt: 0,
             ann: &NoAnn,
             is_expanded: false
@@ -423,7 +423,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
 
 pub trait PrintState<'a> {
     fn writer(&mut self) -> &mut pp::Printer<'a>;
-    fn comments(&mut self) -> &mut Option<Vec<comments::Comment>>;
+    fn comments(&mut self) -> &mut Vec<comments::Comment>;
     fn cur_cmnt(&mut self) -> &mut usize;
 
     fn word_space<S: Into<Cow<'static, str>>>(&mut self, w: S) {
@@ -550,15 +550,11 @@ fn print_comment(&mut self,
 
     fn next_comment(&mut self) -> Option<comments::Comment> {
         let cur_cmnt = *self.cur_cmnt();
-        match *self.comments() {
-            Some(ref cmnts) => {
-                if cur_cmnt < cmnts.len() {
-                    Some(cmnts[cur_cmnt].clone())
-                } else {
-                    None
-                }
-            }
-            _ => None
+        let cmnts = &*self.comments();
+        if cur_cmnt < cmnts.len() {
+            Some(cmnts[cur_cmnt].clone())
+        } else {
+            None
         }
     }
 
@@ -756,7 +752,7 @@ fn writer(&mut self) -> &mut pp::Printer<'a> {
         &mut self.s
     }
 
-    fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
+    fn comments(&mut self) -> &mut Vec<comments::Comment> {
         &mut self.comments
     }