Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c09d7ef

Browse files
authored
Merge pull request rust-lang#3106 from sinkuu/clippy
Clippy and cleanups
2 parents 5f02be6 + e64a6d3 commit c09d7ef

File tree

14 files changed

+66
-96
lines changed

14 files changed

+66
-96
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,12 @@ fn trim_custom_comment_prefix(s: &str) -> String {
764764
// due to comment wrapping, a line that was originaly behind `#` is split over
765765
// multiple lines, which needs then to be prefixed with a `#`
766766
if !orig.trim_left().starts_with("# ") {
767-
format!("# {}", orig)
767+
Cow::from(format!("# {}", orig))
768768
} else {
769-
orig.to_string()
769+
Cow::from(orig)
770770
}
771771
} else {
772-
line.to_string()
772+
Cow::from(line)
773773
}
774774
})
775775
.collect::<Vec<_>>()

src/config/config_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ macro_rules! create_config {
112112
cloned.width_heuristics = None;
113113

114114
::toml::to_string(&cloned)
115-
.map_err(|e| format!("Could not output config: {}", e.to_string()))
115+
.map_err(|e| format!("Could not output config: {}", e))
116116
}
117117
}
118118

src/expr.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,12 +1816,7 @@ fn rewrite_unary_op(
18161816
shape: Shape,
18171817
) -> Option<String> {
18181818
// For some reason, an UnOp is not spanned like BinOp!
1819-
let operator_str = match op {
1820-
ast::UnOp::Deref => "*",
1821-
ast::UnOp::Not => "!",
1822-
ast::UnOp::Neg => "-",
1823-
};
1824-
rewrite_unary_prefix(context, operator_str, expr, shape)
1819+
rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
18251820
}
18261821

18271822
fn rewrite_assignment(

src/format-diff/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn main() {
8383
);
8484

8585
if let Err(e) = run(&opts) {
86-
println!("{}", opts.usage(&format!("{}", e)));
86+
println!("{}", opts.usage(&e.to_string()));
8787
process::exit(1);
8888
}
8989
}

src/items.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ pub fn format_impl(
779779
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
780780

781781
result.push_str(&inner_indent_str);
782-
result.push_str(visitor.buffer.to_string().trim());
782+
result.push_str(visitor.buffer.trim());
783783
result.push_str(&outer_indent_str);
784784
} else if need_newline || !context.config.empty_item_single_line() {
785785
result.push_str(&sep);
@@ -1137,7 +1137,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
11371137
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
11381138

11391139
result.push_str(&inner_indent_str);
1140-
result.push_str(visitor.buffer.to_string().trim());
1140+
result.push_str(visitor.buffer.trim());
11411141
result.push_str(&outer_indent_str);
11421142
} else if result.contains('\n') {
11431143
result.push_str(&outer_indent_str);
@@ -1543,7 +1543,7 @@ pub fn rewrite_struct_field_prefix(
15431543
rewrite_ident(context, name),
15441544
type_annotation_spacing.0
15451545
),
1546-
None => format!("{}", vis),
1546+
None => vis.to_string(),
15471547
})
15481548
}
15491549

@@ -2004,18 +2004,13 @@ fn rewrite_fn_base(
20042004
one_line_budget, multi_line_budget, arg_indent
20052005
);
20062006

2007+
result.push('(');
20072008
// Check if vertical layout was forced.
2008-
if one_line_budget == 0 {
2009-
if snuggle_angle_bracket {
2010-
result.push('(');
2011-
} else {
2012-
result.push_str("(");
2013-
if context.config.indent_style() == IndentStyle::Visual {
2014-
result.push_str(&arg_indent.to_string_with_newline(context.config));
2015-
}
2016-
}
2017-
} else {
2018-
result.push('(');
2009+
if one_line_budget == 0
2010+
&& !snuggle_angle_bracket
2011+
&& context.config.indent_style() == IndentStyle::Visual
2012+
{
2013+
result.push_str(&arg_indent.to_string_with_newline(context.config));
20192014
}
20202015

20212016
// Skip `pub(crate)`.

src/lists.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a> ListFormatting<'a> {
5050
ends_with_newline: true,
5151
preserve_newline: false,
5252
nested: false,
53-
config: config,
53+
config,
5454
}
5555
}
5656

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ fn next_space(tok: &Token) -> SpaceState {
10731073
/// when the macro is not an instance of try! (or parsing the inner expression
10741074
/// failed).
10751075
pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
1076-
if &format!("{}", mac.node.path) == "try" {
1076+
if &mac.node.path.to_string() == "try" {
10771077
let ts: TokenStream = mac.node.tts.clone().into();
10781078
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
10791079

@@ -1493,5 +1493,5 @@ fn rewrite_macro_with_items(
14931493
result.push_str(&shape.indent.to_string_with_newline(context.config));
14941494
result.push_str(closer);
14951495
result.push_str(trailing_semicolon);
1496-
return Some(result);
1496+
Some(result)
14971497
}

src/missed_spans.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'a> FmtVisitor<'a> {
120120
}
121121

122122
fn push_vertical_spaces(&mut self, mut newline_count: usize) {
123-
let offset = self.count_trailing_newlines();
123+
let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count();
124124
let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
125125
let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
126126

@@ -142,16 +142,6 @@ impl<'a> FmtVisitor<'a> {
142142
self.push_str(&blank_lines);
143143
}
144144

145-
fn count_trailing_newlines(&self) -> usize {
146-
let mut buf = &*self.buffer;
147-
let mut result = 0;
148-
while buf.ends_with('\n') {
149-
buf = &buf[..buf.len() - 1];
150-
result += 1;
151-
}
152-
result
153-
}
154-
155145
fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
156146
where
157147
F: Fn(&mut FmtVisitor, &str, &str),
@@ -271,11 +261,7 @@ impl<'a> FmtVisitor<'a> {
271261

272262
if let Some('/') = subslice.chars().nth(1) {
273263
// check that there are no contained block comments
274-
if !subslice
275-
.split('\n')
276-
.map(|s| s.trim_left())
277-
.any(|s| s.len() >= 2 && &s[0..2] == "/*")
278-
{
264+
if !subslice.lines().any(|s| s.trim_left().starts_with("/*")) {
279265
// Add a newline after line comments
280266
self.push_str("\n");
281267
}

src/overflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'a> Context<'a> {
425425
_ => expr.rewrite(self.context, shape),
426426
}
427427
}
428-
item @ _ => item.rewrite(self.context, shape),
428+
item => item.rewrite(self.context, shape),
429429
};
430430

431431
if let Some(rewrite) = rewrite {

src/source_map.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
5151
}
5252

5353
fn span_before(&self, original: Span, needle: &str) -> BytePos {
54-
self.opt_span_before(original, needle).expect(&format!(
55-
"bad span: {}: {}",
56-
needle,
57-
self.span_to_snippet(original).unwrap()
58-
))
54+
self.opt_span_before(original, needle).unwrap_or_else(|| {
55+
panic!(
56+
"bad span: {}: {}",
57+
needle,
58+
self.span_to_snippet(original).unwrap()
59+
)
60+
})
5961
}
6062

6163
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {

src/string.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn detect_url(s: &[&str], index: usize) -> Option<usize> {
172172
if s.len() < start + 8 {
173173
return None;
174174
}
175-
let prefix = s[start..start + 8].join("");
175+
let prefix = s[start..start + 8].concat();
176176
if prefix.starts_with("https://")
177177
|| prefix.starts_with("http://")
178178
|| prefix.starts_with("ftp://")
@@ -242,9 +242,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
242242
for (i, grapheme) in input[0..=index].iter().enumerate() {
243243
if is_line_feed(grapheme) {
244244
if i <= index_minus_ws {
245-
let mut line = input[0..i].join("");
245+
let mut line = &input[0..i].concat()[..];
246246
if trim_end {
247-
line = line.trim_right().to_string();
247+
line = line.trim_right();
248248
}
249249
return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
250250
}
@@ -256,7 +256,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
256256
for (i, grapheme) in input[index + 1..].iter().enumerate() {
257257
if !trim_end && is_line_feed(grapheme) {
258258
return SnippetState::EndWithLineFeed(
259-
input[0..=index + 1 + i].join("").to_string(),
259+
input[0..=index + 1 + i].concat(),
260260
index + 2 + i,
261261
);
262262
} else if not_whitespace_except_line_feed(grapheme) {
@@ -266,15 +266,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
266266
}
267267

268268
if trim_end {
269-
SnippetState::LineEnd(
270-
input[0..=index_minus_ws].join("").to_string(),
271-
index_plus_ws + 1,
272-
)
269+
SnippetState::LineEnd(input[0..=index_minus_ws].concat(), index_plus_ws + 1)
273270
} else {
274-
SnippetState::LineEnd(
275-
input[0..=index_plus_ws].join("").to_string(),
276-
index_plus_ws + 1,
277-
)
271+
SnippetState::LineEnd(input[0..=index_plus_ws].concat(), index_plus_ws + 1)
278272
}
279273
};
280274

@@ -298,15 +292,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
298292
.position(|grapheme| not_whitespace_except_line_feed(grapheme))
299293
.unwrap_or(0);
300294
return if trim_end {
301-
SnippetState::LineEnd(
302-
input[..=url_index_end].join("").to_string(),
303-
index_plus_ws + 1,
304-
)
295+
SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1)
305296
} else {
306-
return SnippetState::LineEnd(
307-
input[..=index_plus_ws].join("").to_string(),
308-
index_plus_ws + 1,
309-
);
297+
return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1);
310298
};
311299
}
312300
match input[0..max_chars]
@@ -331,7 +319,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
331319
// A boundary was found after the line limit
332320
Some(index) => break_at(max_chars + index),
333321
// No boundary to the right, the input cannot be broken
334-
None => SnippetState::EndOfInput(input.join("").to_string()),
322+
None => SnippetState::EndOfInput(input.concat()),
335323
},
336324
},
337325
}

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool {
4545
(
4646
VisibilityKind::Restricted { path: p, .. },
4747
VisibilityKind::Restricted { path: q, .. },
48-
) => format!("{}", p) == format!("{}", q),
48+
) => p.to_string() == q.to_string(),
4949
(VisibilityKind::Public, VisibilityKind::Public)
5050
| (VisibilityKind::Inherited, VisibilityKind::Inherited)
5151
| (

src/visitor.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
112112
if contains_skip(get_attrs_from_stmt(stmt)) {
113113
self.push_skipped_with_span(stmt.span());
114114
} else {
115-
let shape = self.shape().clone();
115+
let shape = self.shape();
116116
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
117117
self.push_rewrite(stmt.span(), rewrite)
118118
}
@@ -367,13 +367,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
367367
let where_span_end = snippet
368368
.find_uncommented("{")
369369
.map(|x| BytePos(x as u32) + source!(self, item.span).lo());
370-
let block_indent = self.block_indent.clone();
370+
let block_indent = self.block_indent;
371371
let rw =
372372
self.with_context(|ctx| format_impl(&ctx, item, block_indent, where_span_end));
373373
self.push_rewrite(item.span, rw);
374374
}
375375
ast::ItemKind::Trait(..) => {
376-
let block_indent = self.block_indent.clone();
376+
let block_indent = self.block_indent;
377377
let rw = self.with_context(|ctx| format_trait(&ctx, item, block_indent));
378378
self.push_rewrite(item.span, rw);
379379
}
@@ -652,20 +652,19 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
652652
ErrorKind::DeprecatedAttr,
653653
)],
654654
);
655-
} else if attr.path.segments[0].ident.to_string() == "rustfmt" {
656-
if attr.path.segments.len() == 1
657-
|| attr.path.segments[1].ident.to_string() != "skip"
658-
{
659-
let file_name = self.source_map.span_to_filename(attr.span).into();
660-
self.report.append(
661-
file_name,
662-
vec![FormattingError::from_span(
663-
attr.span,
664-
&self.source_map,
665-
ErrorKind::BadAttr,
666-
)],
667-
);
668-
}
655+
} else if attr.path.segments[0].ident.to_string() == "rustfmt"
656+
&& (attr.path.segments.len() == 1
657+
|| attr.path.segments[1].ident.to_string() != "skip")
658+
{
659+
let file_name = self.source_map.span_to_filename(attr.span).into();
660+
self.report.append(
661+
file_name,
662+
vec![FormattingError::from_span(
663+
attr.span,
664+
&self.source_map,
665+
ErrorKind::BadAttr,
666+
)],
667+
);
669668
}
670669
}
671670
if contains_skip(attrs) {
@@ -792,13 +791,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
792791
where
793792
F: Fn(&RewriteContext) -> Option<String>,
794793
{
795-
let result;
796-
let macro_rewrite_failure = {
797-
let context = self.get_context();
798-
result = f(&context);
799-
unsafe { *context.macro_rewrite_failure.as_ptr() }
800-
};
801-
self.macro_rewrite_failure |= macro_rewrite_failure;
794+
let context = self.get_context();
795+
let result = f(&context);
796+
self.macro_rewrite_failure |= *context.macro_rewrite_failure.borrow();
802797
result
803798
}
804799

0 commit comments

Comments
 (0)