Skip to content

Commit 7f576b0

Browse files
committed
General cleanup after rebase
1 parent 03c6606 commit 7f576b0

File tree

13 files changed

+105
-116
lines changed

13 files changed

+105
-116
lines changed

src/comment.rs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,44 @@ pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usiz
4141
let line_breaks = s.chars().filter(|&c| c == '\n').count();
4242

4343
let (_, mut s) = s.lines()
44-
.enumerate()
45-
.map(|(i, mut line)| {
46-
line = line.trim();
47-
// Drop old closer.
48-
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
49-
line = &line[..(line.len() - 2)];
50-
}
51-
52-
line.trim_right()
53-
})
54-
.map(left_trim_comment_line)
55-
.map(|line| {
56-
if line_breaks == 0 {
57-
line.trim_left()
58-
} else {
59-
line
60-
}
61-
})
62-
.fold((true, opener.to_owned()), |(first, mut acc), line| {
63-
if !first {
64-
acc.push('\n');
65-
acc.push_str(&indent_str);
66-
acc.push_str(line_start);
67-
}
68-
69-
if line.len() > max_chars {
70-
acc.push_str(&rewrite_string(line, &fmt));
71-
} else {
72-
if line.len() == 0 {
73-
acc.pop(); // Remove space if this is an empty comment.
74-
} else {
75-
acc.push_str(line);
76-
}
77-
}
78-
79-
(false, acc)
80-
});
44+
.enumerate()
45+
.map(|(i, mut line)| {
46+
line = line.trim();
47+
// Drop old closer.
48+
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
49+
line = &line[..(line.len() - 2)];
50+
}
51+
52+
line.trim_right()
53+
})
54+
.map(left_trim_comment_line)
55+
.map(|line| {
56+
if line_breaks == 0 {
57+
line.trim_left()
58+
} else {
59+
line
60+
}
61+
})
62+
.fold((true, opener.to_owned()),
63+
|(first, mut acc), line| {
64+
if !first {
65+
acc.push('\n');
66+
acc.push_str(&indent_str);
67+
acc.push_str(line_start);
68+
}
69+
70+
if line.len() > max_chars {
71+
acc.push_str(&rewrite_string(line, &fmt));
72+
} else {
73+
if line.len() == 0 {
74+
acc.pop(); // Remove space if this is an empty comment.
75+
} else {
76+
acc.push_str(line);
77+
}
78+
}
79+
80+
(false, acc)
81+
});
8182

8283
s.push_str(closer);
8384

src/expr.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use StructLitStyle;
1818
use utils::{span_after, make_indent, extra_offset, first_line_width, last_line_width, wrap_str,
1919
binary_search};
2020
use visitor::FmtVisitor;
21-
use config::{BlockIndentStyle, MultilineStyle};
21+
use config::MultilineStyle;
2222
use comment::{FindUncommented, rewrite_comment, contains_comment};
2323
use types::rewrite_path;
2424
use items::{span_lo_for_arg, span_hi_for_arg, rewrite_fn_input};
@@ -40,12 +40,6 @@ impl Rewrite for ast::Expr {
4040
}
4141
}
4242
ast::Expr_::ExprCall(ref callee, ref args) => {
43-
// FIXME using byte lens instead of char lens (and probably all over the place too)
44-
// 2 is for parens
45-
let max_callee_width = try_opt!(width.checked_sub(2));
46-
let callee_str = try_opt!(callee.rewrite(context, max_callee_width, offset));
47-
let span = mk_sp(callee.span.hi, self.span.hi);
48-
4943
rewrite_call(context, &**callee, args, self.span, width, offset)
5044
}
5145
ast::Expr_::ExprParen(ref subexpr) => {
@@ -214,8 +208,6 @@ fn rewrite_closure(capture: ast::CaptureClause,
214208
prefix.push_str(&ret_str);
215209
}
216210

217-
let closure_indent = closure_indent(context, offset);
218-
219211
// Try to format closure body as a single line expression without braces.
220212
if is_simple_block(body, context.codemap) && !prefix.contains('\n') {
221213
let (spacer, closer) = if ret_str.is_empty() {
@@ -246,17 +238,16 @@ fn rewrite_closure(capture: ast::CaptureClause,
246238

247239
// We couldn't format the closure body as a single line expression; fall
248240
// back to block formatting.
249-
let inner_context = context.overflow_context(closure_indent - context.block_indent);
250241
let body_rewrite = body.expr
251242
.as_ref()
252243
.and_then(|body_expr| {
253244
if let ast::Expr_::ExprBlock(ref inner) = body_expr.node {
254-
Some(inner.rewrite(&inner_context, 2, 0))
245+
Some(inner.rewrite(&context, 2, 0))
255246
} else {
256247
None
257248
}
258249
})
259-
.unwrap_or_else(|| body.rewrite(&inner_context, 2, 0));
250+
.unwrap_or_else(|| body.rewrite(&context, 2, 0));
260251

261252
Some(format!("{} {}", prefix, try_opt!(body_rewrite)))
262253
}
@@ -876,25 +867,21 @@ fn rewrite_string_lit(context: &RewriteContext,
876867
}
877868

878869
pub fn rewrite_call<R>(context: &RewriteContext,
879-
callee: &R,
880-
args: &[ptr::P<ast::Expr>],
881-
span: Span,
882-
width: usize,
883-
offset: usize)
884-
-> Option<String>
870+
callee: &R,
871+
args: &[ptr::P<ast::Expr>],
872+
span: Span,
873+
width: usize,
874+
offset: usize)
875+
-> Option<String>
885876
where R: Rewrite
886877
{
878+
let closure = |callee_max_width| {
879+
rewrite_call_inner(context, callee, callee_max_width, args, span, width, offset)
880+
};
881+
887882
// 2 is for parens
888883
let max_width = try_opt!(width.checked_sub(2));
889-
binary_search(1, max_width, |callee_max_width| {
890-
rewrite_call_inner(context,
891-
callee,
892-
callee_max_width,
893-
args,
894-
span,
895-
width,
896-
offset)
897-
})
884+
binary_search(1, max_width, closure)
898885
}
899886

900887
fn rewrite_call_inner<R>(context: &RewriteContext,
@@ -1021,10 +1008,9 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
10211008
match *item {
10221009
StructLitField::Regular(ref field) => field.span.lo,
10231010
StructLitField::Base(ref expr) => {
1024-
let last_field_hi = fields.last().map_or(span.lo,
1025-
|field| {
1026-
field.span.hi
1027-
});
1011+
let last_field_hi = fields.last()
1012+
.map_or(span.lo,
1013+
|field| field.span.hi);
10281014
let snippet = context.snippet(mk_sp(last_field_hi,
10291015
expr.span.lo));
10301016
let pos = snippet.find_uncommented("..").unwrap();
@@ -1074,11 +1060,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
10741060
let fields_str = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt));
10751061

10761062
let format_on_newline = || {
1077-
let inner_indent = make_indent(context.block_indent +
1078-
context.config.tab_spaces);
1079-
let outer_indent = make_indent(context.block_indent);
1080-
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
1081-
};
1063+
let inner_indent = make_indent(context.block_indent + context.config.tab_spaces);
1064+
let outer_indent = make_indent(context.block_indent);
1065+
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
1066+
};
10821067

10831068
match (context.config.struct_lit_style, context.config.struct_lit_multiline_style) {
10841069
(StructLitStyle::Block, _) if fields_str.contains('\n') => format_on_newline(),

src/imports.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ pub fn rewrite_use_list(width: usize,
150150
let list_str = try_opt!(write_list(&items[first_index..], &fmt));
151151

152152
Some(if path_str.is_empty() {
153-
format!("{{{}}}", list_str)
154-
} else {
155-
format!("{}::{{{}}}", path_str, list_str)
156-
})
153+
format!("{{{}}}", list_str)
154+
} else {
155+
format!("{}::{{{}}}", path_str, list_str)
156+
})
157157
}
158158

159159
// Returns true when self item was found.

src/items.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,8 @@ impl<'a> FmtVisitor<'a> {
211211
let ret_str = fd.output.rewrite(&context, self.config.max_width - indent, indent).unwrap();
212212

213213
// Args.
214-
let (one_line_budget, multi_line_budget, mut arg_indent) = self.compute_budgets_for_args(&result,
215-
indent,
216-
ret_str.len(),
217-
newline_brace);
214+
let (one_line_budget, multi_line_budget, mut arg_indent) =
215+
self.compute_budgets_for_args(&result, indent, ret_str.len(), newline_brace);
218216

219217
debug!("rewrite_fn: one_line_budget: {}, multi_line_budget: {}, arg_indent: {}",
220218
one_line_budget, multi_line_budget, arg_indent);
@@ -239,7 +237,10 @@ impl<'a> FmtVisitor<'a> {
239237
}
240238

241239
// A conservative estimation, to goal is to be over all parens in generics
242-
let args_start = generics.ty_params.last().map(|tp| end_typaram(tp)).unwrap_or(span.lo);
240+
let args_start = generics.ty_params
241+
.last()
242+
.map(|tp| end_typaram(tp))
243+
.unwrap_or(span.lo);
243244
let args_span = codemap::mk_sp(span_after(codemap::mk_sp(args_start, span.hi),
244245
"(",
245246
self.codemap),

src/utils.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, width: usize, offset: usi
211211

212212
impl Rewrite for String {
213213
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
214-
// FIXME: unnecessary clone
215-
wrap_str(self.clone(), context.config.max_width, width, offset)
214+
wrap_str(self, context.config.max_width, width, offset).map(ToOwned::to_owned)
216215
}
217216
}
218217

@@ -245,13 +244,13 @@ pub fn binary_search<C, T>(mut lo: usize, mut hi: usize, callback: C) -> Option<
245244
#[test]
246245
fn bin_search_test() {
247246
let closure = |i| {
248-
match i {
249-
4 => Ok(()),
250-
j if j > 4 => Err(Ordering::Less),
251-
j if j < 4 => Err(Ordering::Greater),
252-
_ => unreachable!(),
253-
}
254-
};
247+
match i {
248+
4 => Ok(()),
249+
j if j > 4 => Err(Ordering::Less),
250+
j if j < 4 => Err(Ordering::Greater),
251+
_ => unreachable!(),
252+
}
253+
};
255254

256255
assert_eq!(Some(()), binary_search(1, 10, &closure));
257256
assert_eq!(None, binary_search(1, 3, &closure));

tests/source/chains-no-overflow.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ fn main() {
2121
{
2222
SCRIPT_TASK_ROOT
2323
.with(|root| {
24-
// Another case of write_list failing us.
25-
*root.borrow_mut() = Some(&script_task);
26-
});
24+
*root.borrow_mut() = Some(&script_task);
25+
});
2726
});
2827

2928
let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum = xxxxxxx

tests/source/chains.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Test chain formatting.
22

33
fn main() {
4-
// Don't put chains on a single list if it wasn't so in source.
4+
// Don't put chains on a single line if it wasn't so in source.
55
let a = b .c
66
.d.1
77
.foo(|x| x + 1);
@@ -11,6 +11,8 @@ fn main() {
1111

1212
bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc.ddddddddddddddddddddddddddd.eeeeeeee();
1313

14+
// Test case where first chain element isn't a path, but is shorter than
15+
// the size of a tab.
1416
x()
1517
.y(|| match cond() { true => (), false => () });
1618

tests/source/string-lit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ formatting"#;
3030
let unicode3 = "中华Việt Nam";
3131
let unicode4 = "☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃";
3232

33-
"stuff"
33+
"stuffin'"
3434
}

tests/system.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use rustfmt::rustfmt_diff::*;
2626
static DIFF_CONTEXT_SIZE: usize = 3;
2727

2828
fn get_path_string(dir_entry: io::Result<fs::DirEntry>) -> String {
29-
let path = dir_entry.ok().expect("Couldn\'t get DirEntry.").path();
29+
let path = dir_entry.ok().expect("Couldn't get DirEntry.").path();
3030

31-
path.to_str().expect("Couldn\'t stringify path.").to_owned()
31+
path.to_str().expect("Couldn't stringify path.").to_owned()
3232
}
3333

3434
// Integration tests. The files in the tests/source are formatted and compared
@@ -40,7 +40,7 @@ fn get_path_string(dir_entry: io::Result<fs::DirEntry>) -> String {
4040
#[test]
4141
fn system_tests() {
4242
// Get all files in the tests/source directory
43-
let files = fs::read_dir("tests/source").ok().expect("Couldn\'t read source dir.");
43+
let files = fs::read_dir("tests/source").ok().expect("Couldn't read source dir.");
4444
// turn a DirEntry into a String that represents the relative path to the file
4545
let files = files.map(get_path_string);
4646

@@ -56,9 +56,9 @@ fn system_tests() {
5656
#[test]
5757
fn idempotence_tests() {
5858
// Get all files in the tests/target directory
59-
let files = fs::read_dir("tests/target").ok().expect("Couldn\'t read target dir.");
60-
let files = files.chain(fs::read_dir("tests").ok().expect("Couldn\'t read tests dir."));
61-
let files = files.chain(fs::read_dir("src/bin").ok().expect("Couldn\'t read src dir."));
59+
let files = fs::read_dir("tests/target").ok().expect("Couldn't read target dir.");
60+
let files = files.chain(fs::read_dir("tests").ok().expect("Couldn't read tests dir."));
61+
let files = files.chain(fs::read_dir("src/bin").ok().expect("Couldn't read src dir."));
6262
// turn a DirEntry into a String that represents the relative path to the file
6363
let files = files.map(get_path_string);
6464
// hack because there's no `IntoIterator` impl for `[T; N]`
@@ -139,9 +139,9 @@ fn get_config(config_file: Option<&str>) -> Box<Config> {
139139

140140
let mut def_config_file = fs::File::open(config_file_name)
141141
.ok()
142-
.expect("Couldn\'t open config.");
142+
.expect("Couldn't open config.");
143143
let mut def_config = String::new();
144-
def_config_file.read_to_string(&mut def_config).ok().expect("Couldn\'t read config.");
144+
def_config_file.read_to_string(&mut def_config).ok().expect("Couldn't read config.");
145145

146146
Box::new(Config::from_toml(&def_config))
147147
}
@@ -151,7 +151,7 @@ fn get_config(config_file: Option<&str>) -> Box<Config> {
151151
fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
152152
let file = fs::File::open(file_name)
153153
.ok()
154-
.expect(&format!("Couldn\'t read file {}.", file_name));
154+
.expect(&format!("Couldn't read file {}.", file_name));
155155
let reader = BufReader::new(file);
156156
let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)";
157157
let regex = regex::Regex::new(&pattern).ok().expect("Failed creating pattern 1.");
@@ -166,8 +166,8 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
166166
.take_while(|line| line_regex.is_match(&line))
167167
.filter_map(|line| {
168168
regex.captures_iter(&line).next().map(|capture| {
169-
(capture.at(1).expect("Couldn\'t unwrap capture.").to_owned(),
170-
capture.at(2).expect("Couldn\'t unwrap capture.").to_owned())
169+
(capture.at(1).expect("Couldn't unwrap capture.").to_owned(),
170+
capture.at(2).expect("Couldn't unwrap capture.").to_owned())
171171
})
172172
})
173173
.collect()
@@ -185,7 +185,7 @@ fn handle_result(result: HashMap<String, String>) {
185185

186186
// If file is in tests/source, compare to file with same name in tests/target.
187187
let target = get_target(&file_name, sig_comments.get("target").map(|x| &(*x)[..]));
188-
let mut f = fs::File::open(&target).ok().expect("Couldn\'t open target.");
188+
let mut f = fs::File::open(&target).ok().expect("Couldn't open target.");
189189

190190
let mut text = String::new();
191191
// TODO: speedup by running through bytes iterator

tests/target/chains-no-overflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fn main() {
2727
fffffffffffffffffffffffffffffffffff(a,
2828
{
2929
SCRIPT_TASK_ROOT.with(|root| {
30-
// Another case of write_list failing us.
31-
*root.borrow_mut() = Some(&script_task);
30+
*root.borrow_mut() =
31+
Some(&script_task);
3232
});
3333
});
3434

0 commit comments

Comments
 (0)