Skip to content

Commit 66e1f49

Browse files
committed
Used clippy to clean itself
1 parent 88832f6 commit 66e1f49

19 files changed

+66
-153
lines changed

clippy_lints/src/attrs.rs

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

483483
fn is_relevant_block(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, block: &Block<'_>) -> bool {
484-
if let Some(stmt) = block.stmts.first() {
485-
match &stmt.kind {
484+
block.stmts.first().map_or(block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e)), |stmt| match &stmt.kind {
486485
StmtKind::Local(_) => true,
487486
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
488487
_ => false,
489-
}
490-
} else {
491-
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e))
492-
}
488+
})
493489
}
494490

495491
fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr<'_>) -> bool {
@@ -499,11 +495,7 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp
499495
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
500496
ExprKind::Call(path_expr, _) => {
501497
if let ExprKind::Path(qpath) = &path_expr.kind {
502-
if let Some(fun_id) = tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
503-
!match_def_path(cx, fun_id, &paths::BEGIN_PANIC)
504-
} else {
505-
true
506-
}
498+
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))
507499
} else {
508500
true
509501
}

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
@@ -2470,11 +2470,7 @@ fn derefs_to_slice<'a, 'tcx>(
24702470
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
24712471
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym!(vec_type)),
24722472
ty::Array(_, size) => {
2473-
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
2474-
size < 32
2475-
} else {
2476-
false
2477-
}
2473+
size.try_eval_usize(cx.tcx, cx.param_env).map_or(false, |size| size < 32)
24782474
},
24792475
ty::Ref(_, inner, _) => may_slice(cx, inner),
24802476
_ => 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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ fn fetch_const<'a>(
9090
if args.len() != 2 {
9191
return None;
9292
}
93-
if let Some(c) = constant_simple(cx, cx.tables(), &args[0]) {
94-
if constant_simple(cx, cx.tables(), &args[1]).is_none() {
95-
// otherwise ignore
96-
Some((m, c, &args[1]))
93+
constant_simple(cx, cx.tables, &args[0]).map_or_else(
94+
|| if let Some(c) = constant_simple(cx, cx.tables(), &args[1]) {
95+
Some((m, c, &args[0]))
9796
} else {
9897
None
98+
},
99+
|c| {
100+
if constant_simple(cx, cx.tables, &args[1]).is_none() {
101+
// otherwise ignore
102+
Some((c, &args[1]))
103+
} else {
104+
None
105+
}
99106
}
100-
} else if let Some(c) = constant_simple(cx, cx.tables(), &args[1]) {
101-
Some((m, c, &args[0]))
102-
} else {
103-
None
104-
}
107+
).map(|(c, arg)| (m, c, arg))
105108
}

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
/// ```

clippy_lints/src/returns.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,10 @@ fn is_unit_expr(expr: &ast::Expr) -> bool {
259259

260260
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
261261
let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
262-
if let Some(rpos) = fn_source.rfind("->") {
263-
#[allow(clippy::cast_possible_truncation)]
264-
(
262+
fn_source.rfind("->").map_or((ty.span, Applicability::MaybeIncorrect), |rpos| (
265263
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
266264
Applicability::MachineApplicable,
267-
)
268-
} else {
269-
(ty.span, Applicability::MaybeIncorrect)
270-
}
265+
))
271266
} else {
272267
(ty.span, Applicability::MaybeIncorrect)
273268
};

clippy_lints/src/shadow.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,11 @@ fn check_local<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, local: &'tcx Local<'_>, bin
164164
}
165165

166166
fn is_binding(cx: &LateContext<'_, '_>, pat_id: HirId) -> bool {
167-
let var_ty = cx.tables().node_type_opt(pat_id);
168-
if let Some(var_ty) = var_ty {
169-
match var_ty.kind {
167+
let var_ty = cx.tables.node_type_opt(pat_id);
168+
var_ty.map_or(false, |var_ty| match var_ty.kind {
170169
ty::Adt(..) => false,
171170
_ => true,
172-
}
173-
} else {
174-
false
175-
}
171+
})
176172
}
177173

178174
fn check_pat<'a, 'tcx>(

clippy_lints/src/types.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,16 +1213,14 @@ fn span_lossless_lint(cx: &LateContext<'_, '_>, expr: &Expr<'_>, op: &Expr<'_>,
12131213
// has parens on the outside, they are no longer needed.
12141214
let mut applicability = Applicability::MachineApplicable;
12151215
let opt = snippet_opt(cx, op.span);
1216-
let sugg = if let Some(ref snip) = opt {
1217-
if should_strip_parens(op, snip) {
1216+
let sugg = opt.as_ref().map_or_else(|| {
1217+
applicability = Applicability::HasPlaceholders;
1218+
".."
1219+
}, |snip| if should_strip_parens(op, snip) {
12181220
&snip[1..snip.len() - 1]
12191221
} else {
12201222
snip.as_str()
1221-
}
1222-
} else {
1223-
applicability = Applicability::HasPlaceholders;
1224-
".."
1225-
};
1223+
});
12261224

12271225
span_lint_and_sugg(
12281226
cx,

clippy_lints/src/use_self.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
167167
if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.kind;
168168
then {
169169
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
170-
let should_check = if let Some(ref params) = *parameters {
171-
!params.parenthesized && !params.args.iter().any(|arg| match arg {
170+
let should_check = parameters.as_ref().map_or(true, |params| !params.parenthesized && !params.args.iter().any(|arg| match arg {
172171
GenericArg::Lifetime(_) => true,
173172
_ => false,
174-
})
175-
} else {
176-
true
177-
};
173+
}));
178174

179175
if should_check {
180176
let visitor = &mut UseSelfVisitor {

clippy_lints/src/utils/attrs.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ pub fn get_attr<'a>(
6565
};
6666
let attr_segments = &attr.path.segments;
6767
if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" {
68-
if let Some(deprecation_status) =
69-
BUILTIN_ATTRIBUTES
68+
BUILTIN_ATTRIBUTES
7069
.iter()
7170
.find_map(|(builtin_name, deprecation_status)| {
7271
if *builtin_name == attr_segments[1].ident.to_string() {
7372
Some(deprecation_status)
7473
} else {
7574
None
7675
}
77-
})
78-
{
76+
}).map_or_else(|| {
77+
sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
78+
false
79+
}, |deprecation_status| {
7980
let mut diag = sess.struct_span_err(attr_segments[1].ident.span, "Usage of deprecated attribute");
8081
match *deprecation_status {
8182
DeprecationStatus::Deprecated => {
@@ -97,10 +98,7 @@ pub fn get_attr<'a>(
9798
attr_segments[1].ident.to_string() == name
9899
},
99100
}
100-
} else {
101-
sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
102-
false
103-
}
101+
})
104102
} else {
105103
false
106104
}

0 commit comments

Comments
 (0)