Skip to content

Commit 83bb5ec

Browse files
committed
Auto merge of #6202 - giraffate:fix_invalid_suggestion_in_needless_collect_test, r=flip1995
Fix an invalid suggestion in `needless_collect` test A test, https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/needless_collect_indirect.rs#L11-L12, suggests following codes, but the suggested codes don't work. An example is here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6947d9f2806a83f41cc5eb8e39b09d0b. ``` error: avoid using `collect()` when not needed --> $DIR/needless_collect_indirect.rs:11:5 | LL | / let indirect_contains = sample.iter().collect::<VecDeque<_>>(); LL | | indirect_contains.contains(&&5); | |____^ | help: Check if the original Iterator contains an element instead of collecting then checking | LL | LL | sample.iter().any(|x| x == &&5); ``` changelog: none
2 parents 15f6fb9 + 2f5d418 commit 83bb5ec

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

clippy_lints/src/loops.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -3001,7 +3001,14 @@ impl IterFunction {
30013001
IterFunctionKind::IntoIter => String::new(),
30023002
IterFunctionKind::Len => String::from(".count()"),
30033003
IterFunctionKind::IsEmpty => String::from(".next().is_none()"),
3004-
IterFunctionKind::Contains(span) => format!(".any(|x| x == {})", snippet(cx, *span, "..")),
3004+
IterFunctionKind::Contains(span) => {
3005+
let s = snippet(cx, *span, "..");
3006+
if let Some(stripped) = s.strip_prefix('&') {
3007+
format!(".any(|x| x == {})", stripped)
3008+
} else {
3009+
format!(".any(|x| x == *{})", s)
3010+
}
3011+
},
30053012
}
30063013
}
30073014
fn get_suggestion_text(&self) -> &'static str {

tests/ui/needless_collect_indirect.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ fn main() {
1616
.into_iter()
1717
.map(|x| (*x, *x + 1))
1818
.collect::<HashMap<_, _>>();
19+
20+
// #6202
21+
let a = "a".to_string();
22+
let sample = vec![a.clone(), "b".to_string(), "c".to_string()];
23+
let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
24+
non_copy_contains.contains(&a);
1925
}

tests/ui/needless_collect_indirect.stderr

+15-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,21 @@ LL | | indirect_contains.contains(&&5);
4848
help: Check if the original Iterator contains an element instead of collecting then checking
4949
|
5050
LL |
51-
LL | sample.iter().any(|x| x == &&5);
51+
LL | sample.iter().any(|x| x == &5);
5252
|
5353

54-
error: aborting due to 4 previous errors
54+
error: avoid using `collect()` when not needed
55+
--> $DIR/needless_collect_indirect.rs:23:5
56+
|
57+
LL | / let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
58+
LL | | non_copy_contains.contains(&a);
59+
| |____^
60+
|
61+
help: Check if the original Iterator contains an element instead of collecting then checking
62+
|
63+
LL |
64+
LL | sample.into_iter().any(|x| x == a);
65+
|
66+
67+
error: aborting due to 5 previous errors
5568

0 commit comments

Comments
 (0)