Skip to content

Commit 9672a04

Browse files
committed
Auto merge of #4500 - jeremystucki:refactoring, r=flip1995
Small refactoring changelog: none
2 parents c3d4294 + 3fc1ec1 commit 9672a04

File tree

9 files changed

+19
-55
lines changed

9 files changed

+19
-55
lines changed

clippy_lints/src/cargo_common_metadata.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, fie
4444
}
4545

4646
fn is_empty_str(value: &Option<String>) -> bool {
47-
match value {
48-
None => true,
49-
Some(value) if value.is_empty() => true,
50-
_ => false,
51-
}
47+
value.as_ref().map_or(true, String::is_empty)
5248
}
5349

5450
fn is_empty_vec(value: &[String]) -> bool {

clippy_lints/src/dbg_macro.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ impl EarlyLintPass for DbgMacro {
5959
fn tts_span(tts: TokenStream) -> Option<Span> {
6060
let mut cursor = tts.into_trees();
6161
let first = cursor.next()?.span();
62-
let span = match cursor.last() {
63-
Some(tree) => first.to(tree.span()),
64-
None => first,
65-
};
62+
let span = cursor.last().map_or(first, |tree| first.to(tree.span()));
6663
Some(span)
6764
}

clippy_lints/src/excessive_precision.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,15 @@ impl ExcessivePrecision {
9898
/// Ex `1_000_000_000.0`
9999
/// Ex `1_000_000_000.`
100100
fn dot_zero_exclusion(s: &str) -> bool {
101-
if let Some(after_dec) = s.split('.').nth(1) {
101+
s.split('.').nth(1).map_or(false, |after_dec| {
102102
let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
103103

104104
match decpart.next() {
105105
Some('0') => decpart.count() == 0,
106106
Some(_) => false,
107107
None => true,
108108
}
109-
} else {
110-
false
111-
}
109+
})
112110
}
113111

114112
fn max_digits(fty: FloatTy) -> u32 {

clippy_lints/src/functions.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,8 @@ impl<'a, 'tcx> Functions {
196196
let mut code_in_line;
197197

198198
// Skip the surrounding function decl.
199-
let start_brace_idx = match code_snippet.find('{') {
200-
Some(i) => i + 1,
201-
None => 0,
202-
};
203-
let end_brace_idx = match code_snippet.rfind('}') {
204-
Some(i) => i,
205-
None => code_snippet.len(),
206-
};
199+
let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
200+
let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
207201
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
208202

209203
for mut line in function_lines {
@@ -223,14 +217,8 @@ impl<'a, 'tcx> Functions {
223217
None => break,
224218
}
225219
} else {
226-
let multi_idx = match line.find("/*") {
227-
Some(i) => i,
228-
None => line.len(),
229-
};
230-
let single_idx = match line.find("//") {
231-
Some(i) => i,
232-
None => line.len(),
233-
};
220+
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
221+
let single_idx = line.find("//").unwrap_or_else(|| line.len());
234222
code_in_line |= multi_idx > 0 && single_idx > 0;
235223
// Implies multi_idx is below line.len()
236224
if multi_idx < single_idx {

clippy_lints/src/loops.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2003,10 +2003,7 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> {
20032003
fn is_simple_break_expr(expr: &Expr) -> bool {
20042004
match expr.node {
20052005
ExprKind::Break(dest, ref passed_expr) if dest.label.is_none() && passed_expr.is_none() => true,
2006-
ExprKind::Block(ref b, _) => match extract_first_expr(b) {
2007-
Some(subexpr) => is_simple_break_expr(subexpr),
2008-
None => false,
2009-
},
2006+
ExprKind::Block(ref b, _) => extract_first_expr(b).map_or(false, |subexpr| is_simple_break_expr(subexpr)),
20102007
_ => false,
20112008
}
20122009
}

clippy_lints/src/methods/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2797,10 +2797,9 @@ fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option<Ty<'a>> {
27972797

27982798
/// This checks whether a given type is known to implement Debug.
27992799
fn has_debug_impl<'a, 'b>(ty: Ty<'a>, cx: &LateContext<'b, 'a>) -> bool {
2800-
match cx.tcx.get_diagnostic_item(sym::debug_trait) {
2801-
Some(debug) => implements_trait(cx, ty, debug, &[]),
2802-
None => false,
2803-
}
2800+
cx.tcx
2801+
.get_diagnostic_item(sym::debug_trait)
2802+
.map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
28042803
}
28052804

28062805
enum Convention {

clippy_lints/src/methods/unnecessary_filter_map.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ pub(super) fn lint(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[hir::Expr
1818
if let hir::ExprKind::Closure(_, _, body_id, ..) = args[1].node {
1919
let body = cx.tcx.hir().body(body_id);
2020
let arg_id = body.params[0].pat.hir_id;
21-
let mutates_arg = match mutated_variables(&body.value, cx) {
22-
Some(used_mutably) => used_mutably.contains(&arg_id),
23-
None => true,
24-
};
21+
let mutates_arg =
22+
mutated_variables(&body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
2523

2624
let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value);
2725

clippy_lints/src/no_effect.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
6363
ExprKind::Struct(_, ref fields, ref base) => {
6464
!has_drop(cx, cx.tables.expr_ty(expr))
6565
&& fields.iter().all(|field| has_no_effect(cx, &field.expr))
66-
&& match *base {
67-
Some(ref base) => has_no_effect(cx, base),
68-
None => true,
69-
}
66+
&& base.as_ref().map_or(true, |base| has_no_effect(cx, base))
7067
},
7168
ExprKind::Call(ref callee, ref args) => {
7269
if let ExprKind::Path(ref qpath) = callee.node {
@@ -82,12 +79,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
8279
}
8380
},
8481
ExprKind::Block(ref block, _) => {
85-
block.stmts.is_empty()
86-
&& if let Some(ref expr) = block.expr {
87-
has_no_effect(cx, expr)
88-
} else {
89-
false
90-
}
82+
block.stmts.is_empty() && block.expr.as_ref().map_or(false, |expr| has_no_effect(cx, expr))
9183
},
9284
_ => false,
9385
}

clippy_lints/src/utils/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
398398

399399
/// Returns `true` if the provided `def_id` is an entrypoint to a program.
400400
pub fn is_entrypoint_fn(cx: &LateContext<'_, '_>, def_id: DefId) -> bool {
401-
if let Some((entry_fn_def_id, _)) = cx.tcx.entry_fn(LOCAL_CRATE) {
402-
return def_id == entry_fn_def_id;
403-
}
404-
false
401+
cx.tcx
402+
.entry_fn(LOCAL_CRATE)
403+
.map_or(false, |(entry_fn_def_id, _)| def_id == entry_fn_def_id)
405404
}
406405

407406
/// Gets the name of the item the expression is in, if available.

0 commit comments

Comments
 (0)