Skip to content

Commit

Permalink
Improve heuristics for determining whether eager of lazy evaluation i…
Browse files Browse the repository at this point in the history
…s preferred
  • Loading branch information
Jarcho committed Sep 5, 2021
1 parent cde7e6b commit a5ae777
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 176 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/functions/too_many_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub(super) fn check_fn(
continue;
}
} else {
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
let single_idx = line.find("//").unwrap_or_else(|| line.len());
let multi_idx = line.find("/*").unwrap_or(line.len());
let single_idx = line.find("//").unwrap_or(line.len());
code_in_line |= multi_idx > 0 && single_idx > 0;
// Implies multi_idx is below line.len()
if multi_idx < single_idx {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/manual_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl LateLintPass<'_> for ManualMap {

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

let as_ref_str = match binding_ref {
Some(Mutability::Mut) => ".as_mut()",
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/from_iter_instead_of_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -
// i.e.: 2 wildcards in `std::collections::BTreeMap<&i32, &char>`
let ty_str = ty.to_string();
let start = ty_str.find('<').unwrap_or(0);
let end = ty_str.find('>').unwrap_or_else(|| ty_str.len());
let end = ty_str.find('>').unwrap_or(ty_str.len());
let nb_wildcard = ty_str[start..end].split(',').count();
let wildcards = format!("_{}", ", _".repeat(nb_wildcard - 1));
format!("{}<{}>", elements.join("::"), wildcards)
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/or_fun_call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::eager_or_lazy::is_lazyness_candidate;
use clippy_utils::eager_or_lazy::switch_to_lazy_eval;
use clippy_utils::is_trait_item;
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_macro_callsite};
use clippy_utils::ty::implements_trait;
Expand Down Expand Up @@ -114,7 +114,7 @@ pub(super) fn check<'tcx>(
if_chain! {
if KNOW_TYPES.iter().any(|k| k.2.contains(&name));

if is_lazyness_candidate(cx, arg);
if switch_to_lazy_eval(cx, arg);
if !contains_return(arg);

let self_ty = cx.typeck_results().expr_ty(self_expr);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/unnecessary_lazy_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(super) fn check<'tcx>(
return;
}

if eager_or_lazy::is_eagerness_candidate(cx, body_expr) {
if eager_or_lazy::switch_to_eager_eval(cx, body_expr) {
let msg = if is_option {
"unnecessary closure used to substitute value for `Option::None`"
} else {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/option_if_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn detect_option_if_let_else<'tcx>(
let capture_mut = if bind_annotation == &BindingAnnotation::Mutable { "mut " } else { "" };
let some_body = extract_body_from_arm(&arms[0])?;
let none_body = extract_body_from_arm(&arms[1])?;
let method_sugg = if eager_or_lazy::is_eagerness_candidate(cx, none_body) { "map_or" } else { "map_or_else" };
let method_sugg = if eager_or_lazy::switch_to_eager_eval(cx, none_body) { "map_or" } else { "map_or_else" };
let capture_name = id.name.to_ident_string();
let (as_ref, as_mut) = match &cond_expr.kind {
ExprKind::AddrOf(_, Mutability::Not, _) => (true, false),
Expand Down
14 changes: 6 additions & 8 deletions clippy_lints/src/redundant_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,12 @@ fn visit_clone_usage(cloned: mir::Local, clone: mir::Local, mir: &mir::Body<'_>,
)
{
self.result.cloned_used = true;
self.result.cloned_consume_or_mutate_loc = self.result.cloned_consume_or_mutate_loc.or_else(|| {
matches!(
ctx,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
| PlaceContext::MutatingUse(MutatingUseContext::Borrow)
)
.then(|| loc)
});
self.result.cloned_consume_or_mutate_loc = self.result.cloned_consume_or_mutate_loc.or(matches!(
ctx,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
| PlaceContext::MutatingUse(MutatingUseContext::Borrow)
)
.then(|| loc));
} else if local == self.clone {
match ctx {
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
Expand Down
Loading

0 comments on commit a5ae777

Please sign in to comment.