Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
Fixes

clippy::single_char_pattern
clippy::manual_strip
clippy::unneccessary_wraps
clippy::redundant_closure
clippy::single_char_add_str
clippy::search_is_some
clippy::unnecessary_wrap
clippy::clone_on_copy
clippy::into_iter_on_ref
  • Loading branch information
matthiaskrgr authored and calebcartwright committed Dec 19, 2020
1 parent cabc1e7 commit 21734b8
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/formatting/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(crate) fn rewrite_closure(
let between_span = Span::between(arg_span, first_span);
if contains_comment(context.snippet(between_span)) {
return rewrite_closure_with_block(body, &prefix, context, body_shape).and_then(|rw| {
let mut parts = rw.splitn(2, "\n");
let mut parts = rw.splitn(2, '\n');
let head = parts.next()?;
let rest = parts.next()?;
let block_shape = shape.block_indent(context.config.tab_spaces());
Expand Down
26 changes: 13 additions & 13 deletions src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ fn identify_comment(
shape,
config,
is_doc_comment || style.is_doc_comment(),
)?
)
};
if rest.is_empty() {
Some(rewritten_first_group)
Expand Down Expand Up @@ -720,8 +720,8 @@ impl<'a> CommentRewrite<'a> {

self.code_block_attr = None;
self.item_block = None;
if line.starts_with("```") {
self.code_block_attr = Some(CodeBlockAttribute::new(&line[3..]))
if let Some(line) = line.strip_prefix("```") {
self.code_block_attr = Some(CodeBlockAttribute::new(&line))
} else if self.fmt.config.wrap_comments() && ItemizedBlock::is_itemized_line(&line) {
let ib = ItemizedBlock::new(&line);
self.item_block = Some(ib);
Expand Down Expand Up @@ -819,7 +819,7 @@ fn rewrite_comment_inner(
shape: Shape,
config: &Config,
is_doc_comment: bool,
) -> Option<String> {
) -> String {
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);

let line_breaks = count_newlines(orig.trim_end());
Expand Down Expand Up @@ -853,7 +853,7 @@ fn rewrite_comment_inner(
}
}

Some(rewriter.finish())
rewriter.finish()
}

const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
Expand Down Expand Up @@ -982,8 +982,8 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
{
(&line[4..], true)
} else if let CommentStyle::Custom(opener) = *style {
if line.starts_with(opener) {
(&line[opener.len()..], true)
if let Some(line) = line.strip_prefix(opener) {
(&line, true)
} else {
(&line[opener.trim_end().len()..], false)
}
Expand All @@ -1002,8 +1002,8 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
|| line.starts_with("**")
{
(&line[2..], line.chars().nth(1).unwrap() == ' ')
} else if line.starts_with('*') {
(&line[1..], false)
} else if let Some(line) = line.strip_prefix('*') {
(&line, false)
} else {
(line, line.starts_with(' '))
}
Expand Down Expand Up @@ -1595,18 +1595,18 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
}

/// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
/// (if it fits in the width/offset, else return `None`), else return `new`
/// and `new` otherwise
pub(crate) fn recover_comment_removed(
new: String,
span: Span,
context: &RewriteContext<'_>,
) -> Option<String> {
) -> String {
let snippet = context.snippet(span);
let includes_comment = contains_comment(snippet);
if snippet != new && includes_comment && changed_comment_content(snippet, &new) {
Some(snippet.to_owned())
snippet.to_owned()
} else {
Some(new)
new
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ pub(crate) fn format_expr(
};

expr_rw
.and_then(|expr_str| recover_comment_removed(expr_str, expr.span, context))
.map(|expr_str| recover_comment_removed(expr_str, expr.span, context))
.and_then(|expr_str| {
let attrs = outer_attributes(&expr.attrs);
let attrs_str = attrs.rewrite(context, shape)?;
Expand Down
20 changes: 10 additions & 10 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ pub(crate) fn format_impl(
// there is only one where-clause predicate
// recover the suppressed comma in single line where_clause formatting
if generics.where_clause.predicates.len() == 1 {
result.push_str(",");
result.push(',');
}
result.push_str(&format!("{}{{{}}}", sep, sep));
} else {
Expand Down Expand Up @@ -1737,18 +1737,18 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
pub(crate) fn rewrite_struct_field_prefix(
context: &RewriteContext<'_>,
field: &ast::StructField,
) -> Option<String> {
) -> String {
let vis = format_visibility(context, &field.vis);
let type_annotation_spacing = type_annotation_spacing(context.config);
Some(match field.ident {
match field.ident {
Some(name) => format!(
"{}{}{}:",
vis,
rewrite_ident(context, name),
type_annotation_spacing.0
),
None => vis.to_string(),
})
}
}

impl Rewrite for ast::StructField {
Expand All @@ -1768,7 +1768,7 @@ pub(crate) fn rewrite_struct_field(
}

let type_annotation_spacing = type_annotation_spacing(context.config);
let prefix = rewrite_struct_field_prefix(context, field)?;
let prefix = rewrite_struct_field_prefix(context, field);

let attrs_str = field.attrs.rewrite(context, shape)?;
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
Expand Down Expand Up @@ -1945,7 +1945,7 @@ fn rewrite_static(
comments_span,
true,
)
.and_then(|res| recover_comment_removed(res, static_parts.span, context))
.map(|res| recover_comment_removed(res, static_parts.span, context))
.or_else(|| {
let nested_indent = offset.block_indent(context.config);
let ty_span_hi = static_parts.ty.span.hi();
Expand Down Expand Up @@ -2357,7 +2357,7 @@ fn rewrite_fn_base(
ret_str_len,
fn_brace_style,
multi_line_ret_str,
)?;
);

debug!(
"rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, param_indent: {:?}",
Expand Down Expand Up @@ -2744,7 +2744,7 @@ fn compute_budgets_for_params(
ret_str_len: usize,
fn_brace_style: FnBraceStyle,
force_vertical_layout: bool,
) -> Option<(usize, usize, Indent)> {
) -> (usize, usize, Indent) {
debug!(
"compute_budgets_for_params {} {:?}, {}, {:?}",
result.len(),
Expand Down Expand Up @@ -2781,7 +2781,7 @@ fn compute_budgets_for_params(
}
};

return Some((one_line_budget, multi_line_budget, indent));
return (one_line_budget, multi_line_budget, indent);
}
}

Expand All @@ -2793,7 +2793,7 @@ fn compute_budgets_for_params(
// Account for `)` and possibly ` {`.
IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
};
Some((0, context.budget(used_space), new_indent))
(0, context.budget(used_space), new_indent)
}

fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> FnBraceStyle {
Expand Down
4 changes: 2 additions & 2 deletions src/formatting/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ pub(crate) fn extract_post_comment(
let post_snippet = post_snippet[..comment_end].trim();
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
post_snippet[1..].trim_matches(white_space)
} else if post_snippet.starts_with(separator) {
post_snippet[separator.len()..].trim_matches(white_space)
} else if let Some(post_snippet) = post_snippet.strip_prefix(separator) {
post_snippet.trim_matches(white_space)
}
// not comment or over two lines
else if post_snippet.ends_with(',')
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ impl MacroBranch {
// Undo our replacement of macro variables.
// FIXME: this could be *much* more efficient.
for (old, new) in &substs {
if old_body.find(new).is_some() {
if old_body.contains(new) {
debug!("rewrite_macro_def: bailing matching variable: `{}`", new);
return None;
}
Expand Down
9 changes: 4 additions & 5 deletions src/formatting/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ impl<'a> OverflowableItem<'a> {
}

pub(crate) fn is_expr(&self) -> bool {
match self {
OverflowableItem::Expr(..) => true,
OverflowableItem::MacroArg(MacroArg::Expr(..)) => true,
_ => false,
}
matches!(
self,
OverflowableItem::Expr(..) | OverflowableItem::MacroArg(MacroArg::Expr(..))
)
}

pub(crate) fn is_nested_call(&self) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions src/formatting/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ where
};
let new_line_width = infix_with_sep.len() - 1 + rhs_result.len() + pp.suffix.len();
let rhs_with_sep = if separator_place == SeparatorPlace::Front && new_line_width > shape.width {
let s: String = String::from(infix_with_sep);
infix_with_sep = s.trim_end().to_string();
infix_with_sep = infix_with_sep.trim_end().to_string();
format!("{}{}", indent_str, rhs_result.trim_start())
} else {
rhs_result
Expand Down
6 changes: 3 additions & 3 deletions src/formatting/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,17 @@ fn rewrite_struct_pat(
if fields_str.contains('\n') || fields_str.len() > one_line_width {
// Add a missing trailing comma.
if context.config.trailing_comma() == SeparatorTactic::Never {
fields_str.push_str(",");
fields_str.push(',');
}
fields_str.push_str("\n");
fields_str.push('\n');
fields_str.push_str(&nested_shape.indent.to_string(context.config));
fields_str.push_str("..");
} else {
if !fields_str.is_empty() {
// there are preceding struct fields being matched on
if tactic == DefinitiveListTactic::Vertical {
// if the tactic is Vertical, write_list already added a trailing ,
fields_str.push_str(" ");
fields_str.push(' ');
} else {
fields_str.push_str(", ");
}
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ fn format_stmt(
}
ast::StmtKind::MacCall(..) | ast::StmtKind::Item(..) | ast::StmtKind::Empty => None,
};
result.and_then(|res| recover_comment_removed(res, stmt.span(), context))
result.map(|res| recover_comment_removed(res, stmt.span(), context))
}
2 changes: 1 addition & 1 deletion src/formatting/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub(crate) fn rewrite_string<'a>(
if is_new_line(grapheme) {
// take care of blank lines
result = trim_end_but_line_feed(fmt.trim_end, result);
result.push_str("\n");
result.push('\n');
if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
result.push_str(&indent_without_newline);
result.push_str(fmt.line_start);
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/syntux/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a> ParserBuilder<'a> {
rustc_span::FileName::Custom("stdin".to_owned()),
text,
)
.map_err(|db| Some(db)),
.map_err(Some),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/formatting/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ impl Rewrite for ast::Ty {
let mut_str = format_mutability(mt.mutbl);
let mut_len = mut_str.len();
let mut result = String::with_capacity(128);
result.push_str("&");
result.push('&');
let ref_hi = context.snippet_provider.span_after(self.span(), "&");
let mut cmnt_lo = ref_hi;

Expand All @@ -677,7 +677,7 @@ impl Rewrite for ast::Ty {
} else {
result.push_str(&lt_str);
}
result.push_str(" ");
result.push(' ');
cmnt_lo = lifetime.ident.span.hi();
}

Expand Down Expand Up @@ -988,11 +988,7 @@ fn join_bounds_inner(
true,
)
.map(|v| (v, trailing_span, extendable)),
_ => Some((
String::from(strs) + &trailing_str,
trailing_span,
extendable,
)),
_ => Some((strs + &trailing_str, trailing_span, extendable)),
}
},
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl AlignedItem for ast::StructField {
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
};
let attrs_extendable = self.ident.is_none() && is_attributes_extendable(&attrs_str);
rewrite_struct_field_prefix(context, self).and_then(|field_str| {
Some(rewrite_struct_field_prefix(context, self)).and_then(|field_str| {
combine_strs_with_missing_comments(
context,
&attrs_str,
Expand Down
4 changes: 2 additions & 2 deletions src/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl CliOptions for Opt {
config.set().error_on_unformatted(true);
}
if let Some(ref edition) = self.edition {
config.set().edition((*edition).clone());
config.set().edition(*edition);
}
if let Some(ref inline_configs) = self.inline_config {
for inline_config in inline_configs {
Expand Down Expand Up @@ -527,7 +527,7 @@ fn format(opt: Opt) -> Result<i32> {
println!(
"Using rustfmt config file(s) {}",
paths
.into_iter()
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(","),
Expand Down

0 comments on commit 21734b8

Please sign in to comment.