]> git.lizzy.rs Git - rust.git/commitdiff
Convert '&str' into 'String' whenever necessary
authorSeiichi Uchida <seuchida@gmail.com>
Wed, 6 Dec 2017 13:52:43 +0000 (22:52 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Thu, 7 Dec 2017 04:57:52 +0000 (13:57 +0900)
src/comment.rs
src/expr.rs
src/items.rs
src/macros.rs
src/patterns.rs
src/visitor.rs

index 5903323f2f47c2df561089a24e0a9f8a86f9fa27..62ebd8aaeab55160ccafc8b0ac3a30b791e1b7de 100644 (file)
@@ -918,7 +918,7 @@ pub fn recover_comment_removed(
     let snippet = context.snippet(span);
     if snippet != new && changed_comment_content(&snippet, &new) {
         // We missed some comments. Keep the original text.
-        Some(snippet)
+        Some(snippet.into())
     } else {
         Some(new)
     }
index 35a441012d8baf73b837fda6eaee05b1500b2b18..8f107888cead996ca2191de446e1a3d342751c6f 100644 (file)
@@ -58,7 +58,7 @@ pub fn format_expr(
     skip_out_of_file_lines_range!(context, expr.span);
 
     if contains_skip(&*expr.attrs) {
-        return Some(context.snippet(expr.span()));
+        return Some(context.snippet(expr.span()).into());
     }
 
     let expr_rw = match expr.node {
@@ -168,7 +168,7 @@ pub fn format_expr(
         ast::ExprKind::Mac(ref mac) => {
             rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
                 wrap_str(
-                    context.snippet(expr.span),
+                    context.snippet(expr.span).into(),
                     context.config.max_width(),
                     shape,
                 )
@@ -274,7 +274,7 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {
         // We do not format these expressions yet, but they should still
         // satisfy our width restrictions.
         ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => {
-            Some(context.snippet(expr.span))
+            Some(context.snippet(expr.span).into())
         }
         ast::ExprKind::Catch(ref block) => {
             if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) {
@@ -1308,7 +1308,7 @@ fn rewrite_match(
             Some(format!("match {} {{}}", cond_str))
         } else {
             // Empty match with comments or inner attributes? We are not going to bother, sorry ;)
-            Some(context.snippet(span))
+            Some(context.snippet(span).into())
         }
     } else {
         Some(format!(
@@ -1767,7 +1767,11 @@ fn can_extend_match_arm_body(body: &ast::Expr) -> bool {
 pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option<String> {
     match l.node {
         ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
-        _ => wrap_str(context.snippet(l.span), context.config.max_width(), shape),
+        _ => wrap_str(
+            context.snippet(l.span).into(),
+            context.config.max_width(),
+            shape,
+        ),
     }
 }
 
@@ -1798,7 +1802,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
             );
             return wrap_str(indented_string_lit, context.config.max_width(), shape);
         } else {
-            return wrap_str(string_lit, context.config.max_width(), shape);
+            return wrap_str(string_lit.into(), context.config.max_width(), shape);
         }
     }
 
@@ -2530,7 +2534,7 @@ pub fn rewrite_field(
     prefix_max_width: usize,
 ) -> Option<String> {
     if contains_skip(&field.attrs) {
-        return Some(context.snippet(field.span()));
+        return Some(context.snippet(field.span()).into());
     }
     let name = &field.ident.node.to_string();
     if field.is_shorthand {
@@ -2738,7 +2742,7 @@ fn rewrite_assignment(
 ) -> Option<String> {
     let operator_str = match op {
         Some(op) => context.snippet(op.span),
-        None => "=".to_owned(),
+        None => "=",
     };
 
     // 1 = space between lhs and operator.
index 8fa1a784c0e55acdc1fd0a4eb8d3e315db6bda83..9d525f9ba9d5fd3411c3bd457a411544de1cb5cf 100644 (file)
@@ -382,7 +382,7 @@ fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
 
                             format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
                                 .map(|s| s + suffix)
-                                .or_else(|| Some(self.snippet(e.span)))
+                                .or_else(|| Some(self.snippet(e.span).into()))
                         }
                         None => stmt.rewrite(&self.get_context(), self.shape()),
                     }
@@ -526,7 +526,7 @@ fn format_variant(&self, field: &ast::Variant, one_line_width: usize) -> Option<
         if contains_skip(&field.node.attrs) {
             let lo = field.node.attrs[0].span.lo();
             let span = mk_sp(lo, field.span.hi());
-            return Some(self.snippet(span));
+            return Some(self.snippet(span).into());
         }
 
         let context = self.get_context();
@@ -1429,7 +1429,8 @@ pub fn rewrite_struct_field(
     lhs_max_width: usize,
 ) -> Option<String> {
     if contains_skip(&field.attrs) {
-        return Some(context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())));
+        let snippet = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi()));
+        return Some(snippet.into());
     }
 
     let type_annotation_spacing = type_annotation_spacing(context.config);
index f53acbc024a24d299bc638d2e79ddb75e2291cb5..a6de1ded8630f62f8964b77ef8df658674a55394 100644 (file)
@@ -162,7 +162,7 @@ pub fn rewrite_macro(
         loop {
             match parse_macro_arg(&mut parser) {
                 Some(arg) => arg_vec.push(arg),
-                None => return Some(context.snippet(mac.span)),
+                None => return Some(context.snippet(mac.span).into()),
             }
 
             match parser.token {
@@ -182,13 +182,13 @@ pub fn rewrite_macro(
                                         break;
                                     }
                                 }
-                                None => return Some(context.snippet(mac.span)),
+                                None => return Some(context.snippet(mac.span).into()),
                             }
                         }
                     }
-                    return Some(context.snippet(mac.span));
+                    return Some(context.snippet(mac.span).into());
                 }
-                _ => return Some(context.snippet(mac.span)),
+                _ => return Some(context.snippet(mac.span).into()),
             }
 
             parser.bump();
@@ -271,7 +271,7 @@ pub fn rewrite_macro(
         }
         MacroStyle::Braces => {
             // Skip macro invocations with braces, for now.
-            indent_macro_snippet(context, &context.snippet(mac.span), shape.indent)
+            indent_macro_snippet(context, context.snippet(mac.span), shape.indent)
         }
     }
 }
index 90a47d785f03a29eede79e1ca43c082a2f66f9a3..2e4c01b7aaaa1f867b55a9b0d9e1f882c45447d9 100644 (file)
@@ -122,7 +122,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape)
             }
             // FIXME(#819) format pattern macros.
-            PatKind::Mac(..) => Some(context.snippet(self.span)),
+            PatKind::Mac(..) => Some(context.snippet(self.span).into()),
         }
     }
 }
index fc83ccef3a4ff997de7f27e69b906fa7f1c03801..f7ae9ec103491d4f820d1e4538aa82fc43c67407 100644 (file)
@@ -424,7 +424,7 @@ pub fn visit_item(&mut self, item: &ast::Item) {
                 self.push_rewrite(item.span, rewrite);
             }
             ast::ItemKind::GlobalAsm(..) => {
-                let snippet = Some(self.snippet(item.span));
+                let snippet = Some(self.snippet(item.span).into());
                 self.push_rewrite(item.span, snippet);
             }
             ast::ItemKind::MacroDef(..) => {
@@ -525,8 +525,12 @@ fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPos
 
     pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
         self.format_missing_with_indent(source!(self, span).lo());
-        let result = rewrite.unwrap_or_else(|| self.snippet(span));
-        self.buffer.push_str(&result);
+        if let Some(ref s) = rewrite {
+            self.buffer.push_str(s);
+        } else {
+            let snippet = self.snippet(span);
+            self.buffer.push_str(snippet);
+        }
         self.last_pos = source!(self, span).hi();
     }
 
@@ -826,10 +830,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     .unwrap_or(0),
                 ..shape
             };
-            rewrite_comment(&snippet, false, doc_shape, context.config)
+            rewrite_comment(snippet, false, doc_shape, context.config)
         } else {
-            if contains_comment(&snippet) {
-                return Some(snippet);
+            if contains_comment(snippet) {
+                return Some(snippet.into());
             }
             // 1 = `[`
             let shape = shape.offset_left(prefix.len() + 1)?;
@@ -980,7 +984,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 }
 
 // Format `#[derive(..)]`, using visual indent & mixed style when we need to go multiline.
-fn format_derive(context: &RewriteContext, derive_args: &[String], shape: Shape) -> Option<String> {
+fn format_derive(context: &RewriteContext, derive_args: &[&str], shape: Shape) -> Option<String> {
     let mut result = String::with_capacity(128);
     result.push_str("#[derive(");
     // 11 = `#[derive()]`
@@ -1022,7 +1026,7 @@ fn is_derive(attr: &ast::Attribute) -> bool {
 }
 
 /// Returns the arguments of `#[derive(...)]`.
-fn get_derive_args(context: &RewriteContext, attr: &ast::Attribute) -> Option<Vec<String>> {
+fn get_derive_args<'a>(context: &'a RewriteContext, attr: &ast::Attribute) -> Option<Vec<&'a str>> {
     attr.meta().and_then(|meta_item| match meta_item.node {
         ast::MetaItemKind::List(ref args) if meta_item.name.as_str() == "derive" => {
             // Every argument of `derive` should be `NestedMetaItemKind::Literal`.
@@ -1041,7 +1045,7 @@ pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Optio
     assert!(is_extern_crate(item));
     let new_str = context.snippet(item.span);
     Some(if contains_comment(&new_str) {
-        new_str
+        new_str.into()
     } else {
         let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
         String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))