Skip to content

Commit dec307b

Browse files
authored
Merge pull request #2584 from sinkuu/cleanup
Misc cleanups
2 parents e2d801f + 56e10aa commit dec307b

File tree

10 files changed

+31
-66
lines changed

10 files changed

+31
-66
lines changed

src/chains.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
104104
};
105105
let parent_rewrite = parent
106106
.rewrite(context, parent_shape)
107-
.map(|parent_rw| parent_rw + &repeat_try(prefix_try_num))?;
107+
.map(|parent_rw| parent_rw + &"?".repeat(prefix_try_num))?;
108108
let parent_rewrite_contains_newline = parent_rewrite.contains('\n');
109109
let is_small_parent = parent_rewrite.len() <= context.config.tab_spaces();
110110

@@ -297,7 +297,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
297297
join_rewrites(&rewrites, &connector)
298298
)
299299
};
300-
let result = format!("{}{}", result, repeat_try(suffix_try_num));
300+
let result = format!("{}{}", result, "?".repeat(suffix_try_num));
301301
if context.config.indent_style() == IndentStyle::Visual {
302302
wrap_str(result, context.config.max_width(), shape)
303303
} else {
@@ -316,20 +316,14 @@ fn chain_only_try(exprs: &[ast::Expr]) -> bool {
316316
})
317317
}
318318

319-
// Try to rewrite and replace the last non-try child. Return `true` if
320-
// replacing succeeds.
321-
fn repeat_try(try_count: usize) -> String {
322-
iter::repeat("?").take(try_count).collect::<String>()
323-
}
324-
325319
fn rewrite_try(
326320
expr: &ast::Expr,
327321
try_count: usize,
328322
context: &RewriteContext,
329323
shape: Shape,
330324
) -> Option<String> {
331325
let sub_expr = expr.rewrite(context, shape.sub_width(try_count)?)?;
332-
Some(format!("{}{}", sub_expr, repeat_try(try_count)))
326+
Some(format!("{}{}", sub_expr, "?".repeat(try_count)))
333327
}
334328

335329
fn join_rewrites(rewrites: &[String], connector: &str) -> String {
@@ -340,7 +334,7 @@ fn join_rewrites(rewrites: &[String], connector: &str) -> String {
340334
if rewrite != "?" {
341335
result.push_str(connector);
342336
}
343-
result.push_str(&rewrite[..]);
337+
result.push_str(&rewrite);
344338
}
345339

346340
result

src/config/options.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,7 @@ impl IgnoreList {
271271
}
272272

273273
fn skip_file_inner(&self, file: &Path) -> bool {
274-
for path in &self.0 {
275-
if file.starts_with(path) {
276-
return true;
277-
}
278-
}
279-
280-
false
274+
self.0.iter().any(|path| file.starts_with(path))
281275
}
282276

283277
pub fn skip_file(&self, file: &FileName) -> bool {

src/git-rustfmt/main.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,7 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> {
3232

3333
let mut pruned_prefixes = vec![];
3434
for p1 in prefixes {
35-
let mut include = true;
36-
if !p1.starts_with("src/bin/") {
37-
for p2 in &pruned_prefixes {
38-
if p1.starts_with(p2) {
39-
include = false;
40-
break;
41-
}
42-
}
43-
}
44-
if include {
35+
if p1.starts_with("src/bin/") || pruned_prefixes.iter().all(|p2| !p1.starts_with(p2)) {
4536
pruned_prefixes.push(p1);
4637
}
4738
}
@@ -50,17 +41,10 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> {
5041
files
5142
.into_iter()
5243
.filter(|f| {
53-
let mut include = true;
5444
if f.ends_with("mod.rs") || f.ends_with("lib.rs") || f.starts_with("src/bin/") {
5545
return true;
5646
}
57-
for pp in &pruned_prefixes {
58-
if f.starts_with(pp) {
59-
include = false;
60-
break;
61-
}
62-
}
63-
include
47+
pruned_prefixes.iter().all(|pp| !f.starts_with(pp))
6448
})
6549
.collect()
6650
}

src/items.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,21 @@ impl<'a> FnSig<'a> {
204204
fn_kind: &'a visit::FnKind,
205205
generics: &'a ast::Generics,
206206
decl: &'a ast::FnDecl,
207-
defualtness: ast::Defaultness,
207+
defaultness: ast::Defaultness,
208208
) -> FnSig<'a> {
209209
match *fn_kind {
210210
visit::FnKind::ItemFn(_, unsafety, constness, abi, visibility, _) => FnSig {
211211
decl,
212212
generics,
213213
abi,
214214
constness: constness.node,
215-
defaultness: defualtness,
215+
defaultness,
216216
unsafety,
217217
visibility: visibility.clone(),
218218
},
219219
visit::FnKind::Method(_, method_sig, vis, _) => {
220220
let mut fn_sig = FnSig::from_method_sig(method_sig, generics);
221-
fn_sig.defaultness = defualtness;
221+
fn_sig.defaultness = defaultness;
222222
if let Some(vis) = vis {
223223
fn_sig.visibility = vis.clone();
224224
}
@@ -1287,7 +1287,7 @@ fn format_tuple_struct(
12871287
result.push(')');
12881288
} else {
12891289
let shape = Shape::indented(offset, context.config).sub_width(1)?;
1290-
let fields = &fields.iter().collect::<Vec<_>>()[..];
1290+
let fields = &fields.iter().collect::<Vec<_>>();
12911291
result = overflow::rewrite_with_parens(
12921292
context,
12931293
&result,
@@ -2312,7 +2312,7 @@ fn rewrite_generics(
23122312
return Some(ident.to_owned());
23132313
}
23142314

2315-
let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>()[..];
2315+
let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
23162316
overflow::rewrite_with_angle_brackets(context, ident, params, shape, span)
23172317
}
23182318

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ extern crate unicode_segmentation;
3434
use std::collections::HashMap;
3535
use std::fmt;
3636
use std::io::{self, stdout, BufRead, Write};
37-
use std::iter::repeat;
3837
use std::panic::{catch_unwind, AssertUnwindSafe};
3938
use std::path::PathBuf;
4039
use std::rc::Rc;
@@ -200,7 +199,7 @@ impl FormatReport {
200199
for (file, errors) in &self.file_error_map {
201200
for error in errors {
202201
let prefix_space_len = error.line.to_string().len();
203-
let prefix_spaces: String = repeat(" ").take(1 + prefix_space_len).collect();
202+
let prefix_spaces = " ".repeat(1 + prefix_space_len);
204203

205204
// First line: the overview of error
206205
t.fg(term::color::RED)?;
@@ -259,8 +258,8 @@ impl FormatReport {
259258
}
260259

261260
fn target_str(space_len: usize, target_len: usize) -> String {
262-
let empty_line: String = repeat(" ").take(space_len).collect();
263-
let overflowed: String = repeat("^").take(target_len).collect();
261+
let empty_line = " ".repeat(space_len);
262+
let overflowed = "^".repeat(target_len);
264263
empty_line + &overflowed
265264
}
266265

@@ -270,7 +269,7 @@ impl fmt::Display for FormatReport {
270269
for (file, errors) in &self.file_error_map {
271270
for error in errors {
272271
let prefix_space_len = error.line.to_string().len();
273-
let prefix_spaces: String = repeat(" ").take(1 + prefix_space_len).collect();
272+
let prefix_spaces = " ".repeat(1 + prefix_space_len);
274273

275274
let error_line_buffer = if error.line_buffer.is_empty() {
276275
String::from(" ")

src/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn rewrite_macro_inner(
239239
overflow::rewrite_with_parens(
240240
context,
241241
&macro_name,
242-
&arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..],
242+
&arg_vec.iter().map(|e| &*e).collect::<Vec<_>>(),
243243
shape,
244244
mac.span,
245245
context.config.width_heuristics().fn_call_width,
@@ -301,7 +301,7 @@ pub fn rewrite_macro_inner(
301301
};
302302
}
303303
// Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
304-
let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..];
304+
let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>();
305305
let rewrite = rewrite_array(
306306
macro_name,
307307
arg_vec,
@@ -991,7 +991,7 @@ fn next_space(tok: &Token) -> SpaceState {
991991
/// when the macro is not an instance of try! (or parsing the inner expression
992992
/// failed).
993993
pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
994-
if &format!("{}", mac.node.path)[..] == "try" {
994+
if &format!("{}", mac.node.path) == "try" {
995995
let ts: TokenStream = mac.node.tts.clone().into();
996996
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
997997

src/missed_spans.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use std::borrow::Cow;
12-
use std::iter::repeat;
1312

1413
use syntax::codemap::{BytePos, FileName, Pos, Span};
1514

@@ -128,7 +127,7 @@ impl<'a> FmtVisitor<'a> {
128127
}
129128
}
130129

131-
let blank_lines: String = repeat('\n').take(newline_count).collect();
130+
let blank_lines = "\n".repeat(newline_count);
132131
self.push_str(&blank_lines);
133132
}
134133

@@ -182,7 +181,7 @@ impl<'a> FmtVisitor<'a> {
182181
let mut status = SnippetStatus::new(char_pos.line);
183182

184183
let snippet = &*match self.config.write_mode() {
185-
WriteMode::Coverage => replace_chars(old_snippet),
184+
WriteMode::Coverage => Cow::from(replace_chars(old_snippet)),
186185
_ => Cow::from(old_snippet),
187186
};
188187

@@ -327,11 +326,9 @@ impl<'a> FmtVisitor<'a> {
327326
}
328327
}
329328

330-
fn replace_chars(string: &str) -> Cow<str> {
331-
Cow::from(
332-
string
333-
.chars()
334-
.map(|ch| if ch.is_whitespace() { ch } else { 'X' })
335-
.collect::<String>(),
336-
)
329+
fn replace_chars(string: &str) -> String {
330+
string
331+
.chars()
332+
.map(|ch| if ch.is_whitespace() { ch } else { 'X' })
333+
.collect()
337334
}

src/patterns.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,12 @@ fn rewrite_tuple_pat(
361361
// add comma if `(x,)`
362362
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
363363
let path_str = path_str.unwrap_or_default();
364-
let mut pat_ref_vec = Vec::with_capacity(pat_vec.len());
365-
for pat in pat_vec {
366-
pat_ref_vec.push(pat);
367-
}
364+
let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
368365

369366
overflow::rewrite_with_parens(
370367
&context,
371368
&path_str,
372-
&pat_ref_vec[..],
369+
&pat_ref_vec,
373370
shape,
374371
span,
375372
context.config.max_width(),
@@ -408,7 +405,7 @@ fn count_wildcard_suffix_len(
408405
}) {
409406
suffix_len += 1;
410407

411-
if item.pre_comment.is_some() || item.post_comment.is_some() {
408+
if item.has_comment() {
412409
break;
413410
}
414411
}

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn rewrite_segment(
247247
let generics_str = overflow::rewrite_with_angle_brackets(
248248
context,
249249
"",
250-
&param_list.iter().map(|e| &*e).collect::<Vec<_>>()[..],
250+
&param_list.iter().map(|e| &*e).collect::<Vec<_>>(),
251251
shape,
252252
mk_sp(*span_lo, span_hi),
253253
)?;

src/visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
335335
let snippet = self.snippet(item.span);
336336
let where_span_end = snippet
337337
.find_uncommented("{")
338-
.map(|x| (BytePos(x as u32)) + source!(self, item.span).lo());
338+
.map(|x| BytePos(x as u32) + source!(self, item.span).lo());
339339
let rw = format_impl(&self.get_context(), item, self.block_indent, where_span_end);
340340
self.push_rewrite(item.span, rw);
341341
}

0 commit comments

Comments
 (0)