Merge pull request #1556 from topecongiro/issue-1555

Only add offset for unary op
This commit is contained in:
Nick Cameron 2017-05-16 09:55:07 +12:00 committed by GitHub
commit 09e5051dee
10 changed files with 71 additions and 82 deletions

View file

@ -156,11 +156,7 @@ fn format_expr(expr: &ast::Expr,
};
if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context,
&format!("break{} ", id_str),
&**expr,
shape,
expr.span)
rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape)
} else {
wrap_str(format!("break{}", id_str), context.config.max_width, shape)
}
@ -180,11 +176,9 @@ fn format_expr(expr: &ast::Expr,
}
ast::ExprKind::Ret(None) => wrap_str("return".to_owned(), context.config.max_width, shape),
ast::ExprKind::Ret(Some(ref expr)) => {
rewrite_unary_prefix(context, "return ", &**expr, shape, expr.span)
}
ast::ExprKind::Box(ref expr) => {
rewrite_unary_prefix(context, "box ", &**expr, shape, expr.span)
rewrite_unary_prefix(context, "return ", &**expr, shape)
}
ast::ExprKind::Box(ref expr) => rewrite_unary_prefix(context, "box ", &**expr, shape),
ast::ExprKind::AddrOf(mutability, ref expr) => {
rewrite_expr_addrof(context, mutability, expr, shape)
}
@ -226,7 +220,7 @@ fn format_expr(expr: &ast::Expr,
} else {
delim.into()
};
rewrite_unary_prefix(context, &sp_delim, &**rhs, shape, expr.span)
rewrite_unary_prefix(context, &sp_delim, &**rhs, shape)
}
(Some(ref lhs), None) => {
let sp_delim = if context.config.spaces_around_ranges {
@ -1967,14 +1961,14 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext,
// 3 = "(" + ",)"
let nested_shape = try_opt!(shape.sub_width(3)).visual_indent(1);
return items
.next()
.unwrap()
.rewrite(context, nested_shape)
.map(|s| if context.config.spaces_within_parens {
format!("( {}, )", s)
} else {
format!("({},)", s)
});
.next()
.unwrap()
.rewrite(context, nested_shape)
.map(|s| if context.config.spaces_within_parens {
format!("( {}, )", s)
} else {
format!("({},)", s)
});
}
let list_lo = context.codemap.span_after(span, "(");
@ -1999,24 +1993,10 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext,
pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext,
prefix: &str,
rewrite: &R,
mut shape: Shape,
span: Span)
shape: Shape)
-> Option<String> {
// Heuristic: if unary is `&` and `rewrite` contains `{`,
// it is likely that block indent is preferred to visual indent.
if prefix == "&" {
let snippet = String::from(context.snippet(span).trim_left_matches('&'));
let first_line = try_opt!(snippet.lines().nth(0));
if first_line.contains("{") {
shape = try_opt!(shape.sub_width(prefix.len())).block_indent(0);
} else {
shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0);
}
} else {
shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0);
}
rewrite
.rewrite(context, shape)
.rewrite(context, try_opt!(shape.offset_left(prefix.len())))
.map(|r| format!("{}{}", prefix, r))
}
@ -2046,7 +2026,7 @@ fn rewrite_unary_op(context: &RewriteContext,
ast::UnOp::Not => "!",
ast::UnOp::Neg => "-",
};
rewrite_unary_prefix(context, operator_str, expr, shape, expr.span)
rewrite_unary_prefix(context, operator_str, expr, shape)
}
fn rewrite_assignment(context: &RewriteContext,
@ -2143,5 +2123,5 @@ fn rewrite_expr_addrof(context: &RewriteContext,
ast::Mutability::Immutable => "&",
ast::Mutability::Mutable => "&mut ",
};
rewrite_unary_prefix(context, operator_str, expr, shape, expr.span)
rewrite_unary_prefix(context, operator_str, expr, shape)
}

View file

@ -131,16 +131,16 @@ impl BadIssueSeeker {
todo_idx += 1;
if todo_idx == TO_DO_CHARS.len() {
return Seeking::Number {
issue: Issue {
issue_type: IssueType::Todo,
missing_number: if let ReportTactic::Unnumbered = self.report_todo {
true
} else {
false
},
},
part: NumberPart::OpenParen,
};
issue: Issue {
issue_type: IssueType::Todo,
missing_number: if let ReportTactic::Unnumbered = self.report_todo {
true
} else {
false
},
},
part: NumberPart::OpenParen,
};
}
fixme_idx = 0;
} else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
@ -149,17 +149,16 @@ impl BadIssueSeeker {
fixme_idx += 1;
if fixme_idx == FIX_ME_CHARS.len() {
return Seeking::Number {
issue: Issue {
issue_type: IssueType::Fixme,
missing_number: if let ReportTactic::Unnumbered =
self.report_fixme {
true
} else {
false
},
},
part: NumberPart::OpenParen,
};
issue: Issue {
issue_type: IssueType::Fixme,
missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
true
} else {
false
},
},
part: NumberPart::OpenParen,
};
}
todo_idx = 0;
} else {
@ -182,10 +181,10 @@ impl BadIssueSeeker {
return IssueClassification::Bad(issue);
} else if c == ')' {
return if let NumberPart::CloseParen = part {
IssueClassification::Good
} else {
IssueClassification::Bad(issue)
};
IssueClassification::Good
} else {
IssueClassification::Bad(issue)
};
}
match part {

View file

@ -1104,7 +1104,7 @@ fn format_tuple_struct(context: &RewriteContext,
// know that earlier, so the where clause will not be indented properly.
result.push('\n');
result.push_str(&(offset.block_only() + (context.config.tab_spaces - 1))
.to_string(context.config));
.to_string(context.config));
}
result.push_str(&where_clause_str);

View file

@ -94,13 +94,13 @@ pub fn rewrite_macro(mac: &ast::Mac,
if mac.node.tts.is_empty() && !contains_comment(&context.snippet(mac.span)) {
return match style {
MacroStyle::Parens if position == MacroPosition::Item => {
Some(format!("{}();", macro_name))
}
MacroStyle::Parens => Some(format!("{}()", macro_name)),
MacroStyle::Brackets => Some(format!("{}[]", macro_name)),
MacroStyle::Braces => Some(format!("{}{{}}", macro_name)),
};
MacroStyle::Parens if position == MacroPosition::Item => {
Some(format!("{}();", macro_name))
}
MacroStyle::Parens => Some(format!("{}()", macro_name)),
MacroStyle::Brackets => Some(format!("{}[]", macro_name)),
MacroStyle::Braces => Some(format!("{}{{}}", macro_name)),
};
}
let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone());

View file

@ -138,8 +138,8 @@ impl<'a> FmtVisitor<'a> {
if rewrite_next_comment &&
!self.config
.file_lines
.intersects_range(file_name, cur_line, cur_line + subslice_num_lines) {
.file_lines
.intersects_range(file_name, cur_line, cur_line + subslice_num_lines) {
rewrite_next_comment = false;
}
@ -164,7 +164,7 @@ impl<'a> FmtVisitor<'a> {
Shape::legacy(comment_width,
self.block_indent),
self.config)
.unwrap());
.unwrap());
last_wspace = None;
line_start = offset + subslice.len();
@ -172,9 +172,9 @@ impl<'a> FmtVisitor<'a> {
if let Some('/') = subslice.chars().skip(1).next() {
// check that there are no contained block comments
if !subslice
.split('\n')
.map(|s| s.trim_left())
.any(|s| s.len() > 2 && &s[0..2] == "/*") {
.split('\n')
.map(|s| s.trim_left())
.any(|s| s.len() > 2 && &s[0..2] == "/*") {
// Add a newline after line comments
self.buffer.push_str("\n");
}

View file

@ -28,9 +28,7 @@ use syntax::codemap::{self, BytePos, Span};
impl Rewrite for Pat {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match self.node {
PatKind::Box(ref pat) => {
rewrite_unary_prefix(context, "box ", &**pat, shape, self.span)
}
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape),
PatKind::Ident(binding_mode, ident, ref sub_pat) => {
let (prefix, mutability) = match binding_mode {
BindingMode::ByRef(mutability) => ("ref ", mutability),
@ -74,7 +72,7 @@ impl Rewrite for Pat {
}
PatKind::Ref(ref pat, mutability) => {
let prefix = format!("&{}", format_mutability(mutability));
rewrite_unary_prefix(context, &prefix, &**pat, shape, self.span)
rewrite_unary_prefix(context, &prefix, &**pat, shape)
}
PatKind::Tuple(ref items, dotdot_pos) => {
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, shape)

View file

@ -595,7 +595,7 @@ impl Rewrite for ast::Ty {
Mutability::Immutable => "*const ",
};
rewrite_unary_prefix(context, prefix, &*mt.ty, shape, self.span)
rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
}
ast::TyKind::Rptr(ref lifetime, ref mt) => {
let mut_str = format_mutability(mt.mutbl);

View file

@ -50,8 +50,8 @@ impl<'a> FmtVisitor<'a> {
// FIXME(#434): Move this check to somewhere more central, eg Rewrite.
if !self.config
.file_lines
.intersects(&self.codemap.lookup_line_range(stmt.span)) {
.file_lines
.intersects(&self.codemap.lookup_line_range(stmt.span)) {
return;
}

View file

@ -79,3 +79,9 @@ gfx_pipeline!(pipe {
fn issue_1279() {
println!("dsfs"); // a comment
}
fn issue_1555() {
let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
"65454654654654654654654655464",
"4");
}

View file

@ -80,3 +80,9 @@ gfx_pipeline!(pipe {
fn issue_1279() {
println!("dsfs"); // a comment
}
fn issue_1555() {
let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
"65454654654654654654654655464",
"4");
}