Skip to content

Commit a5ae777

Browse files
committed
Improve heuristics for determining whether eager of lazy evaluation is preferred
1 parent cde7e6b commit a5ae777

15 files changed

+578
-176
lines changed

clippy_lints/src/functions/too_many_lines.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub(super) fn check_fn(
5656
continue;
5757
}
5858
} else {
59-
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
60-
let single_idx = line.find("//").unwrap_or_else(|| line.len());
59+
let multi_idx = line.find("/*").unwrap_or(line.len());
60+
let single_idx = line.find("//").unwrap_or(line.len());
6161
code_in_line |= multi_idx > 0 && single_idx > 0;
6262
// Implies multi_idx is below line.len()
6363
if multi_idx < single_idx {

clippy_lints/src/manual_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl LateLintPass<'_> for ManualMap {
115115

116116
// Determine which binding mode to use.
117117
let explicit_ref = some_pat.contains_explicit_ref_binding();
118-
let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then(|| ty_mutability));
118+
let binding_ref = explicit_ref.or((ty_ref_count != pat_ref_count).then(|| ty_mutability));
119119

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

clippy_lints/src/methods/from_iter_instead_of_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -
6969
// i.e.: 2 wildcards in `std::collections::BTreeMap<&i32, &char>`
7070
let ty_str = ty.to_string();
7171
let start = ty_str.find('<').unwrap_or(0);
72-
let end = ty_str.find('>').unwrap_or_else(|| ty_str.len());
72+
let end = ty_str.find('>').unwrap_or(ty_str.len());
7373
let nb_wildcard = ty_str[start..end].split(',').count();
7474
let wildcards = format!("_{}", ", _".repeat(nb_wildcard - 1));
7575
format!("{}<{}>", elements.join("::"), wildcards)

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::eager_or_lazy::is_lazyness_candidate;
2+
use clippy_utils::eager_or_lazy::switch_to_lazy_eval;
33
use clippy_utils::is_trait_item;
44
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_macro_callsite};
55
use clippy_utils::ty::implements_trait;
@@ -114,7 +114,7 @@ pub(super) fn check<'tcx>(
114114
if_chain! {
115115
if KNOW_TYPES.iter().any(|k| k.2.contains(&name));
116116

117-
if is_lazyness_candidate(cx, arg);
117+
if switch_to_lazy_eval(cx, arg);
118118
if !contains_return(arg);
119119

120120
let self_ty = cx.typeck_results().expr_ty(self_expr);

clippy_lints/src/methods/unnecessary_lazy_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(super) fn check<'tcx>(
3030
return;
3131
}
3232

33-
if eager_or_lazy::is_eagerness_candidate(cx, body_expr) {
33+
if eager_or_lazy::switch_to_eager_eval(cx, body_expr) {
3434
let msg = if is_option {
3535
"unnecessary closure used to substitute value for `Option::None`"
3636
} else {

clippy_lints/src/option_if_let_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn detect_option_if_let_else<'tcx>(
141141
let capture_mut = if bind_annotation == &BindingAnnotation::Mutable { "mut " } else { "" };
142142
let some_body = extract_body_from_arm(&arms[0])?;
143143
let none_body = extract_body_from_arm(&arms[1])?;
144-
let method_sugg = if eager_or_lazy::is_eagerness_candidate(cx, none_body) { "map_or" } else { "map_or_else" };
144+
let method_sugg = if eager_or_lazy::switch_to_eager_eval(cx, none_body) { "map_or" } else { "map_or_else" };
145145
let capture_name = id.name.to_ident_string();
146146
let (as_ref, as_mut) = match &cond_expr.kind {
147147
ExprKind::AddrOf(_, Mutability::Not, _) => (true, false),

clippy_lints/src/redundant_clone.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,12 @@ fn visit_clone_usage(cloned: mir::Local, clone: mir::Local, mir: &mir::Body<'_>,
406406
)
407407
{
408408
self.result.cloned_used = true;
409-
self.result.cloned_consume_or_mutate_loc = self.result.cloned_consume_or_mutate_loc.or_else(|| {
410-
matches!(
411-
ctx,
412-
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
413-
| PlaceContext::MutatingUse(MutatingUseContext::Borrow)
414-
)
415-
.then(|| loc)
416-
});
409+
self.result.cloned_consume_or_mutate_loc = self.result.cloned_consume_or_mutate_loc.or(matches!(
410+
ctx,
411+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
412+
| PlaceContext::MutatingUse(MutatingUseContext::Borrow)
413+
)
414+
.then(|| loc));
417415
} else if local == self.clone {
418416
match ctx {
419417
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)

0 commit comments

Comments
 (0)