]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | #![allow(dead_code)] |
2 | ||
c620b35d FG |
3 | fn is_any(acc: bool, x: usize) -> bool { |
4 | acc || x > 2 | |
5 | } | |
6 | ||
f20569fa XL |
7 | /// Calls which should trigger the `UNNECESSARY_FOLD` lint |
8 | fn unnecessary_fold() { | |
9 | // Can be replaced by .any | |
10 | let _ = (0..3).fold(false, |acc, x| acc || x > 2); | |
c620b35d FG |
11 | // Can be replaced by .any (checking suggestion) |
12 | let _ = (0..3).fold(false, |acc, x| is_any(acc, x)); | |
f20569fa XL |
13 | // Can be replaced by .all |
14 | let _ = (0..3).fold(true, |acc, x| acc && x > 2); | |
15 | // Can be replaced by .sum | |
16 | let _: i32 = (0..3).fold(0, |acc, x| acc + x); | |
17 | // Can be replaced by .product | |
18 | let _: i32 = (0..3).fold(1, |acc, x| acc * x); | |
19 | } | |
20 | ||
21 | /// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)` | |
22 | fn unnecessary_fold_span_for_multi_element_chain() { | |
23 | let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2); | |
24 | } | |
25 | ||
26 | /// Calls which should not trigger the `UNNECESSARY_FOLD` lint | |
27 | fn unnecessary_fold_should_ignore() { | |
28 | let _ = (0..3).fold(true, |acc, x| acc || x > 2); | |
29 | let _ = (0..3).fold(false, |acc, x| acc && x > 2); | |
30 | let _ = (0..3).fold(1, |acc, x| acc + x); | |
31 | let _ = (0..3).fold(0, |acc, x| acc * x); | |
32 | let _ = (0..3).fold(0, |acc, x| 1 + acc + x); | |
33 | ||
34 | // We only match against an accumulator on the left | |
35 | // hand side. We could lint for .sum and .product when | |
36 | // it's on the right, but don't for now (and this wouldn't | |
37 | // be valid if we extended the lint to cover arbitrary numeric | |
38 | // types). | |
39 | let _ = (0..3).fold(false, |acc, x| x > 2 || acc); | |
40 | let _ = (0..3).fold(true, |acc, x| x > 2 && acc); | |
41 | let _ = (0..3).fold(0, |acc, x| x + acc); | |
42 | let _ = (0..3).fold(1, |acc, x| x * acc); | |
43 | ||
44 | let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len()); | |
45 | let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len()); | |
46 | } | |
47 | ||
48 | /// Should lint only the line containing the fold | |
49 | fn unnecessary_fold_over_multiple_lines() { | |
50 | let _ = (0..3) | |
51 | .map(|x| x + 1) | |
52 | .filter(|x| x % 2 == 0) | |
53 | .fold(false, |acc, x| acc || x > 2); | |
54 | } | |
55 | ||
fe692bf9 FG |
56 | fn issue10000() { |
57 | use std::collections::HashMap; | |
58 | use std::hash::BuildHasher; | |
59 | ||
60 | fn anything<T>(_: T) {} | |
61 | fn num(_: i32) {} | |
62 | fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) { | |
63 | map.insert(0, 0); | |
64 | assert_eq!(map.values().fold(0, |x, y| x + y), 0); | |
65 | ||
66 | // more cases: | |
67 | let _ = map.values().fold(0, |x, y| x + y); | |
68 | let _ = map.values().fold(1, |x, y| x * y); | |
69 | let _: i32 = map.values().fold(0, |x, y| x + y); | |
70 | let _: i32 = map.values().fold(1, |x, y| x * y); | |
71 | anything(map.values().fold(0, |x, y| x + y)); | |
72 | anything(map.values().fold(1, |x, y| x * y)); | |
73 | num(map.values().fold(0, |x, y| x + y)); | |
74 | num(map.values().fold(1, |x, y| x * y)); | |
75 | } | |
76 | ||
77 | smoketest_map(HashMap::new()); | |
78 | } | |
79 | ||
f20569fa | 80 | fn main() {} |