Skip to content

Commit 2e215b9

Browse files
committed
Used clippy to clean itself
1 parent e147e9c commit 2e215b9

19 files changed

+65
-154
lines changed

clippy_lints/src/attrs.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,11 @@ fn is_relevant_trait(cx: &LateContext<'_, '_>, item: &TraitItem<'_>) -> bool {
442442
}
443443

444444
fn is_relevant_block(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, block: &Block<'_>) -> bool {
445-
if let Some(stmt) = block.stmts.first() {
446-
match &stmt.kind {
445+
block.stmts.first().map_or(block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e)), |stmt| match &stmt.kind {
447446
StmtKind::Local(_) => true,
448447
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
449448
_ => false,
450-
}
451-
} else {
452-
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e))
453-
}
449+
})
454450
}
455451

456452
fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr<'_>) -> bool {
@@ -460,11 +456,7 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp
460456
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
461457
ExprKind::Call(path_expr, _) => {
462458
if let ExprKind::Path(qpath) = &path_expr.kind {
463-
if let Some(fun_id) = tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
464-
!match_def_path(cx, fun_id, &paths::BEGIN_PANIC)
465-
} else {
466-
true
467-
}
459+
tables.qpath_res(qpath, path_expr.hir_id).opt_def_id().map_or(true, |fun_id| !match_def_path(cx, fun_id, &paths::BEGIN_PANIC))
468460
} else {
469461
true
470462
}

clippy_lints/src/if_let_mutex.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> {
137137

138138
impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
139139
fn same_mutex(&self, cx: &LateContext<'_, '_>, op_mutex: &Expr<'_>) -> bool {
140-
if let Some(arm_mutex) = self.found_mutex {
141-
SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex)
142-
} else {
143-
false
144-
}
140+
self.found_mutex.map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
145141
}
146142
}
147143

clippy_lints/src/len_zero.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,10 @@ fn has_is_empty(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
303303
let ty = &walk_ptrs_ty(cx.tables.expr_ty(expr));
304304
match ty.kind {
305305
ty::Dynamic(ref tt, ..) => {
306-
if let Some(principal) = tt.principal() {
307-
cx.tcx
306+
tt.principal().map_or(false, |principal| cx.tcx
308307
.associated_items(principal.def_id())
309308
.in_definition_order()
310-
.any(|item| is_is_empty(cx, &item))
311-
} else {
312-
false
313-
}
309+
.any(|item| is_is_empty(cx, &item)))
314310
},
315311
ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id),
316312
ty::Adt(id, _) => has_is_empty_impl(cx, id.did),

clippy_lints/src/literal_representation.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,11 @@ impl LiteralDigitGrouping {
264264

265265
let (part, mistyped_suffixes, missing_char) = if let Some((_, exponent)) = &mut num_lit.exponent {
266266
(exponent, &["32", "64"][..], 'f')
267-
} else if let Some(fraction) = &mut num_lit.fraction {
268-
(fraction, &["32", "64"][..], 'f')
269267
} else {
270-
(&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i')
268+
num_lit.fraction.as_mut().map_or(
269+
(&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i'),
270+
|fraction| (fraction, &["32", "64"][..], 'f')
271+
)
271272
};
272273

273274
let mut split = part.rsplit('_');

clippy_lints/src/loops.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
687687
}
688688
},
689689
ExprKind::Break(_, ref e) | ExprKind::Ret(ref e) => {
690-
if let Some(ref e) = *e {
691-
combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak)
692-
} else {
693-
NeverLoopResult::AlwaysBreak
694-
}
690+
e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak))
695691
},
696692
ExprKind::InlineAsm(ref asm) => asm
697693
.operands
@@ -1882,11 +1878,7 @@ fn is_iterable_array<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'_, 'tcx>) -> bool {
18821878
// IntoIterator is currently only implemented for array sizes <= 32 in rustc
18831879
match ty.kind {
18841880
ty::Array(_, n) => {
1885-
if let Some(val) = n.try_eval_usize(cx.tcx, cx.param_env) {
1886-
(0..=32).contains(&val)
1887-
} else {
1888-
false
1889-
}
1881+
n.try_eval_usize(cx.tcx, cx.param_env).map_or(false, |val| (0..=32).contains(&val))
18901882
},
18911883
_ => false,
18921884
}
@@ -1899,11 +1891,7 @@ fn extract_expr_from_first_stmt<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<
18991891
return None;
19001892
}
19011893
if let StmtKind::Local(ref local) = block.stmts[0].kind {
1902-
if let Some(expr) = local.init {
1903-
Some(expr)
1904-
} else {
1905-
None
1906-
}
1894+
local.init.map(|expr| expr)
19071895
} else {
19081896
None
19091897
}
@@ -2023,15 +2011,11 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
20232011
if let PatKind::Binding(.., ident, _) = local.pat.kind {
20242012
self.name = Some(ident.name);
20252013

2026-
self.state = if let Some(ref init) = local.init {
2027-
if is_integer_const(&self.cx, init, 0) {
2014+
self.state = local.init.as_ref().map_or(VarState::Declared, |init| if is_integer_const(&self.cx, init, 0) {
20282015
VarState::Warn
20292016
} else {
20302017
VarState::Declared
2031-
}
2032-
} else {
2033-
VarState::Declared
2034-
}
2018+
})
20352019
}
20362020
}
20372021
}

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,11 +2467,7 @@ fn derefs_to_slice<'a, 'tcx>(
24672467
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
24682468
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym!(vec_type)),
24692469
ty::Array(_, size) => {
2470-
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
2471-
size < 32
2472-
} else {
2473-
false
2474-
}
2470+
size.try_eval_usize(cx.tcx, cx.param_env).map_or(false, |size| size < 32)
24752471
},
24762472
ty::Ref(_, inner, _) => may_slice(cx, inner),
24772473
_ => false,

clippy_lints/src/methods/unnecessary_filter_map.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ fn check_expression<'a, 'tcx>(
8282
(true, true)
8383
},
8484
hir::ExprKind::Block(ref block, _) => {
85-
if let Some(expr) = &block.expr {
86-
check_expression(cx, arg_id, &expr)
87-
} else {
88-
(false, false)
89-
}
85+
block.expr.as_ref().map_or((false, false), |expr| check_expression(cx, arg_id, &expr))
9086
},
9187
hir::ExprKind::Match(_, arms, _) => {
9288
let mut found_mapping = false;

clippy_lints/src/minmax.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ fn fetch_const<'a>(
8787
if args.len() != 2 {
8888
return None;
8989
}
90-
if let Some(c) = constant_simple(cx, cx.tables, &args[0]) {
91-
if constant_simple(cx, cx.tables, &args[1]).is_none() {
92-
// otherwise ignore
93-
Some((m, c, &args[1]))
94-
} else {
95-
None
90+
constant_simple(cx, cx.tables, &args[0]).map_or_else(
91+
|| {
92+
constant_simple(cx, cx.tables, &args[1]).map(|c| (c, &args[0]))
93+
},
94+
|c| {
95+
if constant_simple(cx, cx.tables, &args[1]).is_none() {
96+
// otherwise ignore
97+
Some((c, &args[1]))
98+
} else {
99+
None
100+
}
96101
}
97-
} else if let Some(c) = constant_simple(cx, cx.tables, &args[1]) {
98-
Some((m, c, &args[0]))
99-
} else {
100-
None
101-
}
102+
).map(|(c, arg)| (m, c, arg))
102103
}

clippy_lints/src/misc.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,16 +682,12 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr<'_>, other: &Expr<'_>, l
682682
/// `unused_variables`'s idea
683683
/// of what it means for an expression to be "used".
684684
fn is_used(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
685-
if let Some(parent) = get_parent_expr(cx, expr) {
686-
match parent.kind {
685+
get_parent_expr(cx, expr).map_or(true, |parent| match parent.kind {
687686
ExprKind::Assign(_, ref rhs, _) | ExprKind::AssignOp(_, _, ref rhs) => {
688687
SpanlessEq::new(cx).eq_expr(rhs, expr)
689688
},
690689
_ => is_used(cx, parent),
691-
}
692-
} else {
693-
true
694-
}
690+
})
695691
}
696692

697693
/// Tests whether an expression is in a macro expansion (e.g., something

clippy_lints/src/option_if_let_else.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ declare_clippy_lint! {
3737
///
3838
/// ```rust
3939
/// # let optional: Option<u32> = Some(0);
40+
/// # fn do_complicated_function() -> u32 { 5 };
4041
/// let _ = if let Some(foo) = optional {
4142
/// foo
4243
/// } else {
@@ -54,9 +55,10 @@ declare_clippy_lint! {
5455
///
5556
/// ```rust
5657
/// # let optional: Option<u32> = Some(0);
58+
/// # fn do_complicated_function() -> u32 { 5 };
5759
/// let _ = optional.map_or(5, |foo| foo);
5860
/// let _ = optional.map_or_else(||{
59-
/// let y = do_complicated_function;
61+
/// let y = do_complicated_function();
6062
/// y*y
6163
/// }, |foo| foo);
6264
/// ```

0 commit comments

Comments
 (0)