Skip to content

Commit

Permalink
Auto merge of #4500 - jeremystucki:refactoring, r=flip1995
Browse files Browse the repository at this point in the history
Small refactoring

changelog: none
  • Loading branch information
bors committed Sep 6, 2019
2 parents c3d4294 + 3fc1ec1 commit 9672a04
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 55 deletions.
6 changes: 1 addition & 5 deletions clippy_lints/src/cargo_common_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 2 additions & 4 deletions clippy_lints/src/excessive_precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,15 @@ 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() {
Some('0') => decpart.count() == 0,
Some(_) => false,
None => true,
}
} else {
false
}
})
}

fn max_digits(fty: FloatTy) -> u32 {
Expand Down
20 changes: 4 additions & 16 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions clippy_lints/src/methods/unnecessary_filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 2 additions & 10 deletions clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9672a04

Please sign in to comment.