-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
Description
Summary
I think clippy has a false positive on clippy::needless-collect
See also this thread https://gitter.im/rust-lang/rust?at=62690723d30b6b44ebbfcd12
Lint Name
clippy::needless-collect
Reproducer
I tried this code:
fn expensive_search() -> Vec<String> {
vec![]
}
fn expensive_transformation(s: String) -> String {
s
}
fn main() {
let things: Vec<String> = expensive_search()
.into_iter()
.map(expensive_transformation)
.collect();
for i in 0..100 {
if things.contains(&i.to_string()) {
println!("foo");
}
}
}
I saw this happen:
error: avoid using `collect()` when not needed
--> bin/main.rs:13:10
|
13 | .collect();
| ^^^^^^^
...
16 | if things.contains(&i.to_string()) {
| ------------------------------- the iterator could be used here instead
|
= note: `-D clippy::needless-collect` implied by `-D warnings`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
help: check if the original Iterator contains an element instead of collecting then checking
|
10 ~
11 |
12 | for i in 0..100 {
13 ~ if expensive_search()
14 + .into_iter()
15 ~ .map(expensive_transformation).any(|x| x == i.to_string()) {
|
I expected to see this happen:
nothing, should be ok
Version
rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-apple-darwin
release: 1.60.0
LLVM version: 14.0.0
Additional Labels
No response
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have