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)
}
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 {
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,
)
// 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) {
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!(
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,
+ ),
}
}
);
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);
}
}
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 {
) -> Option<String> {
let operator_str = match op {
Some(op) => context.snippet(op.span),
- None => "=".to_owned(),
+ None => "=",
};
// 1 = space between lhs and operator.
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()),
}
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();
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);
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 {
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();
}
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)
}
}
}
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()),
}
}
}
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(..) => {
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();
}
.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)?;
}
// 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()]`
}
/// 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`.
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, ";"))