mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Check that we're calling Iterator::fold
This commit is contained in:
parent
70a5535ffa
commit
ad164939ed
2 changed files with 8 additions and 4 deletions
|
@ -1126,7 +1126,11 @@ fn lint_iter_cloned_collect(cx: &LateContext, expr: &hir::Expr, iter_args: &[hir
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lint_fold_any(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::Expr]) {
|
fn lint_fold_any(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::Expr]) {
|
||||||
// DONOTMERGE: What if this is just some other method called fold?
|
// Check that this is a call to Iterator::fold rather than just some function called fold
|
||||||
|
if !match_trait_method(cx, expr, &paths::ITERATOR) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert!(fold_args.len() == 3,
|
assert!(fold_args.len() == 3,
|
||||||
"Expected fold_args to have three entries - the receiver, the initial value and the closure");
|
"Expected fold_args to have three entries - the receiver, the initial value and the closure");
|
||||||
|
|
||||||
|
|
|
@ -385,17 +385,17 @@ fn iter_skip_next() {
|
||||||
let _ = foo.filter().skip(42).next();
|
let _ = foo.filter().skip(42).next();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks implementation of the `FOLD_ANY` lint
|
/// Should trigger the `FOLD_ANY` lint
|
||||||
fn fold_any() {
|
fn fold_any() {
|
||||||
let _ = (0..3).fold(false, |acc, x| acc || x > 2);
|
let _ = (0..3).fold(false, |acc, x| acc || x > 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks implementation of the `FOLD_ANY` lint
|
/// Should not trigger the `FOLD_ANY` lint as the initial value is not the literal `false`
|
||||||
fn fold_any_ignores_initial_value_of_true() {
|
fn fold_any_ignores_initial_value_of_true() {
|
||||||
let _ = (0..3).fold(true, |acc, x| acc || x > 2);
|
let _ = (0..3).fold(true, |acc, x| acc || x > 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks implementation of the `FOLD_ANY` lint
|
/// Should not trigger the `FOLD_ANY` lint as the accumulator is not integer valued
|
||||||
fn fold_any_ignores_non_boolean_accumalator() {
|
fn fold_any_ignores_non_boolean_accumalator() {
|
||||||
let _ = (0..3).fold(0, |acc, x| acc + if x > 2 { 1 } else { 0 });
|
let _ = (0..3).fold(0, |acc, x| acc + if x > 2 { 1 } else { 0 });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue