Skip to content

Commit f93d418

Browse files
committed
Auto merge of rust-lang#9099 - joshtriplett:unnecessary-lazy-eval-then-some, r=flip1995
Extend unnecessary_lazy_eval to cover `bool::then` -> `bool::then_some` fixes rust-lang#9097 changelog: Extend `unnecessary_lazy_eval` to convert `bool::then` to `bool::then_some`
2 parents fd605ab + b7230d4 commit f93d418

22 files changed

+78
-54
lines changed

clippy_dev/src/update_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ fn replace_ident_like(contents: &str, replacements: &[(&str, &str)]) -> Option<S
553553
pos = m.end();
554554
}
555555
result.push_str(&contents[pos..]);
556-
edited.then(|| result)
556+
edited.then_some(result)
557557
}
558558

559559
fn round_to_fifty(count: usize) -> usize {

clippy_lints/src/dereference.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,9 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
744744
},
745745
)
746746
},
747-
ExprKind::Call(func, _) if func.hir_id == child_id => (child_id == e.hir_id).then(|| Position::Callee),
747+
ExprKind::Call(func, _) if func.hir_id == child_id => {
748+
(child_id == e.hir_id).then_some(Position::Callee)
749+
},
748750
ExprKind::Call(func, args) => args
749751
.iter()
750752
.position(|arg| arg.hir_id == child_id)

clippy_lints/src/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ fn find_insert_calls<'tcx>(
650650
let allow_insert_closure = s.allow_insert_closure;
651651
let is_single_insert = s.is_single_insert;
652652
let edits = s.edits;
653-
s.can_use_entry.then(|| InsertSearchResults {
653+
s.can_use_entry.then_some(InsertSearchResults {
654654
edits,
655655
allow_insert_closure,
656656
is_single_insert,

clippy_lints/src/inherent_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn get_impl_span(cx: &LateContext<'_>, id: LocalDefId) -> Option<Span> {
127127
(!span.from_expansion()
128128
&& impl_item.generics.params.is_empty()
129129
&& !is_lint_allowed(cx, MULTIPLE_INHERENT_IMPL, id))
130-
.then(|| span)
130+
.then_some(span)
131131
} else {
132132
None
133133
}

clippy_lints/src/manual_non_exhaustive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustiveEnum {
161161
(matches!(v.data, hir::VariantData::Unit(_))
162162
&& v.ident.as_str().starts_with('_')
163163
&& is_doc_hidden(cx.tcx.hir().attrs(v.id)))
164-
.then(|| (id, v.span))
164+
.then_some((id, v.span))
165165
});
166166
if let Some((id, span)) = iter.next()
167167
&& iter.next().is_none()

clippy_lints/src/matches/manual_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn check<'tcx>(
105105

106106
// Determine which binding mode to use.
107107
let explicit_ref = some_pat.contains_explicit_ref_binding();
108-
let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then(|| ty_mutability));
108+
let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then_some(ty_mutability));
109109

110110
let as_ref_str = match binding_ref {
111111
Some(Mutability::Mut) => ".as_mut()",

clippy_lints/src/matches/match_same_arms.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
3838
normalized_pats[i + 1..]
3939
.iter()
4040
.enumerate()
41-
.find_map(|(j, other)| pat.has_overlapping_values(other).then(|| i + 1 + j))
41+
.find_map(|(j, other)| pat.has_overlapping_values(other).then_some(i + 1 + j))
4242
.unwrap_or(normalized_pats.len())
4343
})
4444
.collect();
@@ -55,7 +55,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
5555
.zip(forwards_blocking_idxs[..i].iter().copied().rev())
5656
.skip_while(|&(_, forward_block)| forward_block > i)
5757
.find_map(|((j, other), forward_block)| {
58-
(forward_block == i || pat.has_overlapping_values(other)).then(|| j)
58+
(forward_block == i || pat.has_overlapping_values(other)).then_some(j)
5959
})
6060
.unwrap_or(0)
6161
})

clippy_lints/src/matches/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
10611061
let start = scrutinee_span.hi();
10621062
let mut arm_spans = arms.iter().map(|arm| {
10631063
let data = arm.span.data();
1064-
(data.ctxt == SyntaxContext::root()).then(|| (data.lo, data.hi))
1064+
(data.ctxt == SyntaxContext::root()).then_some((data.lo, data.hi))
10651065
});
10661066
let end = e.span.hi();
10671067

@@ -1095,7 +1095,7 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
10951095
parent: None,
10961096
}
10971097
.span();
1098-
(!span_contains_cfg(cx, span)).then(|| next_start).ok_or(())
1098+
(!span_contains_cfg(cx, span)).then_some(next_start).ok_or(())
10991099
});
11001100
match found {
11011101
Ok(start) => {

clippy_lints/src/methods/manual_str_repeat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
4343
Some(RepeatKind::String)
4444
} else {
4545
let ty = ty.peel_refs();
46-
(ty.is_str() || is_type_diagnostic_item(cx, ty, sym::String)).then(|| RepeatKind::String)
46+
(ty.is_str() || is_type_diagnostic_item(cx, ty, sym::String)).then_some(RepeatKind::String)
4747
}
4848
}
4949
}

clippy_lints/src/methods/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,12 @@ impl Methods {
27372737
}
27382738
},
27392739
("take", []) => needless_option_take::check(cx, expr, recv),
2740+
("then", [arg]) => {
2741+
if !meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) {
2742+
return;
2743+
}
2744+
unnecessary_lazy_eval::check(cx, expr, recv, arg, "then_some");
2745+
},
27402746
("to_os_string" | "to_owned" | "to_path_buf" | "to_vec", []) => {
27412747
implicit_clone::check(cx, name, expr, recv);
27422748
},

clippy_lints/src/methods/unnecessary_lazy_eval.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ pub(super) fn check<'tcx>(
2020
) {
2121
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option);
2222
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
23+
let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
2324

24-
if is_option || is_result {
25+
if is_option || is_result || is_bool {
2526
if let hir::ExprKind::Closure { body, .. } = arg.kind {
2627
let body = cx.tcx.hir().body(body);
2728
let body_expr = &body.value;
@@ -33,8 +34,10 @@ pub(super) fn check<'tcx>(
3334
if eager_or_lazy::switch_to_eager_eval(cx, body_expr) {
3435
let msg = if is_option {
3536
"unnecessary closure used to substitute value for `Option::None`"
36-
} else {
37+
} else if is_result {
3738
"unnecessary closure used to substitute value for `Result::Err`"
39+
} else {
40+
"unnecessary closure used with `bool::then`"
3841
};
3942
let applicability = if body
4043
.params

clippy_lints/src/option_if_let_else.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn detect_option_if_let_else<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) ->
146146
});
147147
if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(local_id), .. })) = e.kind {
148148
match some_captures.get(local_id)
149-
.or_else(|| (method_sugg == "map_or_else").then(|| ()).and_then(|_| none_captures.get(local_id)))
149+
.or_else(|| (method_sugg == "map_or_else").then_some(()).and_then(|_| none_captures.get(local_id)))
150150
{
151151
Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return None,
152152
Some(CaptureKind::Ref(Mutability::Not)) if as_mut => return None,

clippy_lints/src/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Optio
501501
.iter()
502502
.filter_map(get_rptr_lm)
503503
.filter(|&(lt, _, _)| lt.name == out.name)
504-
.map(|(_, mutability, span)| (mutability == Mutability::Not).then(|| span))
504+
.map(|(_, mutability, span)| (mutability == Mutability::Not).then_some(span))
505505
.collect();
506506
if let Some(args) = args
507507
&& !args.is_empty()

clippy_lints/src/swap_ptr_to_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn is_ptr_to_ref(cx: &LateContext<'_>, e: &Expr<'_>, ctxt: SyntaxContext) -> (bo
7373
&& let ExprKind::Unary(UnOp::Deref, derefed_expr) = borrowed_expr.kind
7474
&& cx.typeck_results().expr_ty(derefed_expr).is_unsafe_ptr()
7575
{
76-
(true, (borrowed_expr.span.ctxt() == ctxt || derefed_expr.span.ctxt() == ctxt).then(|| derefed_expr.span))
76+
(true, (borrowed_expr.span.ctxt() == ctxt || derefed_expr.span.ctxt() == ctxt).then_some(derefed_expr.span))
7777
} else {
7878
(false, None)
7979
}

clippy_lints/src/utils/internal_lints/metadata_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ fn get_lint_group(cx: &LateContext<'_>, lint_id: LintId) -> Option<String> {
843843
fn get_lint_level_from_group(lint_group: &str) -> Option<&'static str> {
844844
DEFAULT_LINT_LEVELS
845845
.iter()
846-
.find_map(|(group_name, group_level)| (*group_name == lint_group).then(|| *group_level))
846+
.find_map(|(group_name, group_level)| (*group_name == lint_group).then_some(*group_level))
847847
}
848848

849849
pub(super) fn is_deprecated_lint(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {

clippy_lints/src/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ impl Write {
515515
args.push(arg, span);
516516
}
517517

518-
parser.errors.is_empty().then(move || args)
518+
parser.errors.is_empty().then_some(args)
519519
}
520520

521521
/// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two

clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ pub fn can_move_expr_to_closure<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'
10161016
captures: HirIdMap::default(),
10171017
};
10181018
v.visit_expr(expr);
1019-
v.allow_closure.then(|| v.captures)
1019+
v.allow_closure.then_some(v.captures)
10201020
}
10211021

10221022
/// Returns the method names and argument list of nested method call expressions that make up

clippy_utils/src/msrvs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ macro_rules! msrv_aliases {
1212

1313
// names may refer to stabilized feature flags or library items
1414
msrv_aliases! {
15+
1,62,0 { BOOL_THEN_SOME }
1516
1,53,0 { OR_PATTERNS, MANUAL_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN }
1617
1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
1718
1,51,0 { BORROW_AS_PTR, UNSIGNED_ABS }

clippy_utils/src/source.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub fn snippet_with_context<'a>(
353353
/// span containing `m!(0)`.
354354
pub fn walk_span_to_context(span: Span, outer: SyntaxContext) -> Option<Span> {
355355
let outer_span = hygiene::walk_chain(span, outer);
356-
(outer_span.ctxt() == outer).then(|| outer_span)
356+
(outer_span.ctxt() == outer).then_some(outer_span)
357357
}
358358

359359
/// Removes block comments from the given `Vec` of lines.

tests/ui/unnecessary_lazy_eval.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn main() {
3030
let ext_opt = Some(42);
3131
let nested_opt = Some(Some(42));
3232
let nested_tuple_opt = Some(Some((42, 43)));
33+
let cond = true;
3334

3435
// Should lint - Option
3536
let _ = opt.unwrap_or(2);
@@ -42,6 +43,7 @@ fn main() {
4243
let _ = opt.get_or_insert(2);
4344
let _ = opt.ok_or(2);
4445
let _ = nested_tuple_opt.unwrap_or(Some((1, 2)));
46+
let _ = cond.then_some(astronomers_pi);
4547

4648
// Cases when unwrap is not called on a simple variable
4749
let _ = Some(10).unwrap_or(2);

tests/ui/unnecessary_lazy_eval.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn main() {
3030
let ext_opt = Some(42);
3131
let nested_opt = Some(Some(42));
3232
let nested_tuple_opt = Some(Some((42, 43)));
33+
let cond = true;
3334

3435
// Should lint - Option
3536
let _ = opt.unwrap_or_else(|| 2);
@@ -42,6 +43,7 @@ fn main() {
4243
let _ = opt.get_or_insert_with(|| 2);
4344
let _ = opt.ok_or_else(|| 2);
4445
let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2)));
46+
let _ = cond.then(|| astronomers_pi);
4547

4648
// Cases when unwrap is not called on a simple variable
4749
let _ = Some(10).unwrap_or_else(|| 2);

0 commit comments

Comments
 (0)