diff --git a/clippy_lints/src/cargo_common_metadata.rs b/clippy_lints/src/cargo_common_metadata.rs index 23532c4b0474..115bb9ee8c8a 100644 --- a/clippy_lints/src/cargo_common_metadata.rs +++ b/clippy_lints/src/cargo_common_metadata.rs @@ -44,11 +44,7 @@ fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, fie } fn is_empty_str(value: &Option) -> bool { - match value { - None => true, - Some(value) if value.is_empty() => true, - _ => false, - } + value.as_ref().map_or(true, String::is_empty) } fn is_empty_vec(value: &[String]) -> bool { diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 1dcdaac9b862..cf21d63880f5 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -59,9 +59,6 @@ impl EarlyLintPass for DbgMacro { fn tts_span(tts: TokenStream) -> Option { let mut cursor = tts.into_trees(); let first = cursor.next()?.span(); - let span = match cursor.last() { - Some(tree) => first.to(tree.span()), - None => first, - }; + let span = cursor.last().map_or(first, |tree| first.to(tree.span())); Some(span) } diff --git a/clippy_lints/src/excessive_precision.rs b/clippy_lints/src/excessive_precision.rs index e996bac3911e..879fb79c0bd3 100644 --- a/clippy_lints/src/excessive_precision.rs +++ b/clippy_lints/src/excessive_precision.rs @@ -98,7 +98,7 @@ impl ExcessivePrecision { /// Ex `1_000_000_000.0` /// Ex `1_000_000_000.` fn dot_zero_exclusion(s: &str) -> bool { - if let Some(after_dec) = s.split('.').nth(1) { + s.split('.').nth(1).map_or(false, |after_dec| { let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E'); match decpart.next() { @@ -106,9 +106,7 @@ fn dot_zero_exclusion(s: &str) -> bool { Some(_) => false, None => true, } - } else { - false - } + }) } fn max_digits(fty: FloatTy) -> u32 { diff --git a/clippy_lints/src/functions.rs b/clippy_lints/src/functions.rs index dfcab83faee8..a609c74c172d 100644 --- a/clippy_lints/src/functions.rs +++ b/clippy_lints/src/functions.rs @@ -196,14 +196,8 @@ impl<'a, 'tcx> Functions { let mut code_in_line; // Skip the surrounding function decl. - let start_brace_idx = match code_snippet.find('{') { - Some(i) => i + 1, - None => 0, - }; - let end_brace_idx = match code_snippet.rfind('}') { - Some(i) => i, - None => code_snippet.len(), - }; + let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1); + let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len()); let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines(); for mut line in function_lines { @@ -223,14 +217,8 @@ impl<'a, 'tcx> Functions { None => break, } } else { - let multi_idx = match line.find("/*") { - Some(i) => i, - None => line.len(), - }; - let single_idx = match line.find("//") { - Some(i) => i, - None => line.len(), - }; + let multi_idx = line.find("/*").unwrap_or_else(|| line.len()); + let single_idx = line.find("//").unwrap_or_else(|| line.len()); code_in_line |= multi_idx > 0 && single_idx > 0; // Implies multi_idx is below line.len() if multi_idx < single_idx { diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 581ed0744fec..9d58d98dbb1f 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -2003,10 +2003,7 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> { fn is_simple_break_expr(expr: &Expr) -> bool { match expr.node { ExprKind::Break(dest, ref passed_expr) if dest.label.is_none() && passed_expr.is_none() => true, - ExprKind::Block(ref b, _) => match extract_first_expr(b) { - Some(subexpr) => is_simple_break_expr(subexpr), - None => false, - }, + ExprKind::Block(ref b, _) => extract_first_expr(b).map_or(false, |subexpr| is_simple_break_expr(subexpr)), _ => false, } } diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 3dd12d3ff4f8..5578dd2654e2 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2797,10 +2797,9 @@ fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option> { /// This checks whether a given type is known to implement Debug. fn has_debug_impl<'a, 'b>(ty: Ty<'a>, cx: &LateContext<'b, 'a>) -> bool { - match cx.tcx.get_diagnostic_item(sym::debug_trait) { - Some(debug) => implements_trait(cx, ty, debug, &[]), - None => false, - } + cx.tcx + .get_diagnostic_item(sym::debug_trait) + .map_or(false, |debug| implements_trait(cx, ty, debug, &[])) } enum Convention { diff --git a/clippy_lints/src/methods/unnecessary_filter_map.rs b/clippy_lints/src/methods/unnecessary_filter_map.rs index 1d562566fdf8..465404c5ae07 100644 --- a/clippy_lints/src/methods/unnecessary_filter_map.rs +++ b/clippy_lints/src/methods/unnecessary_filter_map.rs @@ -18,10 +18,8 @@ pub(super) fn lint(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[hir::Expr if let hir::ExprKind::Closure(_, _, body_id, ..) = args[1].node { let body = cx.tcx.hir().body(body_id); let arg_id = body.params[0].pat.hir_id; - let mutates_arg = match mutated_variables(&body.value, cx) { - Some(used_mutably) => used_mutably.contains(&arg_id), - None => true, - }; + let mutates_arg = + mutated_variables(&body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id)); let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value); diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index 078aa6923f2d..576446d35a76 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -63,10 +63,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool { ExprKind::Struct(_, ref fields, ref base) => { !has_drop(cx, cx.tables.expr_ty(expr)) && fields.iter().all(|field| has_no_effect(cx, &field.expr)) - && match *base { - Some(ref base) => has_no_effect(cx, base), - None => true, - } + && base.as_ref().map_or(true, |base| has_no_effect(cx, base)) }, ExprKind::Call(ref callee, ref args) => { if let ExprKind::Path(ref qpath) = callee.node { @@ -82,12 +79,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool { } }, ExprKind::Block(ref block, _) => { - block.stmts.is_empty() - && if let Some(ref expr) = block.expr { - has_no_effect(cx, expr) - } else { - false - } + block.stmts.is_empty() && block.expr.as_ref().map_or(false, |expr| has_no_effect(cx, expr)) }, _ => false, } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index bbf9572f8fb4..faa578c62760 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -398,10 +398,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option, def_id: DefId) -> bool { - if let Some((entry_fn_def_id, _)) = cx.tcx.entry_fn(LOCAL_CRATE) { - return def_id == entry_fn_def_id; - } - false + cx.tcx + .entry_fn(LOCAL_CRATE) + .map_or(false, |(entry_fn_def_id, _)| def_id == entry_fn_def_id) } /// Gets the name of the item the expression is in, if available.