Skip to content

Commit

Permalink
fix clippy::needless_borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskrgr authored and calebcartwright committed Oct 28, 2021
1 parent d454e81 commit c493ee4
Show file tree
Hide file tree
Showing 25 changed files with 91 additions and 91 deletions.
4 changes: 2 additions & 2 deletions src/attr.rs
Expand Up @@ -451,7 +451,7 @@ impl Rewrite for [ast::Attribute] {
if next.is_doc_comment() {
let snippet = context.snippet(missing_span);
let (_, mlb) = has_newlines_before_after_comment(snippet);
result.push_str(&mlb);
result.push_str(mlb);
}
}
result.push('\n');
Expand Down Expand Up @@ -484,7 +484,7 @@ impl Rewrite for [ast::Attribute] {
if next.is_doc_comment() {
let snippet = context.snippet(missing_span);
let (_, mlb) = has_newlines_before_after_comment(snippet);
result.push_str(&mlb);
result.push_str(mlb);
}
}
result.push('\n');
Expand Down
2 changes: 1 addition & 1 deletion src/attr/doc_comment.rs
Expand Up @@ -77,7 +77,7 @@ mod tests {
) {
assert_eq!(
expected_comment,
format!("{}", DocCommentFormatter::new(&literal, style))
format!("{}", DocCommentFormatter::new(literal, style))
);
}
}
6 changes: 3 additions & 3 deletions src/cargo-fmt/main.rs
Expand Up @@ -401,12 +401,12 @@ fn get_targets_root_only(

fn get_targets_recursive(
manifest_path: Option<&Path>,
mut targets: &mut BTreeSet<Target>,
targets: &mut BTreeSet<Target>,
visited: &mut BTreeSet<String>,
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path)?;
for package in &metadata.packages {
add_targets(&package.targets, &mut targets);
add_targets(&package.targets, targets);

// Look for local dependencies using information available since cargo v1.51
// It's theoretically possible someone could use a newer version of rustfmt with
Expand All @@ -427,7 +427,7 @@ fn get_targets_recursive(
.any(|p| p.manifest_path.eq(&manifest_path))
{
visited.insert(dependency.name.to_owned());
get_targets_recursive(Some(&manifest_path), &mut targets, visited)?;
get_targets_recursive(Some(&manifest_path), targets, visited)?;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/chains.rs
Expand Up @@ -568,7 +568,7 @@ impl<'a> ChainFormatterShared<'a> {
} else {
self.rewrites
.iter()
.map(|rw| utils::unicode_str_width(&rw))
.map(|rw| utils::unicode_str_width(rw))
.sum()
} + last.tries;
let one_line_budget = if self.child_count == 1 {
Expand Down Expand Up @@ -673,7 +673,7 @@ impl<'a> ChainFormatterShared<'a> {
ChainItemKind::Comment(_, CommentPosition::Top) => result.push_str(&connector),
_ => result.push_str(&connector),
}
result.push_str(&rewrite);
result.push_str(rewrite);
}

Some(result)
Expand Down
14 changes: 7 additions & 7 deletions src/comment.rs
Expand Up @@ -563,7 +563,7 @@ impl<'a> CommentRewrite<'a> {
result.push_str(line);
result.push_str(match iter.peek() {
Some(next_line) if next_line.is_empty() => sep.trim_end(),
Some(..) => &sep,
Some(..) => sep,
None => "",
});
}
Expand Down Expand Up @@ -622,7 +622,7 @@ impl<'a> CommentRewrite<'a> {
let is_last = i == count_newlines(orig);

if let Some(ref mut ib) = self.item_block {
if ib.add_line(&line) {
if ib.add_line(line) {
return false;
}
self.is_prev_line_multi_line = false;
Expand Down Expand Up @@ -684,8 +684,8 @@ impl<'a> CommentRewrite<'a> {
self.item_block = None;
if let Some(stripped) = line.strip_prefix("```") {
self.code_block_attr = Some(CodeBlockAttribute::new(stripped))
} else if self.fmt.config.wrap_comments() && ItemizedBlock::is_itemized_line(&line) {
let ib = ItemizedBlock::new(&line);
} else if self.fmt.config.wrap_comments() && ItemizedBlock::is_itemized_line(line) {
let ib = ItemizedBlock::new(line);
self.item_block = Some(ib);
return false;
}
Expand Down Expand Up @@ -941,7 +941,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
{
(&line[4..], true)
} else if let CommentStyle::Custom(opener) = *style {
if let Some(ref stripped) = line.strip_prefix(opener) {
if let Some(stripped) = line.strip_prefix(opener) {
(stripped, true)
} else {
(&line[opener.trim_end().len()..], false)
Expand Down Expand Up @@ -1570,7 +1570,7 @@ pub(crate) fn recover_comment_removed(
context.parse_sess.span_to_filename(span),
vec![FormattingError::from_span(
span,
&context.parse_sess,
context.parse_sess,
ErrorKind::LostComment,
)],
);
Expand Down Expand Up @@ -1675,7 +1675,7 @@ impl<'a> Iterator for CommentReducer<'a> {
fn remove_comment_header(comment: &str) -> &str {
if comment.starts_with("///") || comment.starts_with("//!") {
&comment[3..]
} else if let Some(ref stripped) = comment.strip_prefix("//") {
} else if let Some(stripped) = comment.strip_prefix("//") {
stripped
} else if (comment.starts_with("/**") && !comment.starts_with("/**/"))
|| comment.starts_with("/*!")
Expand Down
4 changes: 2 additions & 2 deletions src/emitter/checkstyle.rs
Expand Up @@ -121,15 +121,15 @@ mod tests {
format!(r#"<file name="{}">"#, bin_file),
format!(
r#"<error line="2" severity="warning" message="Should be `{}`" />"#,
XmlEscaped(&r#" println!("Hello, world!");"#),
XmlEscaped(r#" println!("Hello, world!");"#),
),
String::from("</file>"),
];
let exp_lib_xml = vec![
format!(r#"<file name="{}">"#, lib_file),
format!(
r#"<error line="2" severity="warning" message="Should be `{}`" />"#,
XmlEscaped(&r#" println!("Greetings!");"#),
XmlEscaped(r#" println!("Greetings!");"#),
),
String::from("</file>"),
];
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/diff.rs
Expand Up @@ -23,7 +23,7 @@ impl Emitter for DiffEmitter {
}: FormattedFile<'_>,
) -> Result<EmitterResult, io::Error> {
const CONTEXT_SIZE: usize = 3;
let mismatch = make_diff(&original_text, formatted_text, CONTEXT_SIZE);
let mismatch = make_diff(original_text, formatted_text, CONTEXT_SIZE);
let has_diff = !mismatch.is_empty();

if has_diff {
Expand Down
12 changes: 6 additions & 6 deletions src/expr.rs
Expand Up @@ -257,7 +257,7 @@ pub(crate) fn format_expr(
}
_ => false,
},
ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, &expr),
ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr),
_ => false,
}
}
Expand Down Expand Up @@ -423,7 +423,7 @@ fn rewrite_empty_block(
prefix: &str,
shape: Shape,
) -> Option<String> {
if block_has_statements(&block) {
if block_has_statements(block) {
return None;
}

Expand Down Expand Up @@ -1148,7 +1148,7 @@ pub(crate) fn is_empty_block(
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
) -> bool {
!block_has_statements(&block)
!block_has_statements(block)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
}
Expand Down Expand Up @@ -1621,7 +1621,7 @@ fn rewrite_struct_lit<'a>(
};

let fields_str =
wrap_struct_field(context, &attrs, &fields_str, shape, v_shape, one_line_width)?;
wrap_struct_field(context, attrs, &fields_str, shape, v_shape, one_line_width)?;
Some(format!("{} {{{}}}", path_str, fields_str))

// FIXME if context.config.indent_style() == Visual, but we run out
Expand Down Expand Up @@ -1888,7 +1888,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
shape: Shape,
rhs_tactics: RhsTactics,
) -> Option<String> {
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
Expand Down Expand Up @@ -1947,7 +1947,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(

if contains_comment {
let rhs = rhs.trim_start();
combine_strs_with_missing_comments(context, &lhs, &rhs, between_span, shape, allow_extend)
combine_strs_with_missing_comments(context, &lhs, rhs, between_span, shape, allow_extend)
} else {
Some(lhs + &rhs)
}
Expand Down
4 changes: 2 additions & 2 deletions src/formatting.rs
Expand Up @@ -155,7 +155,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
let snippet_provider = self.parse_session.snippet_provider(module.span);
let mut visitor = FmtVisitor::from_parse_sess(
&self.parse_session,
&self.config,
self.config,
&snippet_provider,
self.report.clone(),
);
Expand All @@ -180,7 +180,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
&mut visitor.buffer,
&path,
&visitor.skipped_range.borrow(),
&self.config,
self.config,
&self.report,
);

Expand Down
4 changes: 2 additions & 2 deletions src/imports.rs
Expand Up @@ -275,7 +275,7 @@ impl UseTree {
shape: Shape,
) -> Option<String> {
let vis = self.visibility.as_ref().map_or(Cow::from(""), |vis| {
crate::utils::format_visibility(context, &vis)
crate::utils::format_visibility(context, vis)
});
let use_str = self
.rewrite(context, shape.offset_left(vis.len())?)
Expand Down Expand Up @@ -929,7 +929,7 @@ impl Rewrite for UseTree {
fn rewrite(&self, context: &RewriteContext<'_>, mut shape: Shape) -> Option<String> {
let mut result = String::with_capacity(256);
let mut iter = self.path.iter().peekable();
while let Some(ref segment) = iter.next() {
while let Some(segment) = iter.next() {
let segment_str = segment.rewrite(context, shape)?;
result.push_str(&segment_str);
if iter.peek().is_some() {
Expand Down
30 changes: 15 additions & 15 deletions src/items.rs
Expand Up @@ -226,7 +226,7 @@ impl<'a> FnSig<'a> {
fn to_str(&self, context: &RewriteContext<'_>) -> String {
let mut result = String::with_capacity(128);
// Vis defaultness constness unsafety abi.
result.push_str(&*format_visibility(context, &self.visibility));
result.push_str(&*format_visibility(context, self.visibility));
result.push_str(format_defaultness(self.defaultness));
result.push_str(format_constness(self.constness));
result.push_str(format_async(&self.is_async));
Expand Down Expand Up @@ -1220,7 +1220,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
} else if fits_single_line {
Cow::from(" ")
} else {
shape.indent.to_string_with_newline(&context.config)
shape.indent.to_string_with_newline(context.config)
};

Some(format!("{}{}{}", generic_bounds_str, space, where_str))
Expand All @@ -1238,7 +1238,7 @@ pub(crate) fn format_trait_alias(
let alias = rewrite_ident(context, ident);
// 6 = "trait ", 2 = " ="
let g_shape = shape.offset_left(6)?.sub_width(2)?;
let generics_str = rewrite_generics(context, &alias, generics, g_shape)?;
let generics_str = rewrite_generics(context, alias, generics, g_shape)?;
let vis_str = format_visibility(context, vis);
let lhs = format!("{}trait {} =", vis_str, generics_str);
// 1 = ";"
Expand Down Expand Up @@ -1386,7 +1386,7 @@ fn format_empty_struct_or_tuple(
closer: &str,
) {
// 3 = " {}" or "();"
let used_width = last_line_used_width(&result, offset.width()) + 3;
let used_width = last_line_used_width(result, offset.width()) + 3;
if used_width > context.config.max_width() {
result.push_str(&offset.to_string_with_newline(context.config))
}
Expand Down Expand Up @@ -2066,7 +2066,7 @@ fn rewrite_explicit_self(
)?;
Some(combine_strs_with_missing_comments(
context,
&param_attrs,
param_attrs,
&format!("&{} {}self", lifetime_str, mut_str),
span,
shape,
Expand All @@ -2075,7 +2075,7 @@ fn rewrite_explicit_self(
}
None => Some(combine_strs_with_missing_comments(
context,
&param_attrs,
param_attrs,
&format!("&{}self", mut_str),
span,
shape,
Expand All @@ -2091,7 +2091,7 @@ fn rewrite_explicit_self(

Some(combine_strs_with_missing_comments(
context,
&param_attrs,
param_attrs,
&format!("{}self: {}", format_mutability(mutability), type_str),
span,
shape,
Expand All @@ -2100,7 +2100,7 @@ fn rewrite_explicit_self(
}
ast::SelfKind::Value(mutability) => Some(combine_strs_with_missing_comments(
context,
&param_attrs,
param_attrs,
&format!("{}self", format_mutability(mutability)),
span,
shape,
Expand Down Expand Up @@ -2226,7 +2226,7 @@ fn rewrite_fn_base(
}

// Skip `pub(crate)`.
let lo_after_visibility = get_bytepos_after_visibility(&fn_sig.visibility, span);
let lo_after_visibility = get_bytepos_after_visibility(fn_sig.visibility, span);
// A conservative estimation, the goal is to be over all parens in generics
let params_start = fn_sig
.generics
Expand Down Expand Up @@ -2984,7 +2984,7 @@ fn format_header(
let mut result = String::with_capacity(128);
let shape = Shape::indented(offset, context.config);

result.push_str(&format_visibility(context, vis).trim());
result.push_str(format_visibility(context, vis).trim());

// Check for a missing comment between the visibility and the item name.
let after_vis = vis.span.hi();
Expand All @@ -3005,7 +3005,7 @@ fn format_header(
}
}

result.push_str(&rewrite_ident(context, ident));
result.push_str(rewrite_ident(context, ident));

result
}
Expand Down Expand Up @@ -3133,7 +3133,7 @@ impl Rewrite for ast::ForeignItem {
let inner_attrs = inner_attributes(&self.attrs);
let fn_ctxt = visit::FnCtxt::Foreign;
visitor.visit_fn(
visit::FnKind::Fn(fn_ctxt, self.ident, &fn_sig, &self.vis, Some(body)),
visit::FnKind::Fn(fn_ctxt, self.ident, fn_sig, &self.vis, Some(body)),
generics,
&fn_sig.decl,
self.span,
Expand All @@ -3146,7 +3146,7 @@ impl Rewrite for ast::ForeignItem {
context,
shape.indent,
self.ident,
&FnSig::from_method_sig(&fn_sig, generics, &self.vis),
&FnSig::from_method_sig(fn_sig, generics, &self.vis),
span,
FnBraceStyle::None,
)
Expand All @@ -3171,7 +3171,7 @@ impl Rewrite for ast::ForeignItem {
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
**ty_alias_kind;
rewrite_type(
&context,
context,
shape.indent,
self.ident,
&self.vis,
Expand Down Expand Up @@ -3229,7 +3229,7 @@ fn rewrite_attrs(
combine_strs_with_missing_comments(
context,
&attrs_str,
&item_str,
item_str,
missed_span,
shape,
allow_extend,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -283,7 +283,7 @@ impl FormatReport {
writeln!(
t,
"{}",
FormatReportFormatterBuilder::new(&self)
FormatReportFormatterBuilder::new(self)
.enable_colors(true)
.build()
)?;
Expand All @@ -297,7 +297,7 @@ impl FormatReport {
impl fmt::Display for FormatReport {
// Prints all the formatting errors.
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(fmt, "{}", FormatReportFormatterBuilder::new(&self).build())?;
write!(fmt, "{}", FormatReportFormatterBuilder::new(self).build())?;
Ok(())
}
}
Expand Down

0 comments on commit c493ee4

Please sign in to comment.