[unnecessary_fold]: suggest turbofish if necessary

This commit is contained in:
y21 2023-06-12 02:43:23 +02:00
parent 60258b061d
commit c9daec2585
4 changed files with 217 additions and 11 deletions

View file

@ -49,4 +49,28 @@ fn unnecessary_fold_over_multiple_lines() {
.any(|x| x > 2);
}
fn issue10000() {
use std::collections::HashMap;
use std::hash::BuildHasher;
fn anything<T>(_: T) {}
fn num(_: i32) {}
fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
map.insert(0, 0);
assert_eq!(map.values().sum::<i32>(), 0);
// more cases:
let _ = map.values().sum::<i32>();
let _ = map.values().product::<i32>();
let _: i32 = map.values().sum();
let _: i32 = map.values().product();
anything(map.values().sum::<i32>());
anything(map.values().product::<i32>());
num(map.values().sum());
num(map.values().product());
}
smoketest_map(HashMap::new());
}
fn main() {}

View file

@ -49,4 +49,28 @@ fn unnecessary_fold_over_multiple_lines() {
.fold(false, |acc, x| acc || x > 2);
}
fn issue10000() {
use std::collections::HashMap;
use std::hash::BuildHasher;
fn anything<T>(_: T) {}
fn num(_: i32) {}
fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
map.insert(0, 0);
assert_eq!(map.values().fold(0, |x, y| x + y), 0);
// more cases:
let _ = map.values().fold(0, |x, y| x + y);
let _ = map.values().fold(1, |x, y| x * y);
let _: i32 = map.values().fold(0, |x, y| x + y);
let _: i32 = map.values().fold(1, |x, y| x * y);
anything(map.values().fold(0, |x, y| x + y));
anything(map.values().fold(1, |x, y| x * y));
num(map.values().fold(0, |x, y| x + y));
num(map.values().fold(1, |x, y| x * y));
}
smoketest_map(HashMap::new());
}
fn main() {}

View file

@ -36,5 +36,59 @@ error: this `.fold` can be written more succinctly using another method
LL | .fold(false, |acc, x| acc || x > 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x > 2)`
error: aborting due to 6 previous errors
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:60:33
|
LL | assert_eq!(map.values().fold(0, |x, y| x + y), 0);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum::<i32>()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:63:30
|
LL | let _ = map.values().fold(0, |x, y| x + y);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum::<i32>()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:64:30
|
LL | let _ = map.values().fold(1, |x, y| x * y);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product::<i32>()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:65:35
|
LL | let _: i32 = map.values().fold(0, |x, y| x + y);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:66:35
|
LL | let _: i32 = map.values().fold(1, |x, y| x * y);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:67:31
|
LL | anything(map.values().fold(0, |x, y| x + y));
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum::<i32>()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:68:31
|
LL | anything(map.values().fold(1, |x, y| x * y));
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product::<i32>()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:69:26
|
LL | num(map.values().fold(0, |x, y| x + y));
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
error: this `.fold` can be written more succinctly using another method
--> $DIR/unnecessary_fold.rs:70:26
|
LL | num(map.values().fold(1, |x, y| x * y));
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product()`
error: aborting due to 15 previous errors