Skip to content

Commit 21734b8

Browse files
matthiaskrgrcalebcartwright
authored andcommitted
Fix clippy warnings
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
1 parent cabc1e7 commit 21734b8

15 files changed

+45
-51
lines changed

src/formatting/closures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub(crate) fn rewrite_closure(
9090
let between_span = Span::between(arg_span, first_span);
9191
if contains_comment(context.snippet(between_span)) {
9292
return rewrite_closure_with_block(body, &prefix, context, body_shape).and_then(|rw| {
93-
let mut parts = rw.splitn(2, "\n");
93+
let mut parts = rw.splitn(2, '\n');
9494
let head = parts.next()?;
9595
let rest = parts.next()?;
9696
let block_shape = shape.block_indent(context.config.tab_spaces());

src/formatting/comment.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ fn identify_comment(
385385
shape,
386386
config,
387387
is_doc_comment || style.is_doc_comment(),
388-
)?
388+
)
389389
};
390390
if rest.is_empty() {
391391
Some(rewritten_first_group)
@@ -720,8 +720,8 @@ impl<'a> CommentRewrite<'a> {
720720

721721
self.code_block_attr = None;
722722
self.item_block = None;
723-
if line.starts_with("```") {
724-
self.code_block_attr = Some(CodeBlockAttribute::new(&line[3..]))
723+
if let Some(line) = line.strip_prefix("```") {
724+
self.code_block_attr = Some(CodeBlockAttribute::new(&line))
725725
} else if self.fmt.config.wrap_comments() && ItemizedBlock::is_itemized_line(&line) {
726726
let ib = ItemizedBlock::new(&line);
727727
self.item_block = Some(ib);
@@ -819,7 +819,7 @@ fn rewrite_comment_inner(
819819
shape: Shape,
820820
config: &Config,
821821
is_doc_comment: bool,
822-
) -> Option<String> {
822+
) -> String {
823823
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);
824824

825825
let line_breaks = count_newlines(orig.trim_end());
@@ -853,7 +853,7 @@ fn rewrite_comment_inner(
853853
}
854854
}
855855

856-
Some(rewriter.finish())
856+
rewriter.finish()
857857
}
858858

859859
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
@@ -982,8 +982,8 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
982982
{
983983
(&line[4..], true)
984984
} else if let CommentStyle::Custom(opener) = *style {
985-
if line.starts_with(opener) {
986-
(&line[opener.len()..], true)
985+
if let Some(line) = line.strip_prefix(opener) {
986+
(&line, true)
987987
} else {
988988
(&line[opener.trim_end().len()..], false)
989989
}
@@ -1002,8 +1002,8 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
10021002
|| line.starts_with("**")
10031003
{
10041004
(&line[2..], line.chars().nth(1).unwrap() == ' ')
1005-
} else if line.starts_with('*') {
1006-
(&line[1..], false)
1005+
} else if let Some(line) = line.strip_prefix('*') {
1006+
(&line, false)
10071007
} else {
10081008
(line, line.starts_with(' '))
10091009
}
@@ -1595,18 +1595,18 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
15951595
}
15961596

15971597
/// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1598-
/// (if it fits in the width/offset, else return `None`), else return `new`
1598+
/// and `new` otherwise
15991599
pub(crate) fn recover_comment_removed(
16001600
new: String,
16011601
span: Span,
16021602
context: &RewriteContext<'_>,
1603-
) -> Option<String> {
1603+
) -> String {
16041604
let snippet = context.snippet(span);
16051605
let includes_comment = contains_comment(snippet);
16061606
if snippet != new && includes_comment && changed_comment_content(snippet, &new) {
1607-
Some(snippet.to_owned())
1607+
snippet.to_owned()
16081608
} else {
1609-
Some(new)
1609+
new
16101610
}
16111611
}
16121612

src/formatting/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ pub(crate) fn format_expr(
444444
};
445445

446446
expr_rw
447-
.and_then(|expr_str| recover_comment_removed(expr_str, expr.span, context))
447+
.map(|expr_str| recover_comment_removed(expr_str, expr.span, context))
448448
.and_then(|expr_str| {
449449
let attrs = outer_attributes(&expr.attrs);
450450
let attrs_str = attrs.rewrite(context, shape)?;

src/formatting/items.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ pub(crate) fn format_impl(
834834
// there is only one where-clause predicate
835835
// recover the suppressed comma in single line where_clause formatting
836836
if generics.where_clause.predicates.len() == 1 {
837-
result.push_str(",");
837+
result.push(',');
838838
}
839839
result.push_str(&format!("{}{{{}}}", sep, sep));
840840
} else {
@@ -1737,18 +1737,18 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
17371737
pub(crate) fn rewrite_struct_field_prefix(
17381738
context: &RewriteContext<'_>,
17391739
field: &ast::StructField,
1740-
) -> Option<String> {
1740+
) -> String {
17411741
let vis = format_visibility(context, &field.vis);
17421742
let type_annotation_spacing = type_annotation_spacing(context.config);
1743-
Some(match field.ident {
1743+
match field.ident {
17441744
Some(name) => format!(
17451745
"{}{}{}:",
17461746
vis,
17471747
rewrite_ident(context, name),
17481748
type_annotation_spacing.0
17491749
),
17501750
None => vis.to_string(),
1751-
})
1751+
}
17521752
}
17531753

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

17701770
let type_annotation_spacing = type_annotation_spacing(context.config);
1771-
let prefix = rewrite_struct_field_prefix(context, field)?;
1771+
let prefix = rewrite_struct_field_prefix(context, field);
17721772

17731773
let attrs_str = field.attrs.rewrite(context, shape)?;
17741774
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
@@ -1945,7 +1945,7 @@ fn rewrite_static(
19451945
comments_span,
19461946
true,
19471947
)
1948-
.and_then(|res| recover_comment_removed(res, static_parts.span, context))
1948+
.map(|res| recover_comment_removed(res, static_parts.span, context))
19491949
.or_else(|| {
19501950
let nested_indent = offset.block_indent(context.config);
19511951
let ty_span_hi = static_parts.ty.span.hi();
@@ -2357,7 +2357,7 @@ fn rewrite_fn_base(
23572357
ret_str_len,
23582358
fn_brace_style,
23592359
multi_line_ret_str,
2360-
)?;
2360+
);
23612361

23622362
debug!(
23632363
"rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, param_indent: {:?}",
@@ -2744,7 +2744,7 @@ fn compute_budgets_for_params(
27442744
ret_str_len: usize,
27452745
fn_brace_style: FnBraceStyle,
27462746
force_vertical_layout: bool,
2747-
) -> Option<(usize, usize, Indent)> {
2747+
) -> (usize, usize, Indent) {
27482748
debug!(
27492749
"compute_budgets_for_params {} {:?}, {}, {:?}",
27502750
result.len(),
@@ -2781,7 +2781,7 @@ fn compute_budgets_for_params(
27812781
}
27822782
};
27832783

2784-
return Some((one_line_budget, multi_line_budget, indent));
2784+
return (one_line_budget, multi_line_budget, indent);
27852785
}
27862786
}
27872787

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

27992799
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> FnBraceStyle {

src/formatting/lists.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ pub(crate) fn extract_post_comment(
639639
let post_snippet = post_snippet[..comment_end].trim();
640640
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
641641
post_snippet[1..].trim_matches(white_space)
642-
} else if post_snippet.starts_with(separator) {
643-
post_snippet[separator.len()..].trim_matches(white_space)
642+
} else if let Some(post_snippet) = post_snippet.strip_prefix(separator) {
643+
post_snippet.trim_matches(white_space)
644644
}
645645
// not comment or over two lines
646646
else if post_snippet.ends_with(',')

src/formatting/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ impl MacroBranch {
14241424
// Undo our replacement of macro variables.
14251425
// FIXME: this could be *much* more efficient.
14261426
for (old, new) in &substs {
1427-
if old_body.find(new).is_some() {
1427+
if old_body.contains(new) {
14281428
debug!("rewrite_macro_def: bailing matching variable: `{}`", new);
14291429
return None;
14301430
}

src/formatting/overflow.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ impl<'a> OverflowableItem<'a> {
137137
}
138138

139139
pub(crate) fn is_expr(&self) -> bool {
140-
match self {
141-
OverflowableItem::Expr(..) => true,
142-
OverflowableItem::MacroArg(MacroArg::Expr(..)) => true,
143-
_ => false,
144-
}
140+
matches!(
141+
self,
142+
OverflowableItem::Expr(..) | OverflowableItem::MacroArg(MacroArg::Expr(..))
143+
)
145144
}
146145

147146
pub(crate) fn is_nested_call(&self) -> bool {

src/formatting/pairs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ where
280280
};
281281
let new_line_width = infix_with_sep.len() - 1 + rhs_result.len() + pp.suffix.len();
282282
let rhs_with_sep = if separator_place == SeparatorPlace::Front && new_line_width > shape.width {
283-
let s: String = String::from(infix_with_sep);
284-
infix_with_sep = s.trim_end().to_string();
283+
infix_with_sep = infix_with_sep.trim_end().to_string();
285284
format!("{}{}", indent_str, rhs_result.trim_start())
286285
} else {
287286
rhs_result

src/formatting/patterns.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,17 @@ fn rewrite_struct_pat(
312312
if fields_str.contains('\n') || fields_str.len() > one_line_width {
313313
// Add a missing trailing comma.
314314
if context.config.trailing_comma() == SeparatorTactic::Never {
315-
fields_str.push_str(",");
315+
fields_str.push(',');
316316
}
317-
fields_str.push_str("\n");
317+
fields_str.push('\n');
318318
fields_str.push_str(&nested_shape.indent.to_string(context.config));
319319
fields_str.push_str("..");
320320
} else {
321321
if !fields_str.is_empty() {
322322
// there are preceding struct fields being matched on
323323
if tactic == DefinitiveListTactic::Vertical {
324324
// if the tactic is Vertical, write_list already added a trailing ,
325-
fields_str.push_str(" ");
325+
fields_str.push(' ');
326326
} else {
327327
fields_str.push_str(", ");
328328
}

src/formatting/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ fn format_stmt(
113113
}
114114
ast::StmtKind::MacCall(..) | ast::StmtKind::Item(..) | ast::StmtKind::Empty => None,
115115
};
116-
result.and_then(|res| recover_comment_removed(res, stmt.span(), context))
116+
result.map(|res| recover_comment_removed(res, stmt.span(), context))
117117
}

src/formatting/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub(crate) fn rewrite_string<'a>(
101101
if is_new_line(grapheme) {
102102
// take care of blank lines
103103
result = trim_end_but_line_feed(fmt.trim_end, result);
104-
result.push_str("\n");
104+
result.push('\n');
105105
if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
106106
result.push_str(&indent_without_newline);
107107
result.push_str(fmt.line_start);

src/formatting/syntux/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a> ParserBuilder<'a> {
9090
rustc_span::FileName::Custom("stdin".to_owned()),
9191
text,
9292
)
93-
.map_err(|db| Some(db)),
93+
.map_err(Some),
9494
}
9595
}
9696
}

src/formatting/types.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl Rewrite for ast::Ty {
654654
let mut_str = format_mutability(mt.mutbl);
655655
let mut_len = mut_str.len();
656656
let mut result = String::with_capacity(128);
657-
result.push_str("&");
657+
result.push('&');
658658
let ref_hi = context.snippet_provider.span_after(self.span(), "&");
659659
let mut cmnt_lo = ref_hi;
660660

@@ -677,7 +677,7 @@ impl Rewrite for ast::Ty {
677677
} else {
678678
result.push_str(&lt_str);
679679
}
680-
result.push_str(" ");
680+
result.push(' ');
681681
cmnt_lo = lifetime.ident.span.hi();
682682
}
683683

@@ -988,11 +988,7 @@ fn join_bounds_inner(
988988
true,
989989
)
990990
.map(|v| (v, trailing_span, extendable)),
991-
_ => Some((
992-
String::from(strs) + &trailing_str,
993-
trailing_span,
994-
extendable,
995-
)),
991+
_ => Some((strs + &trailing_str, trailing_span, extendable)),
996992
}
997993
},
998994
)?;

src/formatting/vertical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl AlignedItem for ast::StructField {
5050
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
5151
};
5252
let attrs_extendable = self.ident.is_none() && is_attributes_extendable(&attrs_str);
53-
rewrite_struct_field_prefix(context, self).and_then(|field_str| {
53+
Some(rewrite_struct_field_prefix(context, self)).and_then(|field_str| {
5454
combine_strs_with_missing_comments(
5555
context,
5656
&attrs_str,

src/rustfmt/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl CliOptions for Opt {
361361
config.set().error_on_unformatted(true);
362362
}
363363
if let Some(ref edition) = self.edition {
364-
config.set().edition((*edition).clone());
364+
config.set().edition(*edition);
365365
}
366366
if let Some(ref inline_configs) = self.inline_config {
367367
for inline_config in inline_configs {
@@ -527,7 +527,7 @@ fn format(opt: Opt) -> Result<i32> {
527527
println!(
528528
"Using rustfmt config file(s) {}",
529529
paths
530-
.into_iter()
530+
.iter()
531531
.map(|p| p.display().to_string())
532532
.collect::<Vec<_>>()
533533
.join(","),

0 commit comments

Comments
 (0)