Auto merge of #4500 - jeremystucki:refactoring, r=flip1995

Small refactoring

changelog: none
This commit is contained in:
bors 2019-09-06 08:41:56 +00:00
commit 9672a0400c
9 changed files with 19 additions and 55 deletions

View file

@ -44,11 +44,7 @@ fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, fie
}
fn is_empty_str(value: &Option<String>) -> 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 {

View file

@ -59,9 +59,6 @@ impl EarlyLintPass for DbgMacro {
fn tts_span(tts: TokenStream) -> Option<Span> {
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)
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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,
}
}

View file

@ -2797,10 +2797,9 @@ fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option<Ty<'a>> {
/// 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 {

View file

@ -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);

View file

@ -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,
}

View file

@ -398,10 +398,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
/// Returns `true` if the provided `def_id` is an entrypoint to a program.
pub fn is_entrypoint_fn(cx: &LateContext<'_, '_>, 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.