Skip to content

Commit

Permalink
Merge pull request rust-lang#3106 from sinkuu/clippy
Browse files Browse the repository at this point in the history
Clippy and cleanups
  • Loading branch information
nrc committed Oct 15, 2018
2 parents 5f02be6 + e64a6d3 commit c09d7ef
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 96 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,12 @@ fn trim_custom_comment_prefix(s: &str) -> String {
// due to comment wrapping, a line that was originaly behind `#` is split over
// multiple lines, which needs then to be prefixed with a `#`
if !orig.trim_left().starts_with("# ") {
format!("# {}", orig)
Cow::from(format!("# {}", orig))
} else {
orig.to_string()
Cow::from(orig)
}
} else {
line.to_string()
Cow::from(line)
}
})
.collect::<Vec<_>>()
Expand Down
2 changes: 1 addition & 1 deletion src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ macro_rules! create_config {
cloned.width_heuristics = None;

::toml::to_string(&cloned)
.map_err(|e| format!("Could not output config: {}", e.to_string()))
.map_err(|e| format!("Could not output config: {}", e))
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1816,12 +1816,7 @@ fn rewrite_unary_op(
shape: Shape,
) -> Option<String> {
// For some reason, an UnOp is not spanned like BinOp!
let operator_str = match op {
ast::UnOp::Deref => "*",
ast::UnOp::Not => "!",
ast::UnOp::Neg => "-",
};
rewrite_unary_prefix(context, operator_str, expr, shape)
rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
}

fn rewrite_assignment(
Expand Down
2 changes: 1 addition & 1 deletion src/format-diff/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn main() {
);

if let Err(e) = run(&opts) {
println!("{}", opts.usage(&format!("{}", e)));
println!("{}", opts.usage(&e.to_string()));
process::exit(1);
}
}
Expand Down
23 changes: 9 additions & 14 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ pub fn format_impl(
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);

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

result.push_str(&inner_indent_str);
result.push_str(visitor.buffer.to_string().trim());
result.push_str(visitor.buffer.trim());
result.push_str(&outer_indent_str);
} else if result.contains('\n') {
result.push_str(&outer_indent_str);
Expand Down Expand Up @@ -1543,7 +1543,7 @@ pub fn rewrite_struct_field_prefix(
rewrite_ident(context, name),
type_annotation_spacing.0
),
None => format!("{}", vis),
None => vis.to_string(),
})
}

Expand Down Expand Up @@ -2004,18 +2004,13 @@ fn rewrite_fn_base(
one_line_budget, multi_line_budget, arg_indent
);

result.push('(');
// Check if vertical layout was forced.
if one_line_budget == 0 {
if snuggle_angle_bracket {
result.push('(');
} else {
result.push_str("(");
if context.config.indent_style() == IndentStyle::Visual {
result.push_str(&arg_indent.to_string_with_newline(context.config));
}
}
} else {
result.push('(');
if one_line_budget == 0
&& !snuggle_angle_bracket
&& context.config.indent_style() == IndentStyle::Visual
{
result.push_str(&arg_indent.to_string_with_newline(context.config));
}

// Skip `pub(crate)`.
Expand Down
2 changes: 1 addition & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'a> ListFormatting<'a> {
ends_with_newline: true,
preserve_newline: false,
nested: false,
config: config,
config,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ fn next_space(tok: &Token) -> SpaceState {
/// when the macro is not an instance of try! (or parsing the inner expression
/// failed).
pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
if &format!("{}", mac.node.path) == "try" {
if &mac.node.path.to_string() == "try" {
let ts: TokenStream = mac.node.tts.clone().into();
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());

Expand Down Expand Up @@ -1493,5 +1493,5 @@ fn rewrite_macro_with_items(
result.push_str(&shape.indent.to_string_with_newline(context.config));
result.push_str(closer);
result.push_str(trailing_semicolon);
return Some(result);
Some(result)
}
18 changes: 2 additions & 16 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<'a> FmtVisitor<'a> {
}

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

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

fn count_trailing_newlines(&self) -> usize {
let mut buf = &*self.buffer;
let mut result = 0;
while buf.ends_with('\n') {
buf = &buf[..buf.len() - 1];
result += 1;
}
result
}

fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
where
F: Fn(&mut FmtVisitor, &str, &str),
Expand Down Expand Up @@ -271,11 +261,7 @@ impl<'a> FmtVisitor<'a> {

if let Some('/') = subslice.chars().nth(1) {
// 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] == "/*")
{
if !subslice.lines().any(|s| s.trim_left().starts_with("/*")) {
// Add a newline after line comments
self.push_str("\n");
}
Expand Down
2 changes: 1 addition & 1 deletion src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl<'a> Context<'a> {
_ => expr.rewrite(self.context, shape),
}
}
item @ _ => item.rewrite(self.context, shape),
item => item.rewrite(self.context, shape),
};

if let Some(rewrite) = rewrite {
Expand Down
12 changes: 7 additions & 5 deletions src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
}

fn span_before(&self, original: Span, needle: &str) -> BytePos {
self.opt_span_before(original, needle).expect(&format!(
"bad span: {}: {}",
needle,
self.span_to_snippet(original).unwrap()
))
self.opt_span_before(original, needle).unwrap_or_else(|| {
panic!(
"bad span: {}: {}",
needle,
self.span_to_snippet(original).unwrap()
)
})
}

fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
Expand Down
30 changes: 9 additions & 21 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn detect_url(s: &[&str], index: usize) -> Option<usize> {
if s.len() < start + 8 {
return None;
}
let prefix = s[start..start + 8].join("");
let prefix = s[start..start + 8].concat();
if prefix.starts_with("https://")
|| prefix.starts_with("http://")
|| prefix.starts_with("ftp://")
Expand Down Expand Up @@ -242,9 +242,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
for (i, grapheme) in input[0..=index].iter().enumerate() {
if is_line_feed(grapheme) {
if i <= index_minus_ws {
let mut line = input[0..i].join("");
let mut line = &input[0..i].concat()[..];
if trim_end {
line = line.trim_right().to_string();
line = line.trim_right();
}
return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
}
Expand All @@ -256,7 +256,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
for (i, grapheme) in input[index + 1..].iter().enumerate() {
if !trim_end && is_line_feed(grapheme) {
return SnippetState::EndWithLineFeed(
input[0..=index + 1 + i].join("").to_string(),
input[0..=index + 1 + i].concat(),
index + 2 + i,
);
} else if not_whitespace_except_line_feed(grapheme) {
Expand All @@ -266,15 +266,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
}

if trim_end {
SnippetState::LineEnd(
input[0..=index_minus_ws].join("").to_string(),
index_plus_ws + 1,
)
SnippetState::LineEnd(input[0..=index_minus_ws].concat(), index_plus_ws + 1)
} else {
SnippetState::LineEnd(
input[0..=index_plus_ws].join("").to_string(),
index_plus_ws + 1,
)
SnippetState::LineEnd(input[0..=index_plus_ws].concat(), index_plus_ws + 1)
}
};

Expand All @@ -298,15 +292,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
.position(|grapheme| not_whitespace_except_line_feed(grapheme))
.unwrap_or(0);
return if trim_end {
SnippetState::LineEnd(
input[..=url_index_end].join("").to_string(),
index_plus_ws + 1,
)
SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1)
} else {
return SnippetState::LineEnd(
input[..=index_plus_ws].join("").to_string(),
index_plus_ws + 1,
);
return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1);
};
}
match input[0..max_chars]
Expand All @@ -331,7 +319,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
// A boundary was found after the line limit
Some(index) => break_at(max_chars + index),
// No boundary to the right, the input cannot be broken
None => SnippetState::EndOfInput(input.join("").to_string()),
None => SnippetState::EndOfInput(input.concat()),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool {
(
VisibilityKind::Restricted { path: p, .. },
VisibilityKind::Restricted { path: q, .. },
) => format!("{}", p) == format!("{}", q),
) => p.to_string() == q.to_string(),
(VisibilityKind::Public, VisibilityKind::Public)
| (VisibilityKind::Inherited, VisibilityKind::Inherited)
| (
Expand Down
43 changes: 19 additions & 24 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
if contains_skip(get_attrs_from_stmt(stmt)) {
self.push_skipped_with_span(stmt.span());
} else {
let shape = self.shape().clone();
let shape = self.shape();
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
self.push_rewrite(stmt.span(), rewrite)
}
Expand Down Expand Up @@ -367,13 +367,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let where_span_end = snippet
.find_uncommented("{")
.map(|x| BytePos(x as u32) + source!(self, item.span).lo());
let block_indent = self.block_indent.clone();
let block_indent = self.block_indent;
let rw =
self.with_context(|ctx| format_impl(&ctx, item, block_indent, where_span_end));
self.push_rewrite(item.span, rw);
}
ast::ItemKind::Trait(..) => {
let block_indent = self.block_indent.clone();
let block_indent = self.block_indent;
let rw = self.with_context(|ctx| format_trait(&ctx, item, block_indent));
self.push_rewrite(item.span, rw);
}
Expand Down Expand Up @@ -652,20 +652,19 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
ErrorKind::DeprecatedAttr,
)],
);
} else if attr.path.segments[0].ident.to_string() == "rustfmt" {
if attr.path.segments.len() == 1
|| attr.path.segments[1].ident.to_string() != "skip"
{
let file_name = self.source_map.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
attr.span,
&self.source_map,
ErrorKind::BadAttr,
)],
);
}
} else if attr.path.segments[0].ident.to_string() == "rustfmt"
&& (attr.path.segments.len() == 1
|| attr.path.segments[1].ident.to_string() != "skip")
{
let file_name = self.source_map.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
attr.span,
&self.source_map,
ErrorKind::BadAttr,
)],
);
}
}
if contains_skip(attrs) {
Expand Down Expand Up @@ -792,13 +791,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
where
F: Fn(&RewriteContext) -> Option<String>,
{
let result;
let macro_rewrite_failure = {
let context = self.get_context();
result = f(&context);
unsafe { *context.macro_rewrite_failure.as_ptr() }
};
self.macro_rewrite_failure |= macro_rewrite_failure;
let context = self.get_context();
let result = f(&context);
self.macro_rewrite_failure |= *context.macro_rewrite_failure.borrow();
result
}

Expand Down

0 comments on commit c09d7ef

Please sign in to comment.