Skip to content

Commit a2bcb11

Browse files
committed
apply return_and_then lint to src
1 parent 718e6eb commit a2bcb11

21 files changed

+186
-192
lines changed

clippy_lints/src/booleans.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -403,29 +403,28 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Opti
403403
return None;
404404
}
405405

406-
match binop.node {
406+
let op = match binop.node {
407407
BinOpKind::Eq => Some(" != "),
408408
BinOpKind::Ne => Some(" == "),
409409
BinOpKind::Lt => Some(" >= "),
410410
BinOpKind::Gt => Some(" <= "),
411411
BinOpKind::Le => Some(" > "),
412412
BinOpKind::Ge => Some(" < "),
413413
_ => None,
414-
}
415-
.and_then(|op| {
416-
let lhs_snippet = lhs.span.get_source_text(cx)?;
417-
let rhs_snippet = rhs.span.get_source_text(cx)?;
418-
419-
if !(lhs_snippet.starts_with('(') && lhs_snippet.ends_with(')')) {
420-
if let (ExprKind::Cast(..), BinOpKind::Ge) = (&lhs.kind, binop.node) {
421-
// e.g. `(a as u64) < b`. Without the parens the `<` is
422-
// interpreted as a start of generic arguments for `u64`
423-
return Some(format!("({lhs_snippet}){op}{rhs_snippet}"));
424-
}
414+
}?;
415+
416+
let lhs_snippet = lhs.span.get_source_text(cx)?;
417+
let rhs_snippet = rhs.span.get_source_text(cx)?;
418+
419+
if !(lhs_snippet.starts_with('(') && lhs_snippet.ends_with(')')) {
420+
if let (ExprKind::Cast(..), BinOpKind::Ge) = (&lhs.kind, binop.node) {
421+
// e.g. `(a as u64) < b`. Without the parens the `<` is
422+
// interpreted as a start of generic arguments for `u64`
423+
return Some(format!("({lhs_snippet}){op}{rhs_snippet}"));
425424
}
425+
}
426426

427-
Some(format!("{lhs_snippet}{op}{rhs_snippet}"))
428-
})
427+
Some(format!("{lhs_snippet}{op}{rhs_snippet}"))
429428
},
430429
ExprKind::MethodCall(path, receiver, args, _) => {
431430
let type_of_receiver = cx.typeck_results().expr_ty(receiver);
@@ -434,22 +433,20 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Opti
434433
{
435434
return None;
436435
}
437-
METHODS_WITH_NEGATION
436+
let (_, _, neg_method) = METHODS_WITH_NEGATION
438437
.iter()
439438
.copied()
440439
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
441-
.find(|&(msrv, a, _)| msrv.is_none_or(|msrv| curr_msrv.meets(msrv)) && a == path.ident.name.as_str())
442-
.and_then(|(_, _, neg_method)| {
443-
let negated_args = args
444-
.iter()
445-
.map(|arg| simplify_not(cx, curr_msrv, arg))
446-
.collect::<Option<Vec<_>>>()?
447-
.join(", ");
448-
Some(format!(
449-
"{}.{neg_method}({negated_args})",
450-
receiver.span.get_source_text(cx)?
451-
))
452-
})
440+
.find(|&(msrv, a, _)| msrv.is_none_or(|msrv| curr_msrv.meets(msrv)) && a == path.ident.name.as_str())?;
441+
let negated_args = args
442+
.iter()
443+
.map(|arg| simplify_not(cx, curr_msrv, arg))
444+
.collect::<Option<Vec<_>>>()?
445+
.join(", ");
446+
Some(format!(
447+
"{}.{neg_method}({negated_args})",
448+
receiver.span.get_source_text(cx)?
449+
))
453450
},
454451
ExprKind::Closure(closure) => {
455452
let body = cx.tcx.hir().body(closure.body);

clippy_lints/src/checked_conversions.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ impl LateLintPass<'_> for CheckedConversions {
7272
None => check_upper_bound(lt1, gt1).filter(|cv| cv.cvt == ConversionType::FromUnsigned),
7373
Some((lt2, gt2)) => {
7474
let upper_lower = |lt1, gt1, lt2, gt2| {
75-
check_upper_bound(lt1, gt1)
76-
.zip(check_lower_bound(lt2, gt2))
77-
.and_then(|(l, r)| l.combine(r, cx))
75+
let (l, r) = check_upper_bound(lt1, gt1).zip(check_lower_bound(lt2, gt2))?;
76+
l.combine(r, cx)
7877
};
7978
upper_lower(lt1, gt1, lt2, gt2).or_else(|| upper_lower(lt2, gt2, lt1, gt1))
8079
},

clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ pub(super) fn check<'tcx>(
5555
let big_sugg = assignments
5656
// The only statements in the for loops can be indexed assignments from
5757
// indexed retrievals (except increments of loop counters).
58-
.map(|o| {
59-
o.and_then(|(lhs, rhs)| {
60-
let rhs = fetch_cloned_expr(rhs);
61-
if let ExprKind::Index(base_left, idx_left, _) = lhs.kind
58+
.map(|assignment| {
59+
let (lhs, rhs) = assignment?;
60+
let rhs = fetch_cloned_expr(rhs);
61+
if let ExprKind::Index(base_left, idx_left, _) = lhs.kind
6262
&& let ExprKind::Index(base_right, idx_right, _) = rhs.kind
6363
&& let Some(ty) = get_slice_like_element_ty(cx, cx.typeck_results().expr_ty(base_left))
6464
&& get_slice_like_element_ty(cx, cx.typeck_results().expr_ty(base_right)).is_some()
@@ -68,24 +68,23 @@ pub(super) fn check<'tcx>(
6868
&& !local_used_in(cx, canonical_id, base_right)
6969
// Source and destination must be different
7070
&& path_to_local(base_left) != path_to_local(base_right)
71-
{
72-
Some((
73-
ty,
74-
IndexExpr {
75-
base: base_left,
76-
idx: start_left,
77-
idx_offset: offset_left,
78-
},
79-
IndexExpr {
80-
base: base_right,
81-
idx: start_right,
82-
idx_offset: offset_right,
83-
},
84-
))
85-
} else {
86-
None
87-
}
88-
})
71+
{
72+
Some((
73+
ty,
74+
IndexExpr {
75+
base: base_left,
76+
idx: start_left,
77+
idx_offset: offset_left,
78+
},
79+
IndexExpr {
80+
base: base_right,
81+
idx: start_right,
82+
idx_offset: offset_right,
83+
},
84+
))
85+
} else {
86+
None
87+
}
8988
})
9089
.map(|o| o.map(|(ty, dst, src)| build_manual_memcpy_suggestion(cx, start, end, limits, ty, &dst, &src)))
9190
.collect::<Option<Vec<_>>>()
@@ -380,7 +379,8 @@ fn get_details_from_idx<'tcx>(
380379
offset_opt.map(|(s, o)| (s, Offset::positive(o)))
381380
},
382381
BinOpKind::Sub => {
383-
get_start(lhs, starts).and_then(|s| get_offset(cx, rhs, starts).map(|o| (s, Offset::negative(o))))
382+
let start = get_start(lhs, starts)?;
383+
get_offset(cx, rhs, starts).map(|o| (start, Offset::negative(o)))
384384
},
385385
_ => None,
386386
},
@@ -442,20 +442,19 @@ fn get_loop_counters<'a, 'tcx>(
442442

443443
// For each candidate, check the parent block to see if
444444
// it's initialized to zero at the start of the loop.
445-
get_enclosing_block(cx, expr.hir_id).and_then(|block| {
446-
increment_visitor
447-
.into_results()
448-
.filter_map(move |var_id| {
449-
let mut initialize_visitor = InitializeVisitor::new(cx, expr, var_id);
450-
walk_block(&mut initialize_visitor, block);
451-
452-
initialize_visitor.get_result().map(|(_, _, initializer)| Start {
453-
id: var_id,
454-
kind: StartKind::Counter { initializer },
455-
})
445+
let block = get_enclosing_block(cx, expr.hir_id)?;
446+
increment_visitor
447+
.into_results()
448+
.filter_map(move |var_id| {
449+
let mut initialize_visitor = InitializeVisitor::new(cx, expr, var_id);
450+
walk_block(&mut initialize_visitor, block);
451+
452+
initialize_visitor.get_result().map(|(_, _, initializer)| Start {
453+
id: var_id,
454+
kind: StartKind::Counter { initializer },
456455
})
457-
.into()
458-
})
456+
})
457+
.into()
459458
}
460459

461460
fn is_array_length_equal_to_range(cx: &LateContext<'_>, start: &Expr<'_>, end: &Expr<'_>, arr: &Expr<'_>) -> bool {

clippy_lints/src/minmax.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,12 @@ fn min_max<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a Expr<'a>) -> Option<(MinM
6565
match expr.kind {
6666
ExprKind::Call(path, args) => {
6767
if let ExprKind::Path(ref qpath) = path.kind {
68-
cx.typeck_results()
69-
.qpath_res(qpath, path.hir_id)
70-
.opt_def_id()
71-
.and_then(|def_id| match cx.tcx.get_diagnostic_name(def_id) {
72-
Some(sym::cmp_min) => fetch_const(cx, None, args, MinMax::Min),
73-
Some(sym::cmp_max) => fetch_const(cx, None, args, MinMax::Max),
74-
_ => None,
75-
})
68+
let def_id = cx.typeck_results().qpath_res(qpath, path.hir_id).opt_def_id()?;
69+
match cx.tcx.get_diagnostic_name(def_id) {
70+
Some(sym::cmp_min) => fetch_const(cx, None, args, MinMax::Min),
71+
Some(sym::cmp_max) => fetch_const(cx, None, args, MinMax::Max),
72+
_ => None,
73+
}
7674
} else {
7775
None
7876
}

clippy_lints/src/needless_late_init.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,27 @@ fn first_usage<'tcx>(
214214
) -> Option<Usage<'tcx>> {
215215
let significant_drop = needs_ordered_drop(cx, cx.typeck_results().node_type(binding_id));
216216

217-
block
217+
let stmt = block
218218
.stmts
219219
.iter()
220220
.skip_while(|stmt| stmt.hir_id != local_stmt_id)
221221
.skip(1)
222222
.take_while(|stmt| !significant_drop || !stmt_needs_ordered_drop(cx, stmt))
223-
.find(|&stmt| is_local_used(cx, stmt, binding_id))
224-
.and_then(|stmt| match stmt.kind {
225-
StmtKind::Expr(expr) => Some(Usage {
226-
stmt,
227-
expr,
228-
needs_semi: true,
229-
}),
230-
StmtKind::Semi(expr) => Some(Usage {
231-
stmt,
232-
expr,
233-
needs_semi: false,
234-
}),
235-
_ => None,
236-
})
223+
.find(|&stmt| is_local_used(cx, stmt, binding_id))?;
224+
225+
match stmt.kind {
226+
StmtKind::Expr(expr) => Some(Usage {
227+
stmt,
228+
expr,
229+
needs_semi: true,
230+
}),
231+
StmtKind::Semi(expr) => Some(Usage {
232+
stmt,
233+
expr,
234+
needs_semi: false,
235+
}),
236+
_ => None,
237+
}
237238
}
238239

239240
fn local_snippet_without_semicolon(cx: &LateContext<'_>, local: &LetStmt<'_>) -> Option<SourceText> {

clippy_lints/src/no_effect.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,13 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec
373373
},
374374
ExprKind::Block(block, _) => {
375375
if block.stmts.is_empty() && !block.targeted_by_break {
376-
block.expr.as_ref().and_then(|e| {
377-
match block.rules {
378-
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
379-
BlockCheckMode::DefaultBlock => Some(vec![&**e]),
380-
// in case of compiler-inserted signaling blocks
381-
BlockCheckMode::UnsafeBlock(_) => reduce_expression(cx, e),
382-
}
383-
})
376+
let expr = block.expr.as_ref()?;
377+
match block.rules {
378+
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
379+
BlockCheckMode::DefaultBlock => Some(vec![&**expr]),
380+
// in case of compiler-inserted signaling blocks
381+
BlockCheckMode::UnsafeBlock(_) => reduce_expression(cx, expr),
382+
}
384383
} else {
385384
None
386385
}

clippy_lints/src/option_if_let_else.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,8 @@ fn try_get_option_occurrence<'tcx>(
171171
)) = e.kind
172172
{
173173
match some_captures.get(local_id).or_else(|| {
174-
(method_sugg == "map_or_else")
175-
.then_some(())
176-
.and_then(|()| none_captures.get(local_id))
174+
(method_sugg == "map_or_else").then_some(())?;
175+
none_captures.get(local_id)
177176
}) {
178177
Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return None,
179178
Some(CaptureKind::Ref(Mutability::Not)) if as_mut => return None,

clippy_lints/src/ptr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl<'tcx> DerefTy<'tcx> {
414414
}
415415
}
416416

417+
#[expect(clippy::too_many_lines)]
417418
fn check_fn_args<'cx, 'tcx: 'cx>(
418419
cx: &'cx LateContext<'tcx>,
419420
fn_sig: ty::FnSig<'tcx>,
@@ -467,7 +468,8 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
467468
.output()
468469
.walk()
469470
.filter_map(|arg| {
470-
arg.as_region().and_then(|lifetime| match lifetime.kind() {
471+
let lifetime = arg.as_region()?;
472+
match lifetime.kind() {
471473
ty::ReEarlyParam(r) => Some(
472474
cx.tcx
473475
.generics_of(cx.tcx.parent(param_def_id.to_def_id()))
@@ -481,7 +483,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
481483
| ty::RePlaceholder(_)
482484
| ty::ReErased
483485
| ty::ReError(_) => None,
484-
})
486+
}
485487
})
486488
.any(|def_id| def_id.as_local().is_some_and(|def_id| def_id == param_def_id))
487489
{

clippy_lints/src/regex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ fn lint_syntax_error(cx: &LateContext<'_>, error: &regex_syntax::Error, unescape
222222
}
223223

224224
fn const_str<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<String> {
225-
ConstEvalCtxt::new(cx).eval(e).and_then(|c| match c {
225+
let constant = ConstEvalCtxt::new(cx).eval(e)?;
226+
match constant {
226227
Constant::Str(s) => Some(s),
227228
_ => None,
228-
})
229+
}
229230
}
230231

231232
fn is_trivial_regex(s: &regex_syntax::hir::Hir) -> Option<&'static str> {

clippy_lints/src/suspicious_operation_groupings.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -635,18 +635,17 @@ fn suggestion_with_swapped_ident(
635635
new_ident: Ident,
636636
applicability: &mut Applicability,
637637
) -> Option<String> {
638-
get_ident(expr, location).and_then(|current_ident| {
639-
if eq_id(current_ident, new_ident) {
640-
// We never want to suggest a non-change
641-
return None;
642-
}
638+
let current_ident = get_ident(expr, location)?;
639+
if eq_id(current_ident, new_ident) {
640+
// We never want to suggest a non-change
641+
return None;
642+
}
643643

644-
Some(format!(
645-
"{}{new_ident}{}",
646-
snippet_with_applicability(cx, expr.span.with_hi(current_ident.span.lo()), "..", applicability),
647-
snippet_with_applicability(cx, expr.span.with_lo(current_ident.span.hi()), "..", applicability),
648-
))
649-
})
644+
Some(format!(
645+
"{}{new_ident}{}",
646+
snippet_with_applicability(cx, expr.span.with_hi(current_ident.span.lo()), "..", applicability),
647+
snippet_with_applicability(cx, expr.span.with_lo(current_ident.span.hi()), "..", applicability),
648+
))
650649
}
651650

652651
fn skip_index<A, Iter>(iter: Iter, index: usize) -> impl Iterator<Item = A>

clippy_utils/src/consts.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,8 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
720720
if b {
721721
self.expr(then)
722722
} else {
723-
otherwise.as_ref().and_then(|expr| self.expr(expr))
723+
let expr = otherwise.as_ref()?;
724+
self.expr(expr)
724725
}
725726
} else {
726727
None

0 commit comments

Comments
 (0)