Skip to content

Commit

Permalink
Clarify conditionals for clippy
Browse files Browse the repository at this point in the history
Remove double-check of is_mod in format_extern
Collapse nested if in is_block_closure_forced
Use map instead of and_then in rewrite_last_closure
Collapse nested if in impl Rewrite for ast::Ty
Collapse nested if in format_expr
Collapse nested if in merge_use_trees_inner
  • Loading branch information
workingjubilee authored and calebcartwright committed Aug 22, 2020
1 parent cd1549f commit f7ba2db
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
22 changes: 10 additions & 12 deletions src/formatting/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,20 @@ pub(crate) fn rewrite_last_closure(

// We force to use block for the body of the closure for certain kinds of expressions.
if is_block_closure_forced(context, body, capture) {
return rewrite_closure_with_block(body, &prefix, context, body_shape).and_then(
return rewrite_closure_with_block(body, &prefix, context, body_shape).map(
|body_str| {
// If the expression can fit in a single line, we need not force block closure.
if body_str.lines().count() <= 7 {
match rewrite_closure_expr(body, &prefix, context, shape) {
Some(ref single_line_body_str)
if !single_line_body_str.contains('\n') =>
{
Some(single_line_body_str.clone())
single_line_body_str.clone()
}
_ => Some(body_str),
_ => body_str,
}
} else {
Some(body_str)
body_str
}
},
);
Expand Down Expand Up @@ -415,15 +415,13 @@ fn is_block_closure_forced(
// If we are inside macro, we do not want to add or remove block from closure body.
if context.inside_macro() {
false
} else {
if let ast::ExprKind::Match(..) = expr.kind {
let is_move_closure_without_brace = capture == ast::CaptureBy::Value
&& !context.snippet(expr.span).trim().starts_with('{');
} else if let ast::ExprKind::Match(..) = expr.kind {
let is_move_closure_without_brace =
capture == ast::CaptureBy::Value && !context.snippet(expr.span).trim().starts_with('{');

is_block_closure_forced_inner(expr) || is_move_closure_without_brace
} else {
is_block_closure_forced_inner(expr)
}
is_block_closure_forced_inner(expr) || is_move_closure_without_brace
} else {
is_block_closure_forced_inner(expr)
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ pub(crate) fn format_expr(
ast::ExprKind::Lit(ref l) => {
if let Some(expr_rw) = rewrite_literal(context, l, shape) {
Some(expr_rw)
} else if let LitKind::StrRaw(_) = l.token.kind {
Some(context.snippet(l.span).trim().into())
} else {
if let LitKind::StrRaw(_) = l.token.kind {
Some(context.snippet(l.span).trim().into())
} else {
None
}
None
}
}
ast::ExprKind::Call(ref callee, ref args) => {
Expand Down
10 changes: 4 additions & 6 deletions src/formatting/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,10 @@ fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
return;
}
}
} else {
if let Some(tree) = similar_trees.max_by_key(|tree| tree.path.len()) {
if tree.path.len() > 1 {
tree.merge(&use_tree);
return;
}
} else if let Some(tree) = similar_trees.max_by_key(|tree| tree.path.len()) {
if tree.path.len() > 1 {
tree.merge(&use_tree);
return;
}
}
trees.push(use_tree);
Expand Down
10 changes: 6 additions & 4 deletions src/formatting/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,12 @@ impl Rewrite for ast::Ty {
let shape = if is_dyn { shape.offset_left(4)? } else { shape };
let mut res = bounds.rewrite(context, shape)?;
// We may have falsely removed a trailing `+` inside macro call.
if context.inside_macro() && bounds.len() == 1 {
if context.snippet(self.span).ends_with('+') && !res.ends_with('+') {
res.push('+');
}
if context.inside_macro()
&& bounds.len() == 1
&& context.snippet(self.span).ends_with('+')
&& !res.ends_with('+')
{
res.push('+');
}
if is_dyn {
Some(format!("dyn {}", res))
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub(crate) fn format_extern(
) -> Cow<'static, str> {
let format_explicit_abi = |abi: &str| Cow::from(format!(r#"extern "{}" "#, abi));
let explicit_conversion_preserves_semantics =
|| !is_mod || (is_mod && attrs.map_or(true, |a| a.is_empty()));
|| !is_mod || attrs.map_or(true, |a| a.is_empty());

match ext {
ast::Extern::None if !is_mod => Cow::from(""),
Expand Down

0 comments on commit f7ba2db

Please sign in to comment.