Auto merge of #10931 - y21:issue10000, r=Jarcho

[`unnecessary_fold`]: suggest turbofish if necessary

Fixes #10000

This adds turbofish `::<T>` to the suggestion in `unnecessary_fold`. This is necessary because the `Sum` trait is generic, which breaks inference when changing `fold()` to `sum()`.

changelog: [`unnecessary_fold`]: suggest turbofish if necessary
This commit is contained in:
bors 2023-06-15 05:45:41 +00:00
commit 2dd452f51b
4 changed files with 258 additions and 59 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