mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-06 13:44:13 +00:00
Convert '&str' into 'String' whenever necessary
This commit is contained in:
parent
69a15b2eee
commit
7c4a84751f
@ -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)
|
||||
}
|
||||
|
20
src/expr.rs
20
src/expr.rs
@ -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 @@ pub fn format_expr(
|
||||
// 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.
|
||||
|
@ -382,7 +382,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
|
||||
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 @@ impl<'a> FmtVisitor<'a> {
|
||||
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);
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ impl Rewrite for Pat {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -424,7 +424,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
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 @@ impl<'a> FmtVisitor<'a> {
|
||||
|
||||
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 @@ impl Rewrite for ast::Attribute {
|
||||
.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 @@ impl<'a> Rewrite for [ast::Attribute] {
|
||||
}
|
||||
|
||||
// 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, ";"))
|
||||
|
Loading…
Reference in New Issue
Block a user