Skip to content

Commit ad65fee

Browse files
committed
Clean existing lint code to match new lint
1 parent 2f2d6d2 commit ad65fee

18 files changed

+156
-125
lines changed

clippy_lints/src/attrs.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,14 @@ 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-
block.stmts.first().map_or(block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e)), |stmt| match &stmt.kind {
445+
block.stmts.first().map_or(
446+
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e)),
447+
|stmt| match &stmt.kind {
446448
StmtKind::Local(_) => true,
447449
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
448450
_ => false,
449-
})
451+
},
452+
)
450453
}
451454

452455
fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr<'_>) -> bool {
@@ -456,7 +459,10 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp
456459
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
457460
ExprKind::Call(path_expr, _) => {
458461
if let ExprKind::Path(qpath) = &path_expr.kind {
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))
462+
tables
463+
.qpath_res(qpath, path_expr.hir_id)
464+
.opt_def_id()
465+
.map_or(true, |fun_id| !match_def_path(cx, fun_id, &paths::BEGIN_PANIC))
460466
} else {
461467
true
462468
}

clippy_lints/src/if_let_mutex.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ 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-
self.found_mutex.map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
140+
self.found_mutex
141+
.map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
141142
}
142143
}
143144

clippy_lints/src/len_zero.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ fn has_is_empty(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
302302

303303
let ty = &walk_ptrs_ty(cx.tables.expr_ty(expr));
304304
match ty.kind {
305-
ty::Dynamic(ref tt, ..) => {
306-
tt.principal().map_or(false, |principal| cx.tcx
307-
.associated_items(principal.def_id())
308-
.in_definition_order()
309-
.any(|item| is_is_empty(cx, &item)))
310-
},
305+
ty::Dynamic(ref tt, ..) => tt.principal().map_or(false, |principal| {
306+
cx.tcx
307+
.associated_items(principal.def_id())
308+
.in_definition_order()
309+
.any(|item| is_is_empty(cx, &item))
310+
}),
311311
ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id),
312312
ty::Adt(id, _) => has_is_empty_impl(cx, id.did),
313313
ty::Array(..) | ty::Slice(..) | ty::Str => true,

clippy_lints/src/literal_representation.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,12 @@ impl LiteralDigitGrouping {
265265
let (part, mistyped_suffixes, missing_char) = if let Some((_, exponent)) = &mut num_lit.exponent {
266266
(exponent, &["32", "64"][..], 'f')
267267
} else {
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-
)
268+
num_lit
269+
.fraction
270+
.as_mut()
271+
.map_or((&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i'), |fraction| {
272+
(fraction, &["32", "64"][..], 'f')
273+
})
272274
};
273275

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

clippy_lints/src/loops.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,9 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
686686
NeverLoopResult::AlwaysBreak
687687
}
688688
},
689-
ExprKind::Break(_, ref e) | ExprKind::Ret(ref e) => {
690-
e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak))
691-
},
689+
ExprKind::Break(_, ref e) | ExprKind::Ret(ref e) => e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| {
690+
combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak)
691+
}),
692692
ExprKind::InlineAsm(ref asm) => asm
693693
.operands
694694
.iter()
@@ -1877,9 +1877,9 @@ fn is_ref_iterable_type(cx: &LateContext<'_, '_>, e: &Expr<'_>) -> bool {
18771877
fn is_iterable_array<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'_, 'tcx>) -> bool {
18781878
// IntoIterator is currently only implemented for array sizes <= 32 in rustc
18791879
match ty.kind {
1880-
ty::Array(_, n) => {
1881-
n.try_eval_usize(cx.tcx, cx.param_env).map_or(false, |val| (0..=32).contains(&val))
1882-
},
1880+
ty::Array(_, n) => n
1881+
.try_eval_usize(cx.tcx, cx.param_env)
1882+
.map_or(false, |val| (0..=32).contains(&val)),
18831883
_ => false,
18841884
}
18851885
}
@@ -1891,7 +1891,7 @@ fn extract_expr_from_first_stmt<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<
18911891
return None;
18921892
}
18931893
if let StmtKind::Local(ref local) = block.stmts[0].kind {
1894-
local.init.map(|expr| expr)
1894+
local.init //.map(|expr| expr)
18951895
} else {
18961896
None
18971897
}
@@ -2011,11 +2011,13 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
20112011
if let PatKind::Binding(.., ident, _) = local.pat.kind {
20122012
self.name = Some(ident.name);
20132013

2014-
self.state = local.init.as_ref().map_or(VarState::Declared, |init| if is_integer_const(&self.cx, init, 0) {
2014+
self.state = local.init.as_ref().map_or(VarState::Declared, |init| {
2015+
if is_integer_const(&self.cx, init, 0) {
20152016
VarState::Warn
20162017
} else {
20172018
VarState::Declared
2018-
})
2019+
}
2020+
})
20192021
}
20202022
}
20212023
}

clippy_lints/src/methods/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2466,9 +2466,9 @@ fn derefs_to_slice<'a, 'tcx>(
24662466
ty::Slice(_) => true,
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)),
2469-
ty::Array(_, size) => {
2470-
size.try_eval_usize(cx.tcx, cx.param_env).map_or(false, |size| size < 32)
2471-
},
2469+
ty::Array(_, size) => size
2470+
.try_eval_usize(cx.tcx, cx.param_env)
2471+
.map_or(false, |size| size < 32),
24722472
ty::Ref(_, inner, _) => may_slice(cx, inner),
24732473
_ => false,
24742474
}

clippy_lints/src/methods/unnecessary_filter_map.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ fn check_expression<'a, 'tcx>(
8181
}
8282
(true, true)
8383
},
84-
hir::ExprKind::Block(ref block, _) => {
85-
block.expr.as_ref().map_or((false, false), |expr| check_expression(cx, arg_id, &expr))
86-
},
84+
hir::ExprKind::Block(ref block, _) => block
85+
.expr
86+
.as_ref()
87+
.map_or((false, false), |expr| check_expression(cx, arg_id, &expr)),
8788
hir::ExprKind::Match(_, arms, _) => {
8889
let mut found_mapping = false;
8990
let mut found_filtering = false;

clippy_lints/src/minmax.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MinMaxPass {
5353
}
5454
}
5555

56-
#[derive(PartialEq, Eq, Debug)]
56+
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
5757
enum MinMax {
5858
Min,
5959
Max,
@@ -87,16 +87,15 @@ 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
96-
}
97-
} else if let Some(c) = constant_simple(cx, cx.tables, &args[1]) {
98-
Some((m, c, &args[0]))
99-
} else {
100-
None
101-
}
90+
constant_simple(cx, cx.tables, &args[0]).map_or(
91+
constant_simple(cx, cx.tables, &args[1]).map(|c| (m, c, &args[0])),
92+
|c| {
93+
if constant_simple(cx, cx.tables, &args[1]).is_none() {
94+
// otherwise ignore
95+
Some((m, c, &args[1]))
96+
} else {
97+
None
98+
}
99+
},
100+
)
102101
}

clippy_lints/src/misc.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,9 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr<'_>, other: &Expr<'_>, l
683683
/// of what it means for an expression to be "used".
684684
fn is_used(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
685685
get_parent_expr(cx, expr).map_or(true, |parent| match parent.kind {
686-
ExprKind::Assign(_, ref rhs, _) | ExprKind::AssignOp(_, _, ref rhs) => {
687-
SpanlessEq::new(cx).eq_expr(rhs, expr)
688-
},
689-
_ => is_used(cx, parent),
690-
})
686+
ExprKind::Assign(_, ref rhs, _) | ExprKind::AssignOp(_, _, ref rhs) => SpanlessEq::new(cx).eq_expr(rhs, expr),
687+
_ => is_used(cx, parent),
688+
})
691689
}
692690

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

clippy_lints/src/option_if_let_else.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OptionIfLetElse {
260260
detection.some_expr,
261261
if detection.wrap_braces { " }" } else { "" },
262262
),
263-
Applicability::MachineApplicable,
263+
Applicability::MaybeIncorrect,
264264
);
265265
}
266266
}

clippy_lints/src/returns.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ 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-
(
265-
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
266-
Applicability::MachineApplicable,
267-
)
268-
} else {
269-
(ty.span, Applicability::MaybeIncorrect)
270-
}
262+
fn_source
263+
.rfind("->")
264+
.map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
265+
(
266+
#[allow(clippy::cast_possible_truncation)]
267+
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
268+
Applicability::MachineApplicable,
269+
)
270+
})
271271
} else {
272272
(ty.span, Applicability::MaybeIncorrect)
273273
};

clippy_lints/src/shadow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ fn check_local<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, local: &'tcx Local<'_>, bin
166166
fn is_binding(cx: &LateContext<'_, '_>, pat_id: HirId) -> bool {
167167
let var_ty = cx.tables.node_type_opt(pat_id);
168168
var_ty.map_or(false, |var_ty| match var_ty.kind {
169-
ty::Adt(..) => false,
170-
_ => true,
171-
})
169+
ty::Adt(..) => false,
170+
_ => true,
171+
})
172172
}
173173

174174
fn check_pat<'a, 'tcx>(

clippy_lints/src/types.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1213,14 +1213,19 @@ 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 = opt.as_ref().map_or_else(|| {
1217-
applicability = Applicability::HasPlaceholders;
1218-
".."
1219-
}, |snip| if should_strip_parens(op, snip) {
1220-
&snip[1..snip.len() - 1]
1221-
} else {
1222-
snip.as_str()
1223-
});
1216+
let sugg = opt.as_ref().map_or_else(
1217+
|| {
1218+
applicability = Applicability::HasPlaceholders;
1219+
".."
1220+
},
1221+
|snip| {
1222+
if should_strip_parens(op, snip) {
1223+
&snip[1..snip.len() - 1]
1224+
} else {
1225+
snip.as_str()
1226+
}
1227+
},
1228+
);
12241229

12251230
span_lint_and_sugg(
12261231
cx,

clippy_lints/src/use_self.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,13 @@ 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 = parameters.as_ref().map_or(true, |params| !params.parenthesized && !params.args.iter().any(|arg| match arg {
170+
let should_check = parameters.as_ref().map_or(
171+
true,
172+
|params| !params.parenthesized && !params.args.iter().any(|arg| match arg {
171173
GenericArg::Lifetime(_) => true,
172174
_ => false,
173-
}));
175+
})
176+
);
174177

175178
if should_check {
176179
let visitor = &mut UseSelfVisitor {

clippy_lints/src/utils/attrs.rs

+35-30
Original file line numberDiff line numberDiff line change
@@ -66,39 +66,44 @@ pub fn get_attr<'a>(
6666
let attr_segments = &attr.path.segments;
6767
if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" {
6868
BUILTIN_ATTRIBUTES
69-
.iter()
70-
.find_map(|(builtin_name, deprecation_status)| {
71-
if *builtin_name == attr_segments[1].ident.to_string() {
72-
Some(deprecation_status)
73-
} else {
74-
None
75-
}
76-
}).map_or_else(|| {
77-
sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
78-
false
79-
}, |deprecation_status| {
80-
let mut diag = sess.struct_span_err(attr_segments[1].ident.span, "Usage of deprecated attribute");
81-
match *deprecation_status {
82-
DeprecationStatus::Deprecated => {
83-
diag.emit();
84-
false
85-
},
86-
DeprecationStatus::Replaced(new_name) => {
87-
diag.span_suggestion(
88-
attr_segments[1].ident.span,
89-
"consider using",
90-
new_name.to_string(),
91-
Applicability::MachineApplicable,
92-
);
93-
diag.emit();
69+
.iter()
70+
.find_map(|(builtin_name, deprecation_status)| {
71+
if *builtin_name == attr_segments[1].ident.to_string() {
72+
Some(deprecation_status)
73+
} else {
74+
None
75+
}
76+
})
77+
.map_or_else(
78+
|| {
79+
sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
9480
false
9581
},
96-
DeprecationStatus::None => {
97-
diag.cancel();
98-
attr_segments[1].ident.to_string() == name
82+
|deprecation_status| {
83+
let mut diag =
84+
sess.struct_span_err(attr_segments[1].ident.span, "Usage of deprecated attribute");
85+
match *deprecation_status {
86+
DeprecationStatus::Deprecated => {
87+
diag.emit();
88+
false
89+
},
90+
DeprecationStatus::Replaced(new_name) => {
91+
diag.span_suggestion(
92+
attr_segments[1].ident.span,
93+
"consider using",
94+
new_name.to_string(),
95+
Applicability::MachineApplicable,
96+
);
97+
diag.emit();
98+
false
99+
},
100+
DeprecationStatus::None => {
101+
diag.cancel();
102+
attr_segments[1].ident.to_string() == name
103+
},
104+
}
99105
},
100-
}
101-
})
106+
)
102107
} else {
103108
false
104109
}

0 commit comments

Comments
 (0)